ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_NodeVisitor_Optimizer Class Reference

Twig_NodeVisitor_Optimizer tries to optimizes the AST. More...

+ Inheritance diagram for Twig_NodeVisitor_Optimizer:
+ Collaboration diagram for Twig_NodeVisitor_Optimizer:

Public Member Functions

 __construct ($optimizers=-1)
 
 getPriority ()
 Returns the priority for this visitor. More...
 
- Public Member Functions inherited from Twig_BaseNodeVisitor
 enterNode (Twig_NodeInterface $node, Twig_Environment $env)
 Called before child nodes are visited. More...
 
 leaveNode (Twig_NodeInterface $node, Twig_Environment $env)
 Called after child nodes are visited. More...
 

Data Fields

const OPTIMIZE_ALL = -1
 
const OPTIMIZE_NONE = 0
 
const OPTIMIZE_FOR = 2
 
const OPTIMIZE_RAW_FILTER = 4
 
const OPTIMIZE_VAR_ACCESS = 8
 

Protected Member Functions

 doEnterNode (Twig_Node $node, Twig_Environment $env)
 
 doLeaveNode (Twig_Node $node, Twig_Environment $env)
 
 optimizeVariables (Twig_NodeInterface $node, Twig_Environment $env)
 
 optimizePrintNode (Twig_NodeInterface $node, Twig_Environment $env)
 Optimizes print nodes. More...
 
 optimizeRawFilter (Twig_NodeInterface $node, Twig_Environment $env)
 Removes "raw" filters. More...
 
 enterOptimizeFor (Twig_NodeInterface $node, Twig_Environment $env)
 Optimizes "for" tag by removing the "loop" variable creation whenever possible. More...
 
 leaveOptimizeFor (Twig_NodeInterface $node, Twig_Environment $env)
 Optimizes "for" tag by removing the "loop" variable creation whenever possible. More...
 
 addLoopToCurrent ()
 
 addLoopToAll ()
 
- Protected Member Functions inherited from Twig_BaseNodeVisitor
 doEnterNode (Twig_Node $node, Twig_Environment $env)
 Called before child nodes are visited. More...
 
 doLeaveNode (Twig_Node $node, Twig_Environment $env)
 Called after child nodes are visited. More...
 

Protected Attributes

 $loops = array()
 
 $loopsTargets = array()
 
 $optimizers
 
 $prependedNodes = array()
 
 $inABody = false
 

Detailed Description

Twig_NodeVisitor_Optimizer tries to optimizes the AST.

This visitor is always the last registered one.

You can configure which optimizations you want to activate via the optimizer mode.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 24 of file Optimizer.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_NodeVisitor_Optimizer::__construct (   $optimizers = -1)
Parameters
int$optimizersThe optimizer mode

Definition at line 41 of file Optimizer.php.

References $optimizers.

42  {
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));
45  }
46 
47  $this->optimizers = $optimizers;
48  }

Member Function Documentation

◆ addLoopToAll()

Twig_NodeVisitor_Optimizer::addLoopToAll ( )
protected

Definition at line 240 of file Optimizer.php.

Referenced by enterOptimizeFor().

241  {
242  foreach ($this->loops as $loop) {
243  $loop->setAttribute('with_loop', true);
244  }
245  }
+ Here is the caller graph for this function:

◆ addLoopToCurrent()

Twig_NodeVisitor_Optimizer::addLoopToCurrent ( )
protected

Definition at line 235 of file Optimizer.php.

Referenced by enterOptimizeFor().

236  {
237  $this->loops[0]->setAttribute('with_loop', true);
238  }
+ Here is the caller graph for this function:

◆ doEnterNode()

Twig_NodeVisitor_Optimizer::doEnterNode ( Twig_Node  $node,
Twig_Environment  $env 
)
protected

Definition at line 50 of file Optimizer.php.

References enterOptimizeFor(), Twig_Environment\hasExtension(), Twig_Environment\isStrictVariables(), and optimizeVariables().

51  {
52  if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
53  $this->enterOptimizeFor($node, $env);
54  }
55 
56  if (PHP_VERSION_ID < 50400 && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('Twig_Extension_Sandbox')) {
57  if ($this->inABody) {
58  if (!$node instanceof Twig_Node_Expression) {
59  if ('Twig_Node' !== get_class($node)) {
60  array_unshift($this->prependedNodes, array());
61  }
62  } else {
63  $node = $this->optimizeVariables($node, $env);
64  }
65  } elseif ($node instanceof Twig_Node_Body) {
66  $this->inABody = true;
67  }
68  }
69 
70  return $node;
71  }
optimizeVariables(Twig_NodeInterface $node, Twig_Environment $env)
Definition: Optimizer.php:106
Abstract class for all nodes that represents an expression.
Definition: Expression.php:18
Represents a body node.
Definition: Body.php:17
enterOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
Optimizes "for" tag by removing the "loop" variable creation whenever possible.
Definition: Optimizer.php:162
hasExtension($class)
Returns true if the given extension is registered.
isStrictVariables()
Checks if the strict_variables option is enabled.
+ Here is the call graph for this function:

◆ doLeaveNode()

Twig_NodeVisitor_Optimizer::doLeaveNode ( Twig_Node  $node,
Twig_Environment  $env 
)
protected

Definition at line 73 of file Optimizer.php.

References $name, $nodes, $prependedNodes, Twig_Node\getTemplateLine(), Twig_Environment\hasExtension(), Twig_Environment\isStrictVariables(), leaveOptimizeFor(), optimizePrintNode(), and optimizeRawFilter().

74  {
75  $expression = $node instanceof Twig_Node_Expression;
76 
77  if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
78  $this->leaveOptimizeFor($node, $env);
79  }
80 
81  if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
82  $node = $this->optimizeRawFilter($node, $env);
83  }
84 
85  $node = $this->optimizePrintNode($node, $env);
86 
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)) {
92  $nodes = array();
93  foreach (array_unique($prependedNodes) as $name) {
94  $nodes[] = new Twig_Node_SetTemp($name, $node->getTemplateLine());
95  }
96 
97  $nodes[] = $node;
98  $node = new Twig_Node($nodes);
99  }
100  }
101  }
102 
103  return $node;
104  }
optimizeRawFilter(Twig_NodeInterface $node, Twig_Environment $env)
Removes "raw" filters.
Definition: Optimizer.php:150
Represents a node in the AST.
Definition: Node.php:18
Abstract class for all nodes that represents an expression.
Definition: Expression.php:18
optimizePrintNode(Twig_NodeInterface $node, Twig_Environment $env)
Optimizes print nodes.
Definition: Optimizer.php:126
Represents a body node.
Definition: Body.php:17
getTemplateLine()
Definition: Node.php:121
hasExtension($class)
Returns true if the given extension is registered.
leaveOptimizeFor(Twig_NodeInterface $node, Twig_Environment $env)
Optimizes "for" tag by removing the "loop" variable creation whenever possible.
Definition: Optimizer.php:226
isStrictVariables()
Checks if the strict_variables option is enabled.
+ Here is the call graph for this function:

◆ enterOptimizeFor()

Twig_NodeVisitor_Optimizer::enterOptimizeFor ( Twig_NodeInterface  $node,
Twig_Environment  $env 
)
protected

Optimizes "for" tag by removing the "loop" variable creation whenever possible.

Definition at line 162 of file Optimizer.php.

References $loopsTargets, addLoopToAll(), and addLoopToCurrent().

Referenced by doEnterNode().

163  {
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
172  return;
173  }
174 
175  // when do we need to add the loop variable back?
176 
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();
181  }
182 
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);
186  }
187 
188  // block reference
189  elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
190  $this->addLoopToCurrent();
191  }
192 
193  // include without the only attribute
194  elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
195  $this->addLoopToAll();
196  }
197 
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')
203  )
204  ) {
205  $this->addLoopToAll();
206  }
207 
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')
212  )
213  && (true === $this->loops[0]->getAttribute('with_loop')
214  || ($node->getNode('node') instanceof Twig_Node_Expression_Name
215  && 'loop' === $node->getNode('node')->getAttribute('name')
216  )
217  )
218  ) {
219  $this->addLoopToAll();
220  }
221  }
Represents an include node.
Definition: Include.php:18
Represents a block call node.
Represents a block call node.
Represents a for node.
Definition: For.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPriority()

Twig_NodeVisitor_Optimizer::getPriority ( )

Returns the priority for this visitor.

Priority should be between -10 and 10 (0 is the default).

Returns
int The priority level

Implements Twig_NodeVisitorInterface.

Definition at line 247 of file Optimizer.php.

248  {
249  return 255;
250  }

◆ leaveOptimizeFor()

Twig_NodeVisitor_Optimizer::leaveOptimizeFor ( Twig_NodeInterface  $node,
Twig_Environment  $env 
)
protected

Optimizes "for" tag by removing the "loop" variable creation whenever possible.

Definition at line 226 of file Optimizer.php.

Referenced by doLeaveNode().

227  {
228  if ($node instanceof Twig_Node_For) {
229  array_shift($this->loops);
230  array_shift($this->loopsTargets);
231  array_shift($this->loopsTargets);
232  }
233  }
Represents a for node.
Definition: For.php:18
+ Here is the caller graph for this function:

◆ optimizePrintNode()

Twig_NodeVisitor_Optimizer::optimizePrintNode ( Twig_NodeInterface  $node,
Twig_Environment  $env 
)
protected

Optimizes print nodes.

It replaces:

  • "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
Returns
Twig_NodeInterface

Definition at line 126 of file Optimizer.php.

Referenced by doLeaveNode().

127  {
128  if (!$node instanceof Twig_Node_Print) {
129  return $node;
130  }
131 
132  $exprNode = $node->getNode('expr');
133  if (
134  $exprNode instanceof Twig_Node_Expression_BlockReference ||
135  $exprNode instanceof Twig_Node_Expression_Parent
136  ) {
137  $exprNode->setAttribute('output', true);
138 
139  return $exprNode;
140  }
141 
142  return $node;
143  }
Represents a node that outputs an expression.
Definition: Print.php:18
Represents a parent node.
Definition: Parent.php:18
Represents a block call node.
+ Here is the caller graph for this function:

◆ optimizeRawFilter()

Twig_NodeVisitor_Optimizer::optimizeRawFilter ( Twig_NodeInterface  $node,
Twig_Environment  $env 
)
protected

Removes "raw" filters.

Returns
Twig_NodeInterface

Definition at line 150 of file Optimizer.php.

Referenced by doLeaveNode().

151  {
152  if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
153  return $node->getNode('node');
154  }
155 
156  return $node;
157  }
+ Here is the caller graph for this function:

◆ optimizeVariables()

Twig_NodeVisitor_Optimizer::optimizeVariables ( Twig_NodeInterface  $node,
Twig_Environment  $env 
)
protected

Definition at line 106 of file Optimizer.php.

Referenced by doEnterNode().

107  {
108  if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
109  $this->prependedNodes[0][] = $node->getAttribute('name');
110 
111  return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getTemplateLine());
112  }
113 
114  return $node;
115  }
+ Here is the caller graph for this function:

Field Documentation

◆ $inABody

Twig_NodeVisitor_Optimizer::$inABody = false
protected

Definition at line 36 of file Optimizer.php.

◆ $loops

Twig_NodeVisitor_Optimizer::$loops = array()
protected

Definition at line 32 of file Optimizer.php.

◆ $loopsTargets

Twig_NodeVisitor_Optimizer::$loopsTargets = array()
protected

Definition at line 33 of file Optimizer.php.

Referenced by enterOptimizeFor().

◆ $optimizers

Twig_NodeVisitor_Optimizer::$optimizers
protected

Definition at line 34 of file Optimizer.php.

Referenced by __construct().

◆ $prependedNodes

Twig_NodeVisitor_Optimizer::$prependedNodes = array()
protected

Definition at line 35 of file Optimizer.php.

Referenced by doLeaveNode().

◆ OPTIMIZE_ALL

const Twig_NodeVisitor_Optimizer::OPTIMIZE_ALL = -1

Definition at line 26 of file Optimizer.php.

◆ OPTIMIZE_FOR

const Twig_NodeVisitor_Optimizer::OPTIMIZE_FOR = 2

Definition at line 28 of file Optimizer.php.

◆ OPTIMIZE_NONE

const Twig_NodeVisitor_Optimizer::OPTIMIZE_NONE = 0

Definition at line 27 of file Optimizer.php.

◆ OPTIMIZE_RAW_FILTER

const Twig_NodeVisitor_Optimizer::OPTIMIZE_RAW_FILTER = 4

Definition at line 29 of file Optimizer.php.

◆ OPTIMIZE_VAR_ACCESS

const Twig_NodeVisitor_Optimizer::OPTIMIZE_VAR_ACCESS = 8

Definition at line 30 of file Optimizer.php.


The documentation for this class was generated from the following file: