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