made a speed optimization to macro calls when imported via the from tag

This commit is contained in:
Fabien Potencier
2012-01-23 09:22:43 +01:00
parent 58f7fa3574
commit e81c932e77
6 changed files with 50 additions and 2 deletions
+1
View File
@@ -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)
+4 -1
View File
@@ -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);
+37
View File
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2012 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_MethodCall extends Twig_Node_Expression
{
public function __construct(Twig_Node_Expression $node, $method, Twig_Node_Expression_Array $arguments, $lineno)
{
parent::__construct(array('node' => $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(')');
}
}
+6
View File
@@ -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());
}
+1
View File
@@ -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());
}
+1 -1
View File
@@ -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;