From 3b87da344ade9a719d3e105eb7ca20864b725875 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 21 Jan 2012 21:16:34 +0100 Subject: [PATCH] made a speed optimization to macro calls when imported via the from tag --- CHANGELOG | 1 + lib/Twig/ExpressionParser.php | 2 +- lib/Twig/Node/Expression/MethodCall.php | 36 +++++++++++++++++++++++++++++++ lib/Twig/TokenParser/From.php | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lib/Twig/Node/Expression/MethodCall.php diff --git a/CHANGELOG b/CHANGELOG index 75cd2e6..5d2ab67 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.6.0-DEV + * made a speed optimization to macro calls when imported via the "from" tag * added slice notation support for the [] operator (syntactic sugar for the slice operator) * added a slice filter * added string support for the reverse filter diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index ddbdc8f..dfe7f03 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -309,7 +309,7 @@ class Twig_ExpressionParser return new Twig_Node_Expression_GetAttr($args->getNode(0), $args->getNode(1), count($args) > 2 ? $args->getNode(2) : new Twig_Node_Expression_Array(array(), $line), Twig_TemplateInterface::ANY_CALL, $line); default: if (null !== $alias = $this->parser->getImportedFunction($name)) { - return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $line), $args, Twig_TemplateInterface::METHOD_CALL, $line); + return new Twig_Node_Expression_MethodCall($alias['node'], $alias['name'], $args, $line); } $class = $this->getFunctionNodeClass($name); diff --git a/lib/Twig/Node/Expression/MethodCall.php b/lib/Twig/Node/Expression/MethodCall.php new file mode 100644 index 0000000..19d4e76 --- /dev/null +++ b/lib/Twig/Node/Expression/MethodCall.php @@ -0,0 +1,36 @@ + $node, 'arguments' => $arguments), array('method' => $method), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler + ->subcompile($this->getNode('node')) + ->raw('->') + ->raw($this->getAttribute('method')) + ->raw('(') + ; + $nodes = $this->getNode('arguments'); + for ($i = 0, $max = count($nodes); $i < $max; $i++) { + $compiler->subcompile($nodes->getNode($i)); + + if ($i < $max - 1) { + $compiler->raw(', '); + } + } + $compiler->raw(')'); + } +} diff --git a/lib/Twig/TokenParser/From.php b/lib/Twig/TokenParser/From.php index ba4d7ff..4e20f5c 100644 --- a/lib/Twig/TokenParser/From.php +++ b/lib/Twig/TokenParser/From.php @@ -56,7 +56,7 @@ class Twig_TokenParser_From extends Twig_TokenParser $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag()); foreach($targets as $name => $alias) { - $this->parser->addImportedFunction($alias, $name, $node->getNode('var')); + $this->parser->addImportedFunction($alias, 'get'.$name, $node->getNode('var')); } return $node; -- 1.7.2.5