protected $blockStack;
protected $macros;
protected $env;
+ protected $reservedMacroNames;
public function __construct(Twig_Environment $env)
{
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()
--- /dev/null
+<?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));
+ }
+}