From eedce6617eea6d79e7be1eee619735e9f0e35271 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 18 Dec 2010 13:15:24 +0100 Subject: [PATCH] added an exception when a macro uses a reserved name --- CHANGELOG | 1 + lib/Twig/Parser.php | 17 +++++++++++++++-- test/Twig/Tests/ParserTest.php | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/ParserTest.php diff --git a/CHANGELOG b/CHANGELOG index f00961f..fd6450f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ Backward incompatibilities: Changes: + * added an exception when a macro uses a reserved name * the "default" filter now uses the "empty" test instead of just checking for null * added the "empty" test diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 31f0e81..a0ba8d5 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -20,6 +20,7 @@ class Twig_Parser implements Twig_ParserInterface protected $blockStack; protected $macros; protected $env; + protected $reservedMacroNames; public function __construct(Twig_Environment $env) { @@ -173,9 +174,21 @@ class Twig_Parser implements Twig_ParserInterface return isset($this->macros[$name]); } - public function setMacro($name, $value) + public function setMacro($name, Twig_Node_Macro $node) { - $this->macros[$name] = $value; + if (null === $this->reservedMacroNames) { + $this->reservedMacroNames = array(); + $r = new ReflectionClass($this->env->getBaseTemplateClass()); + foreach ($r->getMethods() as $method) { + $this->reservedMacroNames[] = $method->getName(); + } + } + + if (in_array($name, $this->reservedMacroNames)) { + throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine()); + } + + $this->macros[$name] = $node; } public function getExpressionParser() diff --git a/test/Twig/Tests/ParserTest.php b/test/Twig/Tests/ParserTest.php new file mode 100644 index 0000000..2159a5c --- /dev/null +++ b/test/Twig/Tests/ParserTest.php @@ -0,0 +1,21 @@ +setMacro('display', $this->getMock('Twig_Node_Macro', null, array(), '', null)); + } +} -- 1.7.2.5