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

  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 <fabien@symfony.com>
  19.  */
  20. #[YieldReady]
  21. class Node implements \Countable\IteratorAggregate
  22. {
  23.     protected $nodes;
  24.     protected $attributes;
  25.     protected $lineno;
  26.     protected $tag;
  27.     private $sourceContext;
  28.     /** @var array<string, NameDeprecation> */
  29.     private $nodeNameDeprecations = [];
  30.     /** @var array<string, NameDeprecation> */
  31.     private $attributeNameDeprecations = [];
  32.     /**
  33.      * @param array  $nodes      An array of named nodes
  34.      * @param array  $attributes An array of attributes (should not be nodes)
  35.      * @param int    $lineno     The line number
  36.      * @param string $tag        The tag name associated with the Node
  37.      */
  38.     public function __construct(array $nodes = [], array $attributes = [], int $lineno 0, ?string $tag null)
  39.     {
  40.         foreach ($nodes as $name => $node) {
  41.             if (!$node instanceof self) {
  42.                 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.'\is_object($node) ? \get_class($node) : (null === $node 'null' \gettype($node)), $name, static::class));
  43.             }
  44.         }
  45.         $this->nodes $nodes;
  46.         $this->attributes $attributes;
  47.         $this->lineno $lineno;
  48.         $this->tag $tag;
  49.     }
  50.     public function __toString()
  51.     {
  52.         $attributes = [];
  53.         foreach ($this->attributes as $name => $value) {
  54.             $attributes[] = \sprintf('%s: %s'$name\is_callable($value) ? '\Closure' str_replace("\n"''var_export($valuetrue)));
  55.         }
  56.         $repr = [static::class.'('.implode(', '$attributes)];
  57.         if (\count($this->nodes)) {
  58.             foreach ($this->nodes as $name => $node) {
  59.                 $len \strlen($name) + 4;
  60.                 $noderepr = [];
  61.                 foreach (explode("\n", (string) $node) as $line) {
  62.                     $noderepr[] = str_repeat(' '$len).$line;
  63.                 }
  64.                 $repr[] = \sprintf('  %s: %s'$nameltrim(implode("\n"$noderepr)));
  65.             }
  66.             $repr[] = ')';
  67.         } else {
  68.             $repr[0] .= ')';
  69.         }
  70.         return implode("\n"$repr);
  71.     }
  72.     /**
  73.      * @return void
  74.      */
  75.     public function compile(Compiler $compiler)
  76.     {
  77.         foreach ($this->nodes as $node) {
  78.             $compiler->subcompile($node);
  79.         }
  80.     }
  81.     public function getTemplateLine(): int
  82.     {
  83.         return $this->lineno;
  84.     }
  85.     public function getNodeTag(): ?string
  86.     {
  87.         return $this->tag;
  88.     }
  89.     public function hasAttribute(string $name): bool
  90.     {
  91.         return \array_key_exists($name$this->attributes);
  92.     }
  93.     public function getAttribute(string $name)
  94.     {
  95.         if (!\array_key_exists($name$this->attributes)) {
  96.             throw new \LogicException(\sprintf('Attribute "%s" does not exist for Node "%s".'$name, static::class));
  97.         }
  98.         $triggerDeprecation \func_num_args() > func_get_arg(1) : true;
  99.         if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  100.             $dep $this->attributeNameDeprecations[$name];
  101.             if ($dep->getNewName()) {
  102.                 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());
  103.             } else {
  104.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting attribute "%s" on a "%s" class is deprecated.'$name, static::class);
  105.             }
  106.         }
  107.         return $this->attributes[$name];
  108.     }
  109.     public function setAttribute(string $name$value): void
  110.     {
  111.         $triggerDeprecation \func_num_args() > func_get_arg(2) : true;
  112.         if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  113.             $dep $this->attributeNameDeprecations[$name];
  114.             if ($dep->getNewName()) {
  115.                 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());
  116.             } else {
  117.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting attribute "%s" on a "%s" class is deprecated.'$name, static::class);
  118.             }
  119.         }
  120.         $this->attributes[$name] = $value;
  121.     }
  122.     public function deprecateAttribute(string $nameNameDeprecation $dep): void
  123.     {
  124.         $this->attributeNameDeprecations[$name] = $dep;
  125.     }
  126.     public function removeAttribute(string $name): void
  127.     {
  128.         unset($this->attributes[$name]);
  129.     }
  130.     public function hasNode(string $name): bool
  131.     {
  132.         return isset($this->nodes[$name]);
  133.     }
  134.     public function getNode(string $name): self
  135.     {
  136.         if (!isset($this->nodes[$name])) {
  137.             throw new \LogicException(\sprintf('Node "%s" does not exist for Node "%s".'$name, static::class));
  138.         }
  139.         $triggerDeprecation \func_num_args() > func_get_arg(1) : true;
  140.         if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  141.             $dep $this->nodeNameDeprecations[$name];
  142.             if ($dep->getNewName()) {
  143.                 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());
  144.             } else {
  145.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting node "%s" on a "%s" class is deprecated.'$name, static::class);
  146.             }
  147.         }
  148.         return $this->nodes[$name];
  149.     }
  150.     public function setNode(string $nameself $node): void
  151.     {
  152.         $triggerDeprecation \func_num_args() > func_get_arg(2) : true;
  153.         if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  154.             $dep $this->nodeNameDeprecations[$name];
  155.             if ($dep->getNewName()) {
  156.                 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());
  157.             } else {
  158.                 trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting node "%s" on a "%s" class is deprecated.'$name, static::class);
  159.             }
  160.         }
  161.         if (null !== $this->sourceContext) {
  162.             $node->setSourceContext($this->sourceContext);
  163.         }
  164.         $this->nodes[$name] = $node;
  165.     }
  166.     public function removeNode(string $name): void
  167.     {
  168.         unset($this->nodes[$name]);
  169.     }
  170.     public function deprecateNode(string $nameNameDeprecation $dep): void
  171.     {
  172.         $this->nodeNameDeprecations[$name] = $dep;
  173.     }
  174.     /**
  175.      * @return int
  176.      */
  177.     #[\ReturnTypeWillChange]
  178.     public function count()
  179.     {
  180.         return \count($this->nodes);
  181.     }
  182.     public function getIterator(): \Traversable
  183.     {
  184.         return new \ArrayIterator($this->nodes);
  185.     }
  186.     public function getTemplateName(): ?string
  187.     {
  188.         return $this->sourceContext $this->sourceContext->getName() : null;
  189.     }
  190.     public function setSourceContext(Source $source): void
  191.     {
  192.         $this->sourceContext $source;
  193.         foreach ($this->nodes as $node) {
  194.             $node->setSourceContext($source);
  195.         }
  196.     }
  197.     public function getSourceContext(): ?Source
  198.     {
  199.         return $this->sourceContext;
  200.     }
  201. }