mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
added an exception when a macro uses a reserved name
This commit is contained in:
@@ -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
@@ -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()
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user