4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Twig_NodeVisitor_Optimizer tries to optimizes the AST.
15 * This visitor is always the last registered one.
17 * You can configure which optimizations you want to activate via the
22 * @author Fabien Potencier <fabien@symfony.com>
24 class Twig_NodeVisitor_Optimizer extends Twig_BaseNodeVisitor
26 const OPTIMIZE_ALL = -1;
27 const OPTIMIZE_NONE = 0;
28 const OPTIMIZE_FOR = 2;
29 const OPTIMIZE_RAW_FILTER = 4;
30 const OPTIMIZE_VAR_ACCESS = 8;
32 protected $loops = array();
33 protected $loopsTargets = array();
34 protected $optimizers;
35 protected $prependedNodes = array();
36 protected $inABody = false;
39 * @param int $optimizers The optimizer mode
41 public function __construct($optimizers = -1)
43 if (!is_int($optimizers) || $optimizers > (self::OPTIMIZE_FOR | self::OPTIMIZE_RAW_FILTER | self::OPTIMIZE_VAR_ACCESS)) {
44 throw new InvalidArgumentException(sprintf('Optimizer mode "%s" is not valid.', $optimizers));
47 $this->optimizers = $optimizers;
50 protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
52 if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
53 $this->enterOptimizeFor($node, $env);
56 if (PHP_VERSION_ID < 50400 && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('Twig_Extension_Sandbox')) {
58 if (!$node instanceof Twig_Node_Expression) {
59 if ('Twig_Node' !== get_class($node)) {
60 array_unshift($this->prependedNodes, array());
63 $node = $this->optimizeVariables($node, $env);
65 } elseif ($node instanceof Twig_Node_Body) {
66 $this->inABody = true;
73 protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
75 $expression = $node instanceof Twig_Node_Expression;
77 if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
78 $this->leaveOptimizeFor($node, $env);
81 if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
82 $node = $this->optimizeRawFilter($node, $env);
85 $node = $this->optimizePrintNode($node, $env);
87 if (self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('Twig_Extension_Sandbox')) {
88 if ($node instanceof Twig_Node_Body) {
89 $this->inABody = false;
90 } elseif ($this->inABody) {
91 if (!$expression && 'Twig_Node' !== get_class($node) && $prependedNodes = array_shift($this->prependedNodes)) {
93 foreach (array_unique($prependedNodes) as $name) {
94 $nodes[] = new Twig_Node_SetTemp($name, $node->getTemplateLine());
98 $node = new Twig_Node($nodes);
106 protected function optimizeVariables(Twig_NodeInterface $node, Twig_Environment $env)
108 if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
109 $this->prependedNodes[0][] = $node->getAttribute('name');
111 return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getTemplateLine());
118 * Optimizes print nodes.
122 * * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
124 * @return Twig_NodeInterface
126 protected function optimizePrintNode(Twig_NodeInterface $node, Twig_Environment $env)
128 if (!$node instanceof Twig_Node_Print) {
132 $exprNode = $node->getNode('expr');
134 $exprNode instanceof Twig_Node_Expression_BlockReference ||
135 $exprNode instanceof Twig_Node_Expression_Parent
137 $exprNode->setAttribute('output', true);
146 * Removes "raw" filters.
148 * @return Twig_NodeInterface
150 protected function optimizeRawFilter(Twig_NodeInterface $node, Twig_Environment $env)
152 if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
153 return $node->getNode('node');
160 * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
162 protected function enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
164 if ($node instanceof Twig_Node_For) {
165 // disable the loop variable by default
166 $node->setAttribute('with_loop', false);
167 array_unshift($this->loops, $node);
168 array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
169 array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
170 } elseif (!$this->loops) {
171 // we are outside a loop
175 // when do we need to add the loop variable back?
177 // the loop variable is referenced for the current loop
178 elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
179 $node->setAttribute('always_defined', true);
180 $this->addLoopToCurrent();
183 // optimize access to loop targets
184 elseif ($node instanceof Twig_Node_Expression_Name && in_array($node->getAttribute('name'), $this->loopsTargets)) {
185 $node->setAttribute('always_defined', true);
189 elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
190 $this->addLoopToCurrent();
193 // include without the only attribute
194 elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
195 $this->addLoopToAll();
198 // include function without the with_context=false parameter
199 elseif ($node instanceof Twig_Node_Expression_Function
200 && 'include' === $node->getAttribute('name')
201 && (!$node->getNode('arguments')->hasNode('with_context')
202 || false !== $node->getNode('arguments')->getNode('with_context')->getAttribute('value')
205 $this->addLoopToAll();
208 // the loop variable is referenced via an attribute
209 elseif ($node instanceof Twig_Node_Expression_GetAttr
210 && (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
211 || 'parent' === $node->getNode('attribute')->getAttribute('value')
213 && (true === $this->loops[0]->getAttribute('with_loop')
214 || ($node->getNode('node') instanceof Twig_Node_Expression_Name
215 && 'loop' === $node->getNode('node')->getAttribute('name')
219 $this->addLoopToAll();
224 * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
226 protected function leaveOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
228 if ($node instanceof Twig_Node_For) {
229 array_shift($this->loops);
230 array_shift($this->loopsTargets);
231 array_shift($this->loopsTargets);
235 protected function addLoopToCurrent()
237 $this->loops[0]->setAttribute('with_loop', true);
240 protected function addLoopToAll()
242 foreach ($this->loops as $loop) {
243 $loop->setAttribute('with_loop', true);
247 public function getPriority()
253 class_alias('Twig_NodeVisitor_Optimizer', 'Twig\NodeVisitor\OptimizerNodeVisitor', false);