* 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
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);
$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);
}
protected $macros;
protected $env;
protected $reservedMacroNames;
- protected $importedFunctions;
+ protected $importedSymbols;
protected $traits;
protected $embeddedTemplates = array();
$this->macros = array();
$this->traits = array();
$this->blockStack = array();
- $this->importedFunctions = array(array());
+ $this->importedSymbols = array(array());
$this->embeddedTemplates = array();
try {
$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);
}
/**
$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;
$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());
}