From a27175b76ac5be5f5a642df370dfe0ed0d120d69 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 16 Jul 2011 20:37:44 +0200 Subject: [PATCH] added an optimization to the Optimizer --- CHANGELOG | 1 + lib/Twig/NodeVisitor/Optimizer.php | 17 +++++++++++++++++ test/Twig/Tests/NodeVisitor/OptimizerTest.php | 10 ++++++++++ 3 files changed, 28 insertions(+), 0 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8879084..a7b0202 100644 --- 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) diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index d89ceb7..7348734 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -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; } diff --git a/test/Twig/Tests/NodeVisitor/OptimizerTest.php b/test/Twig/Tests/NodeVisitor/OptimizerTest.php index 96ccfc7..9309165 100644 --- a/test/Twig/Tests/NodeVisitor/OptimizerTest.php +++ b/test/Twig/Tests/NodeVisitor/OptimizerTest.php @@ -10,6 +10,16 @@ */ 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 */ -- 1.7.2.5