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

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\Compiler;
  13. use Twig\Source;
  14. /**
  15. * Represents a node in the AST.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Node implements \Countable, \IteratorAggregate
  20. {
  21. protected $nodes;
  22. protected $attributes;
  23. protected $lineno;
  24. protected $tag;
  25. private $name;
  26. private $sourceContext;
  27. /**
  28. * @param array $nodes An array of named nodes
  29. * @param array $attributes An array of attributes (should not be nodes)
  30. * @param int $lineno The line number
  31. * @param string $tag The tag name associated with the Node
  32. */
  33. public function __construct(array $nodes = [], array $attributes = [], int $lineno = 0, string $tag = null)
  34. {
  35. foreach ($nodes as $name => $node) {
  36. if (!$node instanceof self) {
  37. 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));
  38. }
  39. }
  40. $this->nodes = $nodes;
  41. $this->attributes = $attributes;
  42. $this->lineno = $lineno;
  43. $this->tag = $tag;
  44. }
  45. public function __toString()
  46. {
  47. $attributes = [];
  48. foreach ($this->attributes as $name => $value) {
  49. $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
  50. }
  51. $repr = [static::class.'('.implode(', ', $attributes)];
  52. if (\count($this->nodes)) {
  53. foreach ($this->nodes as $name => $node) {
  54. $len = \strlen($name) + 4;
  55. $noderepr = [];
  56. foreach (explode("\n", (string) $node) as $line) {
  57. $noderepr[] = str_repeat(' ', $len).$line;
  58. }
  59. $repr[] = sprintf(' %s: %s', $name, ltrim(implode("\n", $noderepr)));
  60. }
  61. $repr[] = ')';
  62. } else {
  63. $repr[0] .= ')';
  64. }
  65. return implode("\n", $repr);
  66. }
  67. public function compile(Compiler $compiler)
  68. {
  69. foreach ($this->nodes as $node) {
  70. $node->compile($compiler);
  71. }
  72. }
  73. public function getTemplateLine()
  74. {
  75. return $this->lineno;
  76. }
  77. public function getNodeTag()
  78. {
  79. return $this->tag;
  80. }
  81. /**
  82. * @return bool
  83. */
  84. public function hasAttribute($name)
  85. {
  86. return \array_key_exists($name, $this->attributes);
  87. }
  88. /**
  89. * @return mixed
  90. */
  91. public function getAttribute($name)
  92. {
  93. if (!\array_key_exists($name, $this->attributes)) {
  94. throw new \LogicException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, static::class));
  95. }
  96. return $this->attributes[$name];
  97. }
  98. /**
  99. * @param string $name
  100. * @param mixed $value
  101. */
  102. public function setAttribute($name, $value)
  103. {
  104. $this->attributes[$name] = $value;
  105. }
  106. public function removeAttribute($name)
  107. {
  108. unset($this->attributes[$name]);
  109. }
  110. /**
  111. * @return bool
  112. */
  113. public function hasNode($name)
  114. {
  115. return isset($this->nodes[$name]);
  116. }
  117. /**
  118. * @return Node
  119. */
  120. public function getNode($name)
  121. {
  122. if (!isset($this->nodes[$name])) {
  123. throw new \LogicException(sprintf('Node "%s" does not exist for Node "%s".', $name, static::class));
  124. }
  125. return $this->nodes[$name];
  126. }
  127. public function setNode($name, self $node)
  128. {
  129. $this->nodes[$name] = $node;
  130. }
  131. public function removeNode($name)
  132. {
  133. unset($this->nodes[$name]);
  134. }
  135. /**
  136. * @return int
  137. */
  138. #[\ReturnTypeWillChange]
  139. public function count()
  140. {
  141. return \count($this->nodes);
  142. }
  143. /**
  144. * @return \Traversable
  145. */
  146. #[\ReturnTypeWillChange]
  147. public function getIterator()
  148. {
  149. return new \ArrayIterator($this->nodes);
  150. }
  151. /**
  152. * @deprecated since 2.8 (to be removed in 3.0)
  153. */
  154. public function setTemplateName($name/*, $triggerDeprecation = true */)
  155. {
  156. $triggerDeprecation = 2 > \func_num_args() || \func_get_arg(1);
  157. if ($triggerDeprecation) {
  158. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use setSourceContext() instead.', \E_USER_DEPRECATED);
  159. }
  160. $this->name = $name;
  161. foreach ($this->nodes as $node) {
  162. $node->setTemplateName($name, $triggerDeprecation);
  163. }
  164. }
  165. public function getTemplateName()
  166. {
  167. return $this->sourceContext ? $this->sourceContext->getName() : null;
  168. }
  169. public function setSourceContext(Source $source)
  170. {
  171. $this->sourceContext = $source;
  172. foreach ($this->nodes as $node) {
  173. $node->setSourceContext($source);
  174. }
  175. $this->setTemplateName($source->getName(), false);
  176. }
  177. public function getSourceContext()
  178. {
  179. return $this->sourceContext;
  180. }
  181. }
  182. class_alias('Twig\Node\Node', 'Twig_Node');
  183. // Ensure that the aliased name is loaded to keep BC for classes implementing the typehint with the old aliased name.
  184. class_exists('Twig\Compiler');