Skip to content

Commit

Permalink
[TwigBundle] optimized calls to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 1, 2010
1 parent 3ce8ad1 commit 416bd78
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
53 changes: 53 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Node/HelperNode.php
@@ -0,0 +1,53 @@
<?php

namespace Symfony\Bundle\TwigBundle\Node;

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
*
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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(")");
}
}
15 changes: 4 additions & 11 deletions src/Symfony/Bundle/TwigBundle/TokenParser/HelperTokenParser.php
Expand Up @@ -2,6 +2,8 @@

namespace Symfony\Bundle\TwigBundle\TokenParser;

use Symfony\Bundle\TwigBundle\Node\HelperNode;

/*
* This file is part of the Symfony package.
*
Expand Down Expand Up @@ -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)
));
}
}

0 comments on commit 416bd78

Please sign in to comment.