ExpressionExtension.php 909 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Component\ExpressionLanguage\Expression;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFunction;
  14. /**
  15. * ExpressionExtension gives a way to create Expressions from a template.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. final class ExpressionExtension extends AbstractExtension
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getFunctions(): array
  25. {
  26. return [
  27. new TwigFunction('expression', [$this, 'createExpression']),
  28. ];
  29. }
  30. public function createExpression(string $expression): Expression
  31. {
  32. return new Expression($expression);
  33. }
  34. }