From 9728a5de40ca5daa81a19b73a303d06d85106b45 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 12 Oct 2012 16:47:13 +0200 Subject: [PATCH] refactored previous commit to avoid collission between symbols of different types --- lib/Twig/ExpressionParser.php | 4 ++-- lib/Twig/Parser.php | 10 +++++----- lib/Twig/TokenParser/From.php | 2 +- lib/Twig/TokenParser/Import.php | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index acfef01..7c1ce24 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -308,7 +308,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->getImportedSymbol($name, 'function')) { + if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) { $arguments = new Twig_Node_Expression_Array(array(), $line); foreach ($args as $n) { $arguments->addElement($n); @@ -380,7 +380,7 @@ class Twig_ExpressionParser $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']'); } - if ($node instanceof Twig_Node_Expression_Name && null !== $alias = $this->parser->getImportedSymbol($node->getAttribute('name'), 'template')) { + if ($node instanceof Twig_Node_Expression_Name && null !== $alias = $this->parser->getImportedSymbol('template', $node->getAttribute('name'))) { $node = new Twig_Node_Expression_MethodCall($node, 'get'.$arg->getAttribute('value'), $arguments, $lineno); $node->setAttribute('safe', true); diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 13b72a0..b2959e5 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -282,16 +282,16 @@ class Twig_Parser implements Twig_ParserInterface $this->embeddedTemplates[] = $template; } - public function addImportedSymbol($alias, $name, Twig_Node_Expression $node = null, $type) + public function addImportedSymbol($type, $alias, $name = null, Twig_Node_Expression $node = null) { - $this->importedSymbols[0][$alias] = array('name' => $name, 'node' => $node, 'type' => $type); + $this->importedSymbols[0][$type][$alias] = array('name' => $name, 'node' => $node); } - public function getImportedSymbol($alias, $type) + public function getImportedSymbol($type, $alias) { foreach ($this->importedSymbols as $functions) { - if (isset($functions[$alias]) && $type === $functions[$alias]['type']) { - return $functions[$alias]; + if (isset($functions[$type][$alias])) { + return $functions[$type][$alias]; } } } diff --git a/lib/Twig/TokenParser/From.php b/lib/Twig/TokenParser/From.php index d70b81f..a54054d 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->addImportedSymbol($alias, 'get'.$name, $node->getNode('var'), 'function'); + $this->parser->addImportedSymbol('function', $alias, 'get'.$name, $node->getNode('var')); } return $node; diff --git a/lib/Twig/TokenParser/Import.php b/lib/Twig/TokenParser/Import.php index 482a8f5..e7050c7 100644 --- a/lib/Twig/TokenParser/Import.php +++ b/lib/Twig/TokenParser/Import.php @@ -32,7 +32,7 @@ class Twig_TokenParser_Import extends Twig_TokenParser $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'); + $this->parser->addImportedSymbol('template', $var->getAttribute('name')); return new Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag()); } -- 1.7.2.5