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

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