vendor/twig/twig/src/Node/Expression/FilterExpression.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. * (c) Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Twig\Node\Expression;
  12. use Twig\Attribute\FirstClassTwigCallableReady;
  13. use Twig\Compiler;
  14. use Twig\Node\NameDeprecation;
  15. use Twig\Node\Node;
  16. use Twig\TwigFilter;
  17. class FilterExpression extends CallExpression
  18. {
  19. /**
  20. * @param AbstractExpression $node
  21. */
  22. #[FirstClassTwigCallableReady]
  23. public function __construct(Node $node, TwigFilter|ConstantExpression $filter, Node $arguments, int $lineno)
  24. {
  25. if (!$node instanceof AbstractExpression) {
  26. trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, $node::class);
  27. }
  28. if ($filter instanceof TwigFilter) {
  29. $name = $filter->getName();
  30. $filterName = new ConstantExpression($name, $lineno);
  31. } else {
  32. $name = $filter->getAttribute('value');
  33. $filterName = $filter;
  34. trigger_deprecation('twig/twig', '3.12', 'Not passing an instance of "TwigFilter" when creating a "%s" filter of type "%s" is deprecated.', $name, static::class);
  35. }
  36. parent::__construct(['node' => $node, 'filter' => $filterName, 'arguments' => $arguments], ['name' => $name, 'type' => 'filter'], $lineno);
  37. if ($filter instanceof TwigFilter) {
  38. $this->setAttribute('twig_callable', $filter);
  39. }
  40. $this->deprecateNode('filter', new NameDeprecation('twig/twig', '3.12'));
  41. $this->deprecateAttribute('needs_charset', new NameDeprecation('twig/twig', '3.12'));
  42. $this->deprecateAttribute('needs_environment', new NameDeprecation('twig/twig', '3.12'));
  43. $this->deprecateAttribute('needs_context', new NameDeprecation('twig/twig', '3.12'));
  44. $this->deprecateAttribute('arguments', new NameDeprecation('twig/twig', '3.12'));
  45. $this->deprecateAttribute('callable', new NameDeprecation('twig/twig', '3.12'));
  46. $this->deprecateAttribute('is_variadic', new NameDeprecation('twig/twig', '3.12'));
  47. $this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12'));
  48. }
  49. public function compile(Compiler $compiler): void
  50. {
  51. $name = $this->getNode('filter', false)->getAttribute('value');
  52. if ($name !== $this->getAttribute('name')) {
  53. trigger_deprecation('twig/twig', '3.11', 'Changing the value of a "filter" node in a NodeVisitor class is not supported anymore.');
  54. $this->removeAttribute('twig_callable');
  55. }
  56. if ('raw' === $name) {
  57. trigger_deprecation('twig/twig', '3.11', 'Creating the "raw" filter via "FilterExpression" is deprecated; use "RawFilter" instead.');
  58. $compiler->subcompile($this->getNode('node'));
  59. return;
  60. }
  61. if (!$this->hasAttribute('twig_callable')) {
  62. $this->setAttribute('twig_callable', $compiler->getEnvironment()->getFilter($name));
  63. }
  64. $this->compileCallable($compiler);
  65. }
  66. }