mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 20:36:33 +00:00
optimize access to loop, key and value variables
This commit is contained in:
@@ -28,6 +28,7 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
|
|||||||
const OPTIMIZE_VAR_ACCESS = 8;
|
const OPTIMIZE_VAR_ACCESS = 8;
|
||||||
|
|
||||||
protected $loops = array();
|
protected $loops = array();
|
||||||
|
protected $loopsTargets = array();
|
||||||
protected $optimizers;
|
protected $optimizers;
|
||||||
protected $prependedNodes = array();
|
protected $prependedNodes = array();
|
||||||
protected $inABody = false;
|
protected $inABody = false;
|
||||||
@@ -174,6 +175,8 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
|
|||||||
// disable the loop variable by default
|
// disable the loop variable by default
|
||||||
$node->setAttribute('with_loop', false);
|
$node->setAttribute('with_loop', false);
|
||||||
array_unshift($this->loops, $node);
|
array_unshift($this->loops, $node);
|
||||||
|
array_unshift($this->loopsTargets, $node->getNode('value_target')->getAttribute('name'));
|
||||||
|
array_unshift($this->loopsTargets, $node->getNode('key_target')->getAttribute('name'));
|
||||||
} elseif (!$this->loops) {
|
} elseif (!$this->loops) {
|
||||||
// we are outside a loop
|
// we are outside a loop
|
||||||
return;
|
return;
|
||||||
@@ -183,9 +186,15 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
|
|||||||
|
|
||||||
// the loop variable is referenced for the current loop
|
// the loop variable is referenced for the current loop
|
||||||
elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
|
elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
|
||||||
|
$node->setAttribute('always_defined', true);
|
||||||
$this->addLoopToCurrent();
|
$this->addLoopToCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optimize access to loop targets
|
||||||
|
elseif ($node instanceof Twig_Node_Expression_Name && in_array($node->getAttribute('name'), $this->loopsTargets)) {
|
||||||
|
$node->setAttribute('always_defined', true);
|
||||||
|
}
|
||||||
|
|
||||||
// block reference
|
// block reference
|
||||||
elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
|
elseif ($node instanceof Twig_Node_BlockReference || $node instanceof Twig_Node_Expression_BlockReference) {
|
||||||
$this->addLoopToCurrent();
|
$this->addLoopToCurrent();
|
||||||
@@ -221,6 +230,8 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
|
|||||||
{
|
{
|
||||||
if ($node instanceof Twig_Node_For) {
|
if ($node instanceof Twig_Node_For) {
|
||||||
array_shift($this->loops);
|
array_shift($this->loops);
|
||||||
|
array_shift($this->loopsTargets);
|
||||||
|
array_shift($this->loopsTargets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user