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

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 <fabien@symfony.com>
  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(): string
  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($value, true));
  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", $name, ltrim(implode("\n", $noderepr)));
  88. }
  89. }
  90. return $repr;
  91. }
  92. public function __clone()
  93. {
  94. foreach ($this->nodes as $name => $node) {
  95. $this->nodes[$name] = clone $node;
  96. }
  97. }
  98. /**
  99. * @return void
  100. */
  101. public function compile(Compiler $compiler)
  102. {
  103. foreach ($this->nodes as $node) {
  104. $compiler->subcompile($node);
  105. }
  106. }
  107. public function getTemplateLine(): int
  108. {
  109. return $this->lineno;
  110. }
  111. public function getNodeTag(): ?string
  112. {
  113. return $this->tag;
  114. }
  115. /**
  116. * @internal
  117. */
  118. public function setNodeTag(string $tag): void
  119. {
  120. if ($this->tag) {
  121. throw new \LogicException('The tag of a node can only be set once.');
  122. }
  123. $this->tag = $tag;
  124. }
  125. public function hasAttribute(string $name): bool
  126. {
  127. return \array_key_exists($name, $this->attributes);
  128. }
  129. /**
  130. * @return mixed
  131. */
  132. public function getAttribute(string $name)
  133. {
  134. if (!\array_key_exists($name, $this->attributes)) {
  135. throw new \LogicException(\sprintf('Attribute "%s" does not exist for Node "%s".', $name, static::class));
  136. }
  137. $triggerDeprecation = \func_num_args() > 1 ? func_get_arg(1) : true;
  138. if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  139. $dep = $this->attributeNameDeprecations[$name];
  140. if ($dep->getNewName()) {
  141. 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());
  142. } else {
  143. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting attribute "%s" on a "%s" class is deprecated.', $name, static::class);
  144. }
  145. }
  146. return $this->attributes[$name];
  147. }
  148. public function setAttribute(string $name, $value): void
  149. {
  150. $triggerDeprecation = \func_num_args() > 2 ? func_get_arg(2) : true;
  151. if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  152. $dep = $this->attributeNameDeprecations[$name];
  153. if ($dep->getNewName()) {
  154. 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());
  155. } else {
  156. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting attribute "%s" on a "%s" class is deprecated.', $name, static::class);
  157. }
  158. }
  159. $this->attributes[$name] = $value;
  160. }
  161. public function deprecateAttribute(string $name, NameDeprecation $dep): void
  162. {
  163. $this->attributeNameDeprecations[$name] = $dep;
  164. }
  165. public function removeAttribute(string $name): void
  166. {
  167. unset($this->attributes[$name]);
  168. }
  169. /**
  170. * @param string|int $name
  171. */
  172. public function hasNode(string $name): bool
  173. {
  174. return isset($this->nodes[$name]);
  175. }
  176. /**
  177. * @param string|int $name
  178. */
  179. public function getNode(string $name): self
  180. {
  181. if (!isset($this->nodes[$name])) {
  182. throw new \LogicException(\sprintf('Node "%s" does not exist for Node "%s".', $name, static::class));
  183. }
  184. $triggerDeprecation = \func_num_args() > 1 ? func_get_arg(1) : true;
  185. if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  186. $dep = $this->nodeNameDeprecations[$name];
  187. if ($dep->getNewName()) {
  188. 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());
  189. } else {
  190. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting node "%s" on a "%s" class is deprecated.', $name, static::class);
  191. }
  192. }
  193. return $this->nodes[$name];
  194. }
  195. /**
  196. * @param string|int $name
  197. */
  198. public function setNode(string $name, self $node): void
  199. {
  200. $triggerDeprecation = \func_num_args() > 2 ? func_get_arg(2) : true;
  201. if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  202. $dep = $this->nodeNameDeprecations[$name];
  203. if ($dep->getNewName()) {
  204. 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());
  205. } else {
  206. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting node "%s" on a "%s" class is deprecated.', $name, static::class);
  207. }
  208. }
  209. if (null !== $this->sourceContext) {
  210. $node->setSourceContext($this->sourceContext);
  211. }
  212. $this->nodes[$name] = $node;
  213. }
  214. /**
  215. * @param string|int $name
  216. */
  217. public function removeNode(string $name): void
  218. {
  219. unset($this->nodes[$name]);
  220. }
  221. /**
  222. * @param string|int $name
  223. */
  224. public function deprecateNode(string $name, NameDeprecation $dep): void
  225. {
  226. $this->nodeNameDeprecations[$name] = $dep;
  227. }
  228. /**
  229. * @return int
  230. */
  231. #[\ReturnTypeWillChange]
  232. public function count()
  233. {
  234. return \count($this->nodes);
  235. }
  236. public function getIterator(): \Traversable
  237. {
  238. return new \ArrayIterator($this->nodes);
  239. }
  240. public function getTemplateName(): ?string
  241. {
  242. return $this->sourceContext ? $this->sourceContext->getName() : null;
  243. }
  244. public function setSourceContext(Source $source): void
  245. {
  246. $this->sourceContext = $source;
  247. foreach ($this->nodes as $node) {
  248. $node->setSourceContext($source);
  249. }
  250. }
  251. public function getSourceContext(): ?Source
  252. {
  253. return $this->sourceContext;
  254. }
  255. }