From: Fabien Potencier Date: Mon, 23 Jan 2012 08:22:43 +0000 (+0100) Subject: made a speed optimization to macro calls when imported via the from tag X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e81c932e779352f5716290765ac9786950b6b473;p=web%2Fkonrad%2Ftwig.git made a speed optimization to macro calls when imported via the from tag --- diff --git a/CHANGELOG b/CHANGELOG index be00af0..efb0adc 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 * fixed globals, parsers, visitors, filters, tests, and functions management in Twig_Environment when a new one or new extension is added * fixed the attribute function when passing arguments * added slice notation support for the [] operator (syntactic sugar for the slice operator) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index ca2c5f0..8cf91d8 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -314,7 +314,10 @@ class Twig_ExpressionParser $arguments->addElement($n); } - return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $line), $arguments, Twig_TemplateInterface::METHOD_CALL, $line); + $node = new Twig_Node_Expression_MethodCall($alias['node'], $alias['name'], $arguments, $line); + $node->setAttribute('safe', true); + + return $node; } $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..5093808 --- /dev/null +++ b/lib/Twig/Node/Expression/MethodCall.php @@ -0,0 +1,37 @@ + $node, 'arguments' => $arguments), array('method' => $method, 'safe' => false), $lineno); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler + ->subcompile($this->getNode('node')) + ->raw('->') + ->raw($this->getAttribute('method')) + ->raw('(') + ; + $first = true; + foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) { + if (!$first) { + $compiler->raw(', '); + } + $first = false; + + $compiler->subcompile($pair['value']); + } + $compiler->raw(')'); + } +} diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index 5961ba2..f068a16 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -73,6 +73,12 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface } else { $this->setSafe($node, array()); } + } elseif ($node instanceof Twig_Node_Expression_MethodCall) { + if ($node->getAttribute('safe')) { + $this->setSafe($node, array('all')); + } else { + $this->setSafe($node, array()); + } } else { $this->setSafe($node, array()); } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 86ca5c6..324410e 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -427,6 +427,7 @@ abstract class Twig_Template implements Twig_TemplateInterface $ret = call_user_func_array(array($object, $method), $arguments); + // hack to be removed when macro calls are refactored if ($object instanceof Twig_TemplateInterface) { return new Twig_Markup($ret, $this->env->getCharset()); } 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;