Permalink
Browse files

optimized macros when imported via the "import" tag

This commit unifies the way macros are called independently of how they
were included (via the from or import tag -- see e81c932).
  • Loading branch information...
1 parent 347bc81 commit a4e10c14edfb5fa2b2996c7a10ef5348ea8115c5 @fabpot committed Oct 12, 2012
Showing with 22 additions and 12 deletions.
  1. +1 −0 CHANGELOG
  2. +8 −1 lib/Twig/ExpressionParser.php
  3. +10 −10 lib/Twig/Parser.php
  4. +1 −1 lib/Twig/TokenParser/From.php
  5. +2 −0 lib/Twig/TokenParser/Import.php
View
@@ -1,5 +1,6 @@
* 1.10.1 (2012-XX-XX)
+ * made a speed optimization to macro calls when imported via the "import" tag
* fixed C extension compilation on Windows
* fixed a segfault in the C extension when using DateTime objects
@@ -308,7 +308,7 @@ public function getFunctionNode($name, $line)
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)) {
+ if (null !== $alias = $this->parser->getImportedSymbol($name, 'function')) {
$arguments = new Twig_Node_Expression_Array(array(), $line);
foreach ($args as $n) {
$arguments->addElement($n);
@@ -380,6 +380,13 @@ public function parseSubscriptExpression($node)
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
}
+ if ($node instanceof Twig_Node_Expression_Name && null !== $alias = $this->parser->getImportedSymbol($node->getAttribute('name'), 'template')) {
+ $node = new Twig_Node_Expression_MethodCall($node, 'get'.$arg->getAttribute('value'), $arguments, $lineno);
+ $node->setAttribute('safe', true);
+
+ return $node;
+ }
+
return new Twig_Node_Expression_GetAttr($node, $arg, $arguments, $type, $lineno);
}
View
@@ -29,7 +29,7 @@ class Twig_Parser implements Twig_ParserInterface
protected $macros;
protected $env;
protected $reservedMacroNames;
- protected $importedFunctions;
+ protected $importedSymbols;
protected $traits;
protected $embeddedTemplates = array();
@@ -88,7 +88,7 @@ public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = fals
$this->macros = array();
$this->traits = array();
$this->blockStack = array();
- $this->importedFunctions = array(array());
+ $this->importedSymbols = array(array());
$this->embeddedTemplates = array();
try {
@@ -282,33 +282,33 @@ public function embedTemplate(Twig_Node_Module $template)
$this->embeddedTemplates[] = $template;
}
- public function addImportedFunction($alias, $name, Twig_Node_Expression $node)
+ public function addImportedSymbol($alias, $name, Twig_Node_Expression $node = null, $type)
{
- $this->importedFunctions[0][$alias] = array('name' => $name, 'node' => $node);
+ $this->importedSymbols[0][$alias] = array('name' => $name, 'node' => $node, 'type' => $type);
}
- public function getImportedFunction($alias)
+ public function getImportedSymbol($alias, $type)
{
- foreach ($this->importedFunctions as $functions) {
- if (isset($functions[$alias])) {
+ foreach ($this->importedSymbols as $functions) {
+ if (isset($functions[$alias]) && $type === $functions[$alias]['type']) {
return $functions[$alias];
}
}
}
public function isMainScope()
{
- return 1 === count($this->importedFunctions);
+ return 1 === count($this->importedSymbols);
}
public function pushLocalScope()
{
- array_unshift($this->importedFunctions, array());
+ array_unshift($this->importedSymbols, array());
}
public function popLocalScope()
{
- array_shift($this->importedFunctions);
+ array_shift($this->importedSymbols);
}
/**
@@ -56,7 +56,7 @@ public function parse(Twig_Token $token)
$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, 'get'.$name, $node->getNode('var'));
+ $this->parser->addImportedSymbol($alias, 'get'.$name, $node->getNode('var'), 'function');
}
return $node;
@@ -32,6 +32,8 @@ public function parse(Twig_Token $token)
$var = new Twig_Node_Expression_AssignName($this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(), $token->getLine());
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+ $this->parser->addImportedSymbol($var->getAttribute('name'), null, null, 'template');
+
return new Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag());
}

0 comments on commit a4e10c1

Please sign in to comment.