PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / vendor_prefixed / twig / src / NodeVisitor / OptimizerNodeVisitor.php

OptimizerNodeVisitor.php in Elementor Website Builder – more than just a page builder 3.28.0-dev1, at vendor_prefixed/twig/src/NodeVisitor/OptimizerNodeVisitor.php

182 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
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 ElementorDeps\Twig\NodeVisitor;
12
13 use ElementorDeps\Twig\Environment;
14 use ElementorDeps\Twig\Node\BlockReferenceNode;
15 use ElementorDeps\Twig\Node\Expression\BlockReferenceExpression;
16 use ElementorDeps\Twig\Node\Expression\ConstantExpression;
17 use ElementorDeps\Twig\Node\Expression\FunctionExpression;
18 use ElementorDeps\Twig\Node\Expression\GetAttrExpression;
19 use ElementorDeps\Twig\Node\Expression\NameExpression;
20 use ElementorDeps\Twig\Node\Expression\ParentExpression;
21 use ElementorDeps\Twig\Node\ForNode;
22 use ElementorDeps\Twig\Node\IncludeNode;
23 use ElementorDeps\Twig\Node\Node;
24 use ElementorDeps\Twig\Node\PrintNode;
25 use ElementorDeps\Twig\Node\TextNode;
26 /**
27 * Tries to optimize the AST.
28 *
29 * This visitor is always the last registered one.
30 *
31 * You can configure which optimizations you want to activate via the
32 * optimizer mode.
33 *
34 * @author Fabien Potencier <fabien@symfony.com>
35 *
36 * @internal
37 */
38 final class OptimizerNodeVisitor implements NodeVisitorInterface
39 {
40 public const OPTIMIZE_ALL = -1;
41 public const OPTIMIZE_NONE = 0;
42 public const OPTIMIZE_FOR = 2;
43 public const OPTIMIZE_RAW_FILTER = 4;
44 public const OPTIMIZE_TEXT_NODES = 8;
45 private $loops = [];
46 private $loopsTargets = [];
47 private $optimizers;
48 /**
49 * @param int $optimizers The optimizer mode
50 */
51 public function __construct(int $optimizers = -1)
52 {
53 if ($optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_TEXT_NODES)) {
54 throw new \InvalidArgumentException(\sprintf('Optimizer mode "%s" is not valid.', $optimizers));
55 }
56 if (-1 !== $optimizers && self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $optimizers)) {
57 trigger_deprecation('twig/twig', '3.11', 'The "Twig\\NodeVisitor\\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER" option is deprecated and does nothing.');
58 }
59 $this->optimizers = $optimizers;
60 }
61 public function enterNode(Node $node, Environment $env) : Node
62 {
63 if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
64 $this->enterOptimizeFor($node);
65 }
66 return $node;
67 }
68 public function leaveNode(Node $node, Environment $env) : ?Node
69 {
70 if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
71 $this->leaveOptimizeFor($node);
72 }
73 $node = $this->optimizePrintNode($node);
74 if (self::OPTIMIZE_TEXT_NODES === (self::OPTIMIZE_TEXT_NODES & $this->optimizers)) {
75 $node = $this->mergeTextNodeCalls($node);
76 }
77 return $node;
78 }
79 private function mergeTextNodeCalls(Node $node) : Node
80 {
81 $text = '';
82 $names = [];
83 foreach ($node as $k => $n) {
84 if (!$n instanceof TextNode) {
85 return $node;
86 }
87 $text .= $n->getAttribute('data');
88 $names[] = $k;
89 }
90 if (!$text) {
91 return $node;
92 }
93 if (Node::class === \get_class($node)) {
94 return new TextNode($text, $node->getTemplateLine());
95 }
96 foreach ($names as $i => $name) {
97 if (0 === $i) {
98 $node->setNode($name, new TextNode($text, $node->getTemplateLine()));
99 } else {
100 $node->removeNode($name);
101 }
102 }
103 return $node;
104 }
105 /**
106 * Optimizes print nodes.
107 *
108 * It replaces:
109 *
110 * * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
111 */
112 private function optimizePrintNode(Node $node) : Node
113 {
114 if (!$node instanceof PrintNode) {
115 return $node;
116 }
117 $exprNode = $node->getNode('expr');
118 if ($exprNode instanceof ConstantExpression && \is_string($exprNode->getAttribute('value'))) {
119 return new TextNode($exprNode->getAttribute('value'), $exprNode->getTemplateLine());
120 }
121 if ($exprNode instanceof BlockReferenceExpression || $exprNode instanceof ParentExpression) {
122 $exprNode->setAttribute('output', \true);
123 return $exprNode;
124 }
125 return $node;
126 }
127 /**
128 * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
129 */
130 private function enterOptimizeFor(Node $node) : void
131 {
132 if ($node instanceof ForNode) {
133 // disable the loop variable by default
134 $node->setAttribute('with_loop', \false);
135 \array_unshift($this->loops, $node);
136 \array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
137 \array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
138 } elseif (!$this->loops) {
139 // we are outside a loop
140 return;
141 } elseif ($node instanceof NameExpression && 'loop' === $node->getAttribute('name')) {
142 $node->setAttribute('always_defined', \true);
143 $this->addLoopToCurrent();
144 } elseif ($node instanceof NameExpression && \in_array($node->getAttribute('name'), $this->loopsTargets)) {
145 $node->setAttribute('always_defined', \true);
146 } elseif ($node instanceof BlockReferenceNode || $node instanceof BlockReferenceExpression) {
147 $this->addLoopToCurrent();
148 } elseif ($node instanceof IncludeNode && !$node->getAttribute('only')) {
149 $this->addLoopToAll();
150 } elseif ($node instanceof FunctionExpression && 'include' === $node->getAttribute('name') && (!$node->getNode('arguments')->hasNode('with_context') || \false !== $node->getNode('arguments')->getNode('with_context')->getAttribute('value'))) {
151 $this->addLoopToAll();
152 } elseif ($node instanceof GetAttrExpression && (!$node->getNode('attribute') instanceof ConstantExpression || 'parent' === $node->getNode('attribute')->getAttribute('value')) && (\true === $this->loops[0]->getAttribute('with_loop') || $node->getNode('node') instanceof NameExpression && 'loop' === $node->getNode('node')->getAttribute('name'))) {
153 $this->addLoopToAll();
154 }
155 }
156 /**
157 * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
158 */
159 private function leaveOptimizeFor(Node $node) : void
160 {
161 if ($node instanceof ForNode) {
162 \array_shift($this->loops);
163 \array_shift($this->loopsTargets);
164 \array_shift($this->loopsTargets);
165 }
166 }
167 private function addLoopToCurrent() : void
168 {
169 $this->loops[0]->setAttribute('with_loop', \true);
170 }
171 private function addLoopToAll() : void
172 {
173 foreach ($this->loops as $loop) {
174 $loop->setAttribute('with_loop', \true);
175 }
176 }
177 public function getPriority() : int
178 {
179 return 255;
180 }
181 }
182