PEEL Shopping
Open source ecommerce : PEEL Shopping
Optimizer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 2010 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 
23 {
24  const OPTIMIZE_ALL = -1;
25  const OPTIMIZE_NONE = 0;
26  const OPTIMIZE_FOR = 2;
29 
30  protected $loops = array();
31  protected $optimizers;
32  protected $prependedNodes = array();
33  protected $inABody = false;
34 
40  public function __construct($optimizers = -1)
41  {
42  if (!is_int($optimizers) || $optimizers > 2) {
43  throw new InvalidArgumentException(sprintf('Optimizer mode "%s" is not valid.', $optimizers));
44  }
45 
46  $this->optimizers = $optimizers;
47  }
48 
52  public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
53  {
54  if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
55  $this->enterOptimizeFor($node, $env);
56  }
57 
58  if (!version_compare(phpversion(), '5.4.0RC1', '>=') && self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('sandbox')) {
59  if ($this->inABody) {
60  if (!$node instanceof Twig_Node_Expression) {
61  if (get_class($node) !== 'Twig_Node') {
62  array_unshift($this->prependedNodes, array());
63  }
64  } else {
65  $node = $this->optimizeVariables($node, $env);
66  }
67  } elseif ($node instanceof Twig_Node_Body) {
68  $this->inABody = true;
69  }
70  }
71 
72  return $node;
73  }
74 
78  public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
79  {
80  $expression = $node instanceof Twig_Node_Expression;
81 
82  if (self::OPTIMIZE_FOR === (self::OPTIMIZE_FOR & $this->optimizers)) {
83  $this->leaveOptimizeFor($node, $env);
84  }
85 
86  if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) {
87  $node = $this->optimizeRawFilter($node, $env);
88  }
89 
90  $node = $this->optimizePrintNode($node, $env);
91 
92  if (self::OPTIMIZE_VAR_ACCESS === (self::OPTIMIZE_VAR_ACCESS & $this->optimizers) && !$env->isStrictVariables() && !$env->hasExtension('sandbox')) {
93  if ($node instanceof Twig_Node_Body) {
94  $this->inABody = false;
95  } elseif ($this->inABody) {
96  if (!$expression && get_class($node) !== 'Twig_Node' && $prependedNodes = array_shift($this->prependedNodes)) {
97  $nodes = array();
98  foreach (array_unique($prependedNodes) as $name) {
99  $nodes[] = new Twig_Node_SetTemp($name, $node->getLine());
100  }
101 
102  $nodes[] = $node;
103  $node = new Twig_Node($nodes);
104  }
105  }
106  }
107 
108  return $node;
109  }
110 
111  protected function optimizeVariables($node, $env)
112  {
113  if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
114  $this->prependedNodes[0][] = $node->getAttribute('name');
115 
116  return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getLine());
117  }
118 
119  return $node;
120  }
121 
132  protected function optimizePrintNode($node, $env)
133  {
134  if (!$node instanceof Twig_Node_Print) {
135  return $node;
136  }
137 
138  if (
139  $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference ||
140  $node->getNode('expr') instanceof Twig_Node_Expression_Parent
141  ) {
142  $node->getNode('expr')->setAttribute('output', true);
143 
144  return $node->getNode('expr');
145  }
146 
147  return $node;
148  }
149 
156  protected function optimizeRawFilter($node, $env)
157  {
158  if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
159  return $node->getNode('node');
160  }
161 
162  return $node;
163  }
164 
171  protected function enterOptimizeFor($node, $env)
172  {
173  if ($node instanceof Twig_Node_For) {
174  // disable the loop variable by default
175  $node->setAttribute('with_loop', false);
176  array_unshift($this->loops, $node);
177  } elseif (!$this->loops) {
178  // we are outside a loop
179  return;
180  }
181 
182  // when do we need to add the loop variable back?
183 
184  // the loop variable is referenced for the current loop
185  elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
186  $this->addLoopToCurrent();
187  }
188 
189  // block reference
190  elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
191  $this->addLoopToCurrent();
192  }
193 
194  // include without the only attribute
195  elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) {
196  $this->addLoopToAll();
197  }
198 
199  // the loop variable is referenced via an attribute
200  elseif ($node instanceof Twig_Node_Expression_GetAttr
201  && (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant
202  || 'parent' === $node->getNode('attribute')->getAttribute('value')
203  )
204  && (true === $this->loops[0]->getAttribute('with_loop')
205  || ($node->getNode('node') instanceof Twig_Node_Expression_Name
206  && 'loop' === $node->getNode('node')->getAttribute('name')
207  )
208  )
209  ) {
210  $this->addLoopToAll();
211  }
212  }
213 
220  protected function leaveOptimizeFor($node, $env)
221  {
222  if ($node instanceof Twig_Node_For) {
223  array_shift($this->loops);
224  }
225  }
226 
227  protected function addLoopToCurrent()
228  {
229  $this->loops[0]->setAttribute('with_loop', true);
230  }
231 
232  protected function addLoopToAll()
233  {
234  foreach ($this->loops as $loop) {
235  $loop->setAttribute('with_loop', true);
236  }
237  }
238 
242  public function getPriority()
243  {
244  return 255;
245  }
246 }
optimizeRawFilter($node, $env)
Removes "raw" filters.
Definition: Optimizer.php:156
optimizePrintNode($node, $env)
Optimizes print nodes.
Definition: Optimizer.php:132
enterOptimizeFor($node, $env)
Optimizes "for" tag by removing the "loop" variable creation whenever possible.
Definition: Optimizer.php:171
getPriority()
{Returns the priority for this visitor.Priority should be between -10 and 10 (0 is the default)...
Definition: Optimizer.php:242
Represents a body node.
Definition: Body.php:17
Represents an include node.
Definition: Include.php:18
Represents a node in the AST.
Definition: Node.php:18
leaveOptimizeFor($node, $env)
Optimizes "for" tag by removing the "loop" variable creation whenever possible.
Definition: Optimizer.php:220
Represents a block call node.
isStrictVariables()
Checks if the strict_variables option is enabled.
getAttribute($name)
Gets an attribute.
Definition: Node.php:138
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
Abstract class for all nodes that represents an expression.
Definition: Expression.php:18
enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{Called before child nodes are visited.The node to visit The Twig environment instanceTwig_NodeInterf...
Definition: Optimizer.php:52
leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
{Called after child nodes are visited.The node to visit The Twig environment instanceTwig_NodeInterfa...
Definition: Optimizer.php:78
Represents a for node.
Definition: For.php:18
Represents a parent node.
Definition: Parent.php:18
__construct($optimizers=-1)
Constructor.
Definition: Optimizer.php:40
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
Represents a node in the AST.
hasExtension($name)
Returns true if the given extension is registered.
Twig_NodeVisitor_Optimizer tries to optimizes the AST.
Definition: Optimizer.php:22
Stores the Twig configuration.
Definition: Environment.php:17
Represents a block call node.
optimizeVariables($node, $env)
Definition: Optimizer.php:111
getNode($name)
Gets a node by name.
Definition: Node.php:187
Represents a node that outputs an expression.
Definition: Print.php:18

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:18 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.