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

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. public function getAttribute(string $name)
  130. {
  131. if (!\array_key_exists($name, $this->attributes)) {
  132. throw new \LogicException(\sprintf('Attribute "%s" does not exist for Node "%s".', $name, static::class));
  133. }
  134. $triggerDeprecation = \func_num_args() > 1 ? func_get_arg(1) : true;
  135. if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  136. $dep = $this->attributeNameDeprecations[$name];
  137. if ($dep->getNewName()) {
  138. 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());
  139. } else {
  140. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting attribute "%s" on a "%s" class is deprecated.', $name, static::class);
  141. }
  142. }
  143. return $this->attributes[$name];
  144. }
  145. public function setAttribute(string $name, $value): void
  146. {
  147. $triggerDeprecation = \func_num_args() > 2 ? func_get_arg(2) : true;
  148. if ($triggerDeprecation && isset($this->attributeNameDeprecations[$name])) {
  149. $dep = $this->attributeNameDeprecations[$name];
  150. if ($dep->getNewName()) {
  151. 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());
  152. } else {
  153. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting attribute "%s" on a "%s" class is deprecated.', $name, static::class);
  154. }
  155. }
  156. $this->attributes[$name] = $value;
  157. }
  158. public function deprecateAttribute(string $name, NameDeprecation $dep): void
  159. {
  160. $this->attributeNameDeprecations[$name] = $dep;
  161. }
  162. public function removeAttribute(string $name): void
  163. {
  164. unset($this->attributes[$name]);
  165. }
  166. /**
  167. * @param string|int $name
  168. */
  169. public function hasNode(string $name): bool
  170. {
  171. return isset($this->nodes[$name]);
  172. }
  173. /**
  174. * @param string|int $name
  175. */
  176. public function getNode(string $name): self
  177. {
  178. if (!isset($this->nodes[$name])) {
  179. throw new \LogicException(\sprintf('Node "%s" does not exist for Node "%s".', $name, static::class));
  180. }
  181. $triggerDeprecation = \func_num_args() > 1 ? func_get_arg(1) : true;
  182. if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  183. $dep = $this->nodeNameDeprecations[$name];
  184. if ($dep->getNewName()) {
  185. 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());
  186. } else {
  187. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Getting node "%s" on a "%s" class is deprecated.', $name, static::class);
  188. }
  189. }
  190. return $this->nodes[$name];
  191. }
  192. /**
  193. * @param string|int $name
  194. */
  195. public function setNode(string $name, self $node): void
  196. {
  197. $triggerDeprecation = \func_num_args() > 2 ? func_get_arg(2) : true;
  198. if ($triggerDeprecation && isset($this->nodeNameDeprecations[$name])) {
  199. $dep = $this->nodeNameDeprecations[$name];
  200. if ($dep->getNewName()) {
  201. 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());
  202. } else {
  203. trigger_deprecation($dep->getPackage(), $dep->getVersion(), 'Setting node "%s" on a "%s" class is deprecated.', $name, static::class);
  204. }
  205. }
  206. if (null !== $this->sourceContext) {
  207. $node->setSourceContext($this->sourceContext);
  208. }
  209. $this->nodes[$name] = $node;
  210. }
  211. /**
  212. * @param string|int $name
  213. */
  214. public function removeNode(string $name): void
  215. {
  216. unset($this->nodes[$name]);
  217. }
  218. /**
  219. * @param string|int $name
  220. */
  221. public function deprecateNode(string $name, NameDeprecation $dep): void
  222. {
  223. $this->nodeNameDeprecations[$name] = $dep;
  224. }
  225. /**
  226. * @return int
  227. */
  228. #[\ReturnTypeWillChange]
  229. public function count()
  230. {
  231. return \count($this->nodes);
  232. }
  233. public function getIterator(): \Traversable
  234. {
  235. return new \ArrayIterator($this->nodes);
  236. }
  237. public function getTemplateName(): ?string
  238. {
  239. return $this->sourceContext ? $this->sourceContext->getName() : null;
  240. }
  241. public function setSourceContext(Source $source): void
  242. {
  243. $this->sourceContext = $source;
  244. foreach ($this->nodes as $node) {
  245. $node->setSourceContext($source);
  246. }
  247. }
  248. public function getSourceContext(): ?Source
  249. {
  250. return $this->sourceContext;
  251. }
  252. }