Example #1
0
 /**
  * Optimizes "for" tag by removing the "loop" variable creation whenever possible.
  *
  * @param IfwPsn_Vendor_Twig_NodeInterface $node A Node
  * @param IfwPsn_Vendor_Twig_Environment   $env  The current Twig environment
  */
 protected function enterOptimizeFor(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Environment $env)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_For) {
         // disable the loop variable by default
         $node->setAttribute('with_loop', false);
         array_unshift($this->loops, $node);
     } elseif (!$this->loops) {
         // we are outside a loop
         return;
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) {
         $this->addLoopToCurrent();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_BlockReference || $node instanceof IfwPsn_Vendor_Twig_Node_Expression_BlockReference) {
         $this->addLoopToCurrent();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Include && !$node->getAttribute('only')) {
         $this->addLoopToAll();
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_GetAttr && (!$node->getNode('attribute') instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant || 'parent' === $node->getNode('attribute')->getAttribute('value')) && (true === $this->loops[0]->getAttribute('with_loop') || $node->getNode('node') instanceof IfwPsn_Vendor_Twig_Node_Expression_Name && 'loop' === $node->getNode('node')->getAttribute('name'))) {
         $this->addLoopToAll();
     }
 }