mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 12:26:30 +00:00
Remove the reserved macro names
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 2.0.0 (201X-XX-XX)
|
||||
|
||||
* remove reserved macro names. All names can be used as macro
|
||||
* removed Twig_Template::getEnvironment()
|
||||
* removed _self variable (except for usage in from and import tags)
|
||||
* made the loader a required argument of Twig_Environment constructor
|
||||
@@ -22,7 +23,7 @@
|
||||
* fixed limited RCEs when in sandbox mode
|
||||
* deprecated Twig_Template::getEnvironment()
|
||||
* deprecated the _self variable for usage outside of the from and import tags
|
||||
* added Twig_BaseNodeVisitor to ease the compatibility of node visitors
|
||||
* added Twig_BaseNodeVisitor to ease the compatibility of node visitors
|
||||
between 1.x and 2.x
|
||||
|
||||
* 1.19.0 (2015-07-31)
|
||||
|
||||
@@ -389,11 +389,7 @@ class Twig_ExpressionParser
|
||||
|
||||
$name = $arg->getAttribute('value');
|
||||
|
||||
if ($this->parser->isReservedMacroName($name)) {
|
||||
throw new Twig_Error_Syntax(sprintf('"%s" cannot be called as macro as it is a reserved keyword', $name), $token->getLine(), $this->parser->getFilename());
|
||||
}
|
||||
|
||||
$node = new Twig_Node_Expression_MethodCall($node, 'get'.$name, $arguments, $lineno);
|
||||
$node = new Twig_Node_Expression_MethodCall($node, 'macro_'.$name, $arguments, $lineno);
|
||||
$node->setAttribute('safe', true);
|
||||
|
||||
return $node;
|
||||
|
||||
@@ -38,7 +38,7 @@ class Twig_Node_Macro extends Twig_Node
|
||||
{
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write(sprintf('public function get%s(', $this->getAttribute('name')))
|
||||
->write(sprintf('public function macro_%s(', $this->getAttribute('name')))
|
||||
;
|
||||
|
||||
$count = count($this->getNode('arguments'));
|
||||
|
||||
+5
-17
@@ -27,7 +27,6 @@ class Twig_Parser
|
||||
private $blockStack;
|
||||
private $macros;
|
||||
private $env;
|
||||
private $reservedMacroNames;
|
||||
private $importedSymbols;
|
||||
private $traits;
|
||||
private $embeddedTemplates = array();
|
||||
@@ -258,28 +257,17 @@ class Twig_Parser
|
||||
|
||||
public function setMacro($name, Twig_Node_Macro $node)
|
||||
{
|
||||
if ($this->isReservedMacroName($name)) {
|
||||
throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine(), $this->getFilename());
|
||||
}
|
||||
|
||||
$this->macros[$name] = $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.0. Will be removed in 3.0. There is no reserved macro names anymore
|
||||
*/
|
||||
public function isReservedMacroName($name)
|
||||
{
|
||||
if (null === $this->reservedMacroNames) {
|
||||
$this->reservedMacroNames = array();
|
||||
$r = new ReflectionClass($this->env->getBaseTemplateClass());
|
||||
foreach ($r->getMethods() as $method) {
|
||||
$methodName = strtolower($method->getName());
|
||||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.0 and will be removed in 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) {
|
||||
$this->reservedMacroNames[] = substr($methodName, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return in_array(strtolower($name), $this->reservedMacroNames);
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addTrait($trait)
|
||||
|
||||
@@ -52,11 +52,7 @@ class Twig_TokenParser_From extends Twig_TokenParser
|
||||
$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) {
|
||||
if ($this->parser->isReservedMacroName($name)) {
|
||||
throw new Twig_Error_Syntax(sprintf('"%s" cannot be an imported macro as it is a reserved keyword', $name), $token->getLine(), $stream->getFilename());
|
||||
}
|
||||
|
||||
$this->parser->addImportedSymbol('function', $alias, 'get'.$name, $node->getNode('var'));
|
||||
$this->parser->addImportedSymbol('function', $alias, 'macro_'.$name, $node->getNode('var'));
|
||||
}
|
||||
|
||||
return $node;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
--TEST--
|
||||
"from" tag with reserved name
|
||||
--TEMPLATE--
|
||||
{% from 'forms.twig' import templateName %}
|
||||
--TEMPLATE(forms.twig)--
|
||||
--DATA--
|
||||
return array()
|
||||
--EXCEPTION--
|
||||
Twig_Error_Syntax: "templateName" cannot be an imported macro as it is a reserved keyword in "index.twig" at line 2
|
||||
@@ -1,11 +0,0 @@
|
||||
--TEST--
|
||||
"from" tag with reserved name
|
||||
--TEMPLATE--
|
||||
{% import 'forms.twig' as macros %}
|
||||
|
||||
{{ macros.parent() }}
|
||||
--TEMPLATE(forms.twig)--
|
||||
--DATA--
|
||||
return array()
|
||||
--EXCEPTION--
|
||||
Twig_Error_Syntax: "parent" cannot be called as macro as it is a reserved keyword in "index.twig" at line 4
|
||||
@@ -1,10 +0,0 @@
|
||||
--TEST--
|
||||
"macro" tag with reserved name
|
||||
--TEMPLATE--
|
||||
{% macro parent(arg1, arg2) %}
|
||||
parent
|
||||
{% endmacro %}
|
||||
--DATA--
|
||||
return array()
|
||||
--EXCEPTION--
|
||||
Twig_Error_Syntax: "parent" cannot be used as a macro name as it is a reserved keyword in "index.twig" at line 2
|
||||
@@ -42,7 +42,7 @@ class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase
|
||||
return array(
|
||||
array($node, <<<EOF
|
||||
// line 1
|
||||
public function getfoo(\$__foo__ = null, \$__bar__ = "Foo"$declaration)
|
||||
public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo"$declaration)
|
||||
{
|
||||
\$context = \$this->env->mergeGlobals(array(
|
||||
"foo" => \$__foo__,
|
||||
|
||||
@@ -10,15 +10,6 @@
|
||||
*/
|
||||
class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
*/
|
||||
public function testSetMacroThrowsExceptionOnReservedMethods()
|
||||
{
|
||||
$parser = $this->getParser();
|
||||
$parser->setMacro('parent', $this->getMock('Twig_Node_Macro', array(), array(), '', null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Twig_Error_Syntax
|
||||
* @expectedExceptionMessage Unknown tag name "foo". Did you mean "for" at line 1
|
||||
|
||||
Reference in New Issue
Block a user