From 57ff88255ef35ad8da325d0cb0bf140e653b132c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 6 Jan 2011 08:37:18 +0100 Subject: [PATCH] made temporary variable names predictable --- lib/Twig/Environment.php | 10 ++++++++++ lib/Twig/Node/From.php | 24 ------------------------ lib/Twig/Parser.php | 8 ++++++++ lib/Twig/TokenParser/Filter.php | 2 +- lib/Twig/TokenParser/From.php | 2 +- 5 files changed, 20 insertions(+), 26 deletions(-) delete mode 100644 lib/Twig/Node/From.php diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 48af99a..4100640 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -260,6 +260,16 @@ class Twig_Environment } /** + * Gets the template class prefix. + * + * @return string The template class prefix + */ + public function getTemplateClassPrefix() + { + return $this->templateClassPrefix; + } + + /** * Loads a template by name. * * @param string $name The template name diff --git a/lib/Twig/Node/From.php b/lib/Twig/Node/From.php deleted file mode 100644 index 224a905..0000000 --- a/lib/Twig/Node/From.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -class Twig_Node_From extends Twig_Node_Import -{ - public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) - { - parent::__construct($expr, new Twig_Node_Expression_AssignName(str_replace('.', '_', uniqid('_imported_', true)), $lineno), $lineno, $tag); - } -} diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 346e732..88d4181 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -29,6 +29,7 @@ class Twig_Parser implements Twig_ParserInterface protected $env; protected $reservedMacroNames; protected $importedFunctions; + protected $tmpVarCount; /** * Constructor. @@ -40,6 +41,11 @@ class Twig_Parser implements Twig_ParserInterface $this->env = $env; } + public function getVarName() + { + return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount); + } + /** * Converts a token stream to a node tree. * @@ -49,6 +55,8 @@ class Twig_Parser implements Twig_ParserInterface */ public function parse(Twig_TokenStream $stream) { + $this->tmpVarCount = 0; + // tag handlers $this->handlers = $this->env->getTokenParsers(); $this->handlers->setParser($this); diff --git a/lib/Twig/TokenParser/Filter.php b/lib/Twig/TokenParser/Filter.php index 2e0e31c..4ef8023 100644 --- a/lib/Twig/TokenParser/Filter.php +++ b/lib/Twig/TokenParser/Filter.php @@ -19,7 +19,7 @@ class Twig_TokenParser_Filter extends Twig_TokenParser */ public function parse(Twig_Token $token) { - $name = str_replace('.', '_', uniqid('_tmp_', true)); + $name = $this->parser->getVarName(); $ref = new Twig_Node_Expression_BlockReference(new Twig_Node_Expression_Constant($name, $token->getLine()), $token->getLine(), $this->getTag()); $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag()); diff --git a/lib/Twig/TokenParser/From.php b/lib/Twig/TokenParser/From.php index 788239b..b26a4fb 100644 --- a/lib/Twig/TokenParser/From.php +++ b/lib/Twig/TokenParser/From.php @@ -45,7 +45,7 @@ class Twig_TokenParser_From extends Twig_TokenParser $stream->expect(Twig_Token::BLOCK_END_TYPE); - $node = new Twig_Node_From($macro, $token->getLine(), $this->getTag()); + $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')); -- 1.7.2.5