added an exception when a macro uses a reserved name

This commit is contained in:
Fabien Potencier
2010-12-18 13:15:24 +01:00
parent 583718ee1a
commit eedce6617e
3 changed files with 37 additions and 2 deletions
+1
View File
@@ -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
+15 -2
View File
@@ -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()
+21
View File
@@ -0,0 +1,21 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException Twig_Error_Syntax
*/
public function testSetMacroThrowsExceptionOnReservedMethods()
{
$parser = new Twig_Parser(new Twig_Environment());
$parser->setMacro('display', $this->getMock('Twig_Node_Macro', null, array(), '', null));
}
}