added an optimization to the Optimizer
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 16 Jul 2011 18:37:44 +0000 (20:37 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 16 Jul 2011 18:38:00 +0000 (20:38 +0200)
CHANGELOG
lib/Twig/NodeVisitor/Optimizer.php
test/Twig/Tests/NodeVisitor/OptimizerTest.php

index 8879084..a7b0202 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,7 @@
 
 Changes:
 
+ * added a performance optimization in the Optimizer (also helps to lower the number of nested level calls)
  * made some performance improvement for some edge cases
 
 * 1.1.0 (2011-06-28)
index d89ceb7..7348734 100644 (file)
@@ -69,6 +69,23 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
             $node = $this->optimizeRawFilter($node, $env);
         }
 
+        $node = $this->optimizeRenderBlock($node, $env);
+
+        return $node;
+    }
+
+    /**
+     * Replaces "echo $this->renderBlock()" with "$this->displayBlock()".
+     *
+     * @param Twig_NodeInterface $node A Node
+     * @param Twig_Environment   $env  The current Twig environment
+     */
+    protected function optimizeRenderBlock($node, $env)
+    {
+        if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) {
+            return new Twig_Node_BlockReference($node->getNode('expr')->getNode('name')->getAttribute('value'), $node->getLine(), $node->getNodeTag());
+        }
+
         return $node;
     }
 
index 96ccfc7..9309165 100644 (file)
  */
 class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase
 {
+    public function testRenderBlockOptimizer()
+    {
+        $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
+        $env->addExtension(new Twig_Extension_Optimizer());
+
+        $stream = $env->parse($env->tokenize('{{ block("foo") }}', 'index'));
+
+        $this->assertInstanceOf('Twig_Node_BlockReference', $stream->getNode('body'));
+    }
+
     /**
      * @dataProvider getTestsForForOptimizer
      */