vendor/twig/twig/src/Node/Node.php line 51

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;
  12. use Twig\Attribute\YieldReady;
  13. use Twig\Compiler;
  14. use Twig\Source;
  15. /**
  16.  * Represents a node in the AST.
  17.  *
  18.  * @author Fabien Potencier <[email protected]>
  19.  *
  20.  * @implements \IteratorAggregate<int|string, Node>
  21.  */
  22. #[YieldReady]
  23. class Node implements \Countable\IteratorAggregate
  24. {
  25.     /**
  26.      * @var array<string|int, Node>
  27.      */
  28.     protected $nodes;
  29.     protected $attributes;
  30.     protected $lineno;
  31.     protected $tag;
  32.     private $sourceContext;
  33.     /** @var array<string, NameDeprecation> */
  34.     private $nodeNameDeprecations = [];
  35.     /** @var array<string, NameDeprecation> */
  36.     private $attributeNameDeprecations = [];
  37.     /**
  38.      * @param array<string|int, Node> $nodes      An array of named nodes
  39.      * @param array                   $attributes An array of attributes (should not be nodes)
  40.      * @param int                     $lineno     The line number
  41.      */
  42.     public function __construct(array $nodes = [], array $attributes = [], int $lineno 0)
  43.     {
  44.         if (self::class === static::class) {
  45.             trigger_deprecation('twig/twig''3.15'\sprintf('Instantiating "%s" directly is deprecated; the class will become abstract in 4.0.'self::class));
  46.         }
  47.         foreach ($nodes as $name => $node) {
  48.             if (!$node instanceof self) {
  49.                 throw new \InvalidArgumentException(\sprintf('Using "%s" for the value of node "%s" of "%s" is not supported. You must pass a \Twig\Node\Node instance.'get_debug_type($node), $name, static::class));
  50.             }
  51.         }
  52.         $this->nodes $nodes;
  53.         $this->attributes $attributes;
  54.         $this->lineno $lineno;
  55.         if (\func_num_args() > 3) {
  56.             trigger_deprecation('twig/twig''3.12'\sprintf('The "tag" constructor argument of the "%s" class is deprecated and ignored (check which TokenParser class set it to "%s"), the tag is now automatically set by the Parser when needed.', static::class, func_get_arg(3) ?: 'null'));
  57.         }
  58.     }
  59.     public function __toString()
  60.     {
  61.         $repr = static::class;
  62.         if ($this->tag) {
  63.             $repr .= \sprintf("\n  tag: %s"$this->tag);
  64.         }
  65.         $attributes = [];
  66.         foreach ($this->attributes as $name => $value) {
  67.             if (\is_callable($value)) {
  68.                 $v '\Closure';
  69.             } elseif ($value instanceof \Stringable) {
  70.                 $v = (string) $value;
  71.             } else {
  72.                 $v str_replace("\n"''var_export($valuetrue));
  73.             }
  74.             $attributes[] = \sprintf('%s: %s'$name$v);
  75.         }
  76.         if ($attributes) {
  77.             $repr .= \sprintf("\n  attributes:\n    %s"implode("\n    "$attributes));
  78.         }
  79.         if (\count($this->nodes)) {
  80.             $repr .= "\n  nodes:";
  81.             foreach ($this->nodes as $name => $node) {
  82.                 $len \strlen($name) + 6;
  83.                 $noderepr = [];
  84.                 foreach (explode("\n", (string) $node) as $line) {
  85.                     $noderepr[] = str_repeat(' '$len).$line;
  86.                 }
  87.                 $repr .= \sprintf("\n    %s: %s"$nameltrim(implode("\n"$noderepr)));
  88.             }
  89.         }
  90.         return $repr;
  91.     }
  92.     /**
  93.      * @return void
  94.      */
  95.     public function compile(Compiler $compiler)
  96.     {
  97.         foreach ($this->nodes as $node) {
  98.             $compiler->subcompile($node);
  99.         }
  100.     }
  101.     public function getTemplateLine(): int
  102.     {
  103.         return $this->lineno;
  104.     }
  105.     public function getNodeTag(): ?string
  106.     {
  107.         return $this->tag;
  108.     }
  109.     /**
  110.      * @internal
  111.      */
  112.     public function setNodeTag(string $tag): void
  113.     {
  114.         if ($this->tag) {
  115.             throw new \LogicException('The tag of a node can only be set once.');
  116.         }
  117.         $this->tag $tag;
  118.     }
  119.     public function hasAttribute(string $name): bool
  120.     {
  121.         return \array_key_exists($name$this->attributes);
  122.     }
  123.     public function getAttribute(string $name)
  124.     {
  125.         if (!\array_key_exists($name$this->attributes)) {
  126.             throw new \LogicException(\sprintf('Attribute "%s" does not exist for Node "%s".'$name, static::class));
  127.         }
  128.         $triggerDeprecation \func_num_args() > func_get_arg(1) : true;
  129.         if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  130.             $dep $this->attributeNameDeprecations[$name];
  131.             if ($dep->getNewName()) {
  132.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting attribute "%s" on a "%s" class is deprecated, get the "%s" attribute instead.'$name, static::class, $dep->getNewName());
  133.             } else {
  134.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting attribute "%s" on a "%s" class is deprecated.'$name, static::class);
  135.             }
  136.         }
  137.         return $this->attributes[$name];
  138.     }
  139.     public function setAttribute(string $name$value): void
  140.     {
  141.         $triggerDeprecation \func_num_args() > func_get_arg(2) : true;
  142.         if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  143.             $dep $this->attributeNameDeprecations[$name];
  144.             if ($dep->getNewName()) {
  145.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting attribute "%s" on a "%s" class is deprecated, set the "%s" attribute instead.'$name, static::class, $dep->getNewName());
  146.             } else {
  147.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting attribute "%s" on a "%s" class is deprecated.'$name, static::class);
  148.             }
  149.         }
  150.         $this->attributes[$name] = $value;
  151.     }
  152.     public function deprecateAttribute(string $nameNameDeprecation $dep): void
  153.     {
  154.         $this->attributeNameDeprecations[$name] = $dep;
  155.     }
  156.     public function removeAttribute(string $name): void
  157.     {
  158.         unset($this->attributes[$name]);
  159.     }
  160.     /**
  161.      * @param string|int $name
  162.      */
  163.     public function hasNode(string $name): bool
  164.     {
  165.         return isset($this->nodes[$name]);
  166.     }
  167.     /**
  168.      * @param string|int $name
  169.      */
  170.     public function getNode(string $name): self
  171.     {
  172.         if (!isset($this->nodes[$name])) {
  173.             throw new \LogicException(\sprintf('Node "%s" does not exist for Node "%s".'$name, static::class));
  174.         }
  175.         $triggerDeprecation \func_num_args() > func_get_arg(1) : true;
  176.         if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  177.             $dep $this->nodeNameDeprecations[$name];
  178.             if ($dep->getNewName()) {
  179.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting node "%s" on a "%s" class is deprecated, get the "%s" node instead.'$name, static::class, $dep->getNewName());
  180.             } else {
  181.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting node "%s" on a "%s" class is deprecated.'$name, static::class);
  182.             }
  183.         }
  184.         return $this->nodes[$name];
  185.     }
  186.     /**
  187.      * @param string|int $name
  188.      */
  189.     public function setNode(string $nameself $node): void
  190.     {
  191.         $triggerDeprecation \func_num_args() > func_get_arg(2) : true;
  192.         if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  193.             $dep $this->nodeNameDeprecations[$name];
  194.             if ($dep->getNewName()) {
  195.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting node "%s" on a "%s" class is deprecated, set the "%s" node instead.'$name, static::class, $dep->getNewName());
  196.             } else {
  197.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting node "%s" on a "%s" class is deprecated.'$name, static::class);
  198.             }
  199.         }
  200.         if (null !== $this->sourceContext) {
  201.             $node->setSourceContext($this->sourceContext);
  202.         }
  203.         $this->nodes[$name] = $node;
  204.     }
  205.     /**
  206.      * @param string|int $name
  207.      */
  208.     public function removeNode(string $name): void
  209.     {
  210.         unset($this->nodes[$name]);
  211.     }
  212.     /**
  213.      * @param string|int $name
  214.      */
  215.     public function deprecateNode(string $nameNameDeprecation $dep): void
  216.     {
  217.         $this->nodeNameDeprecations[$name] = $dep;
  218.     }
  219.     /**
  220.      * @return int
  221.      */
  222.     #[\ReturnTypeWillChange]
  223.     public function count()
  224.     {
  225.         return \count($this->nodes);
  226.     }
  227.     public function getIterator(): \Traversable
  228.     {
  229.         return new \ArrayIterator($this->nodes);
  230.     }
  231.     public function getTemplateName(): ?string
  232.     {
  233.         return $this->sourceContext $this->sourceContext->getName() : null;
  234.     }
  235.     public function setSourceContext(Source $source): void
  236.     {
  237.         $this->sourceContext $source;
  238.         foreach ($this->nodes as $node) {
  239.             $node->setSourceContext($source);
  240.         }
  241.     }
  242.     public function getSourceContext(): ?Source
  243.     {
  244.         return $this->sourceContext;
  245.     }
  246. }