From 416bd7872ec7fb4df5676280414b7e41872b62d8 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 1 Oct 2010 20:49:59 +0200 Subject: [PATCH] [TwigBundle] optimized calls to helpers --- .../Bundle/TwigBundle/Node/HelperNode.php | 53 +++++++++++++++++++ .../TokenParser/HelperTokenParser.php | 15 ++---- 2 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Node/HelperNode.php diff --git a/src/Symfony/Bundle/TwigBundle/Node/HelperNode.php b/src/Symfony/Bundle/TwigBundle/Node/HelperNode.php new file mode 100644 index 000000000000..a4d8d4eb0c38 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Node/HelperNode.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * + * @author Fabien Potencier + */ +class HelperNode extends \Twig_Node +{ + public function __construct($helper, $method, \Twig_Node_Expression_Array $values, $lineno, $tag = null) + { + parent::__construct(array('values' => $values), array('helper' => $helper, 'method' => $method), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param \Twig_Compiler A Twig_Compiler instance + */ + public function compile($compiler) + { + $compiler + ->addDebugInfo($this) + ->raw("\$this->env->getExtension(") + ->string('symfony.helpers') + ->raw(")->getContainer()->get(") + ->string($this['helper']) + ->raw(")->") + ->raw($this['method']) + ->raw("(") + ; + + foreach ($this->values as $i => $value) { + $compiler->subcompile($value); + if ($i !== count($this->values) - 1) { + $compiler->raw(', '); + } + } + + $compiler->raw(")"); + } +} diff --git a/src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php b/src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php index 3aa9fa2ab79d..84581598808f 100644 --- a/src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php +++ b/src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php @@ -2,6 +2,8 @@ namespace Symfony\Bundle\TwigBundle\TokenParser; +use Symfony\Bundle\TwigBundle\Node\HelperNode; + /* * This file is part of the Symfony package. * @@ -55,16 +57,7 @@ protected function getNode(array $values, $line) { return $this->output( $this->markAsSafe( - $this->call( - $this->call( - $this->call(new \Twig_Node_Expression_ExtensionReference('symfony.helpers', 0), 'getContainer'), - 'get', - array(new \Twig_Node_Expression_Constant($this->helper, 0)) - ), - $this->method, - $this->getNodeValues($values) - ) - ) - ); + new HelperNode($this->helper, $this->method, new \Twig_Node_Expression_Array($this->getNodeValues($values), $line), $line) + )); } }