diff --git a/CHANGELOG b/CHANGELOG
index 255595cc2..7e570dd44 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -19,6 +19,8 @@
* 1.27.0 (2016-XX-XX)
+ * deprecated Twig_Node::getFilename() in favor of Twig_Node::getName()
+ * deprecated the "filename" escaping strategy (use "name" instead)
* added Twig_Source to hold information about the original template
* deprecated Twig_Error::getTemplateFile() and Twig_Error::setTemplateFile() in favor of Twig_Error::getTemplateName() and Twig_Error::setTemplateName()
* deprecated Parser::getFilename()
diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php
index b491fab51..2e2f153d0 100644
--- a/lib/Twig/Compiler.php
+++ b/lib/Twig/Compiler.php
@@ -73,10 +73,6 @@ class Twig_Compiler
$this->sourceLine = 1;
$this->indentation = $indentation;
- if ($node instanceof Twig_Node_Module) {
- $node->setFilename($node->getAttribute('filename'));
- }
-
$node->compile($this);
return $this;
diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php
index ab9343372..f5c0f6e46 100644
--- a/lib/Twig/Environment.php
+++ b/lib/Twig/Environment.php
@@ -65,8 +65,8 @@ class Twig_Environment
* * autoescape: Whether to enable auto-escaping (default to html):
* * false: disable auto-escaping
* * html, js: set the autoescaping to one of the supported strategies
- * * filename: set the autoescaping strategy based on the template filename extension
- * * PHP callback: a PHP callback that returns an escaping strategy based on the template "filename"
+ * * name: set the autoescaping strategy based on the template name extension
+ * * PHP callback: a PHP callback that returns an escaping strategy based on the template "name"
*
* * optimizations: A flag that indicates which optimizations to apply
* (default to -1 which means that all optimizations are enabled;
diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php
index 5b63243bf..872846cb7 100644
--- a/lib/Twig/Extension/Escaper.php
+++ b/lib/Twig/Extension/Escaper.php
@@ -45,13 +45,19 @@ class Twig_Extension_Escaper extends Twig_Extension
* Sets the default strategy to use when not defined by the user.
*
* The strategy can be a valid PHP callback that takes the template
- * "filename" as an argument and returns the strategy to use.
+ * name as an argument and returns the strategy to use.
*
* @param string|false|callable $defaultStrategy An escaping strategy
*/
public function setDefaultStrategy($defaultStrategy)
{
if ('filename' === $defaultStrategy) {
+ @trigger_error('Using "filename" as the default strategy is deprecated since version 1.27. Use "name" instead.', E_USER_DEPRECATED);
+
+ $defaultStrategy = 'name';
+ }
+
+ if ('name' === $defaultStrategy) {
$defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
}
@@ -61,16 +67,16 @@ class Twig_Extension_Escaper extends Twig_Extension
/**
* Gets the default strategy to use when not defined by the user.
*
- * @param string $filename The template "filename"
+ * @param string $name The template name
*
* @return string|false The default strategy to use for the template
*/
- public function getDefaultStrategy($filename)
+ public function getDefaultStrategy($name)
{
// disable string callables to avoid calling a function named html or js,
// or any other upcoming escaping strategy
if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
- return call_user_func($this->defaultStrategy, $filename);
+ return call_user_func($this->defaultStrategy, $name);
}
return $this->defaultStrategy;
diff --git a/lib/Twig/FileExtensionEscapingStrategy.php b/lib/Twig/FileExtensionEscapingStrategy.php
index 9bda0b4f6..772139e23 100644
--- a/lib/Twig/FileExtensionEscapingStrategy.php
+++ b/lib/Twig/FileExtensionEscapingStrategy.php
@@ -13,7 +13,7 @@
* Default autoescaping strategy based on file names.
*
* This strategy sets the HTML as the default autoescaping strategy,
- * but changes it based on the filename.
+ * but changes it based on the template name.
*
* Note that there is no runtime performance impact as the
* default autoescaping strategy is set at compilation time.
@@ -25,21 +25,21 @@ class Twig_FileExtensionEscapingStrategy
/**
* Guesses the best autoescaping strategy based on the file name.
*
- * @param string $filename The template file name
+ * @param string $name The template name
*
* @return string|false The escaping strategy name to use or false to disable
*/
- public static function guess($filename)
+ public static function guess($name)
{
- if (in_array(substr($filename, -1), array('/', '\\'))) {
+ if (in_array(substr($name, -1), array('/', '\\'))) {
return 'html'; // return html for directories
}
- if ('.twig' === substr($filename, -5)) {
- $filename = substr($filename, 0, -5);
+ if ('.twig' === substr($name, -5)) {
+ $name = substr($name, 0, -5);
}
- $extension = pathinfo($filename, PATHINFO_EXTENSION);
+ $extension = pathinfo($name, PATHINFO_EXTENSION);
switch ($extension) {
case 'js':
diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php
index f4da93544..61574f227 100644
--- a/lib/Twig/Node.php
+++ b/lib/Twig/Node.php
@@ -22,7 +22,7 @@ class Twig_Node implements Countable, IteratorAggregate
protected $lineno;
protected $tag;
- private $filename;
+ private $name;
/**
* Constructor.
@@ -201,16 +201,36 @@ class Twig_Node implements Countable, IteratorAggregate
return new ArrayIterator($this->nodes);
}
- public function setFilename($filename)
+ public function setName($name)
{
- $this->filename = $filename;
+ $this->name = $name;
foreach ($this->nodes as $node) {
- $node->setFilename($filename);
+ $node->setName($name);
}
}
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * @deprecated since 1.27 (to be removed in 2.0)
+ */
+ public function setFilename($name)
+ {
+ @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use setName() instead.', E_USER_DEPRECATED);
+
+ $this->setName($name);
+ }
+
+ /**
+ * @deprecated since 1.27 (to be removed in 2.0)
+ */
public function getFilename()
{
- return $this->filename;
+ @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getName() instead.', E_USER_DEPRECATED);
+
+ return $this->name;
}
}
diff --git a/lib/Twig/Node/Embed.php b/lib/Twig/Node/Embed.php
index 7ed12fb88..51a8f4a7f 100644
--- a/lib/Twig/Node/Embed.php
+++ b/lib/Twig/Node/Embed.php
@@ -17,11 +17,13 @@
class Twig_Node_Embed extends Twig_Node_Include
{
// we don't inject the module to avoid node visitors to traverse it twice (as it will be already visited in the main module)
- public function __construct($filename, $index, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
+ public function __construct($name, $index, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
{
parent::__construct(new Twig_Node_Expression_Constant('not_used', $lineno), $variables, $only, $ignoreMissing, $lineno, $tag);
- $this->setAttribute('filename', $filename);
+ $this->setAttribute('name', $name);
+ // to be removed in 2.0, used name instead
+ $this->setAttribute('filename', $name);
$this->setAttribute('index', $index);
}
@@ -29,9 +31,9 @@ class Twig_Node_Embed extends Twig_Node_Include
{
$compiler
->write('$this->loadTemplate(')
- ->string($this->getAttribute('filename'))
+ ->string($this->getAttribute('name'))
->raw(', ')
- ->repr($this->getFilename())
+ ->repr($this->getName())
->raw(', ')
->repr($this->getLine())
->raw(', ')
diff --git a/lib/Twig/Node/Import.php b/lib/Twig/Node/Import.php
index f69a7471e..b3a7c1df5 100644
--- a/lib/Twig/Node/Import.php
+++ b/lib/Twig/Node/Import.php
@@ -37,7 +37,7 @@ class Twig_Node_Import extends Twig_Node
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
- ->repr($this->getFilename())
+ ->repr($this->getName())
->raw(', ')
->repr($this->getLine())
->raw(')')
diff --git a/lib/Twig/Node/Include.php b/lib/Twig/Node/Include.php
index bc80dc4f1..24bcfc2a5 100644
--- a/lib/Twig/Node/Include.php
+++ b/lib/Twig/Node/Include.php
@@ -64,7 +64,7 @@ class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
->write('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
- ->repr($this->getFilename())
+ ->repr($this->getName())
->raw(', ')
->repr($this->getLine())
->raw(')')
diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php
index c5d0801b4..8fad172a5 100644
--- a/lib/Twig/Node/Module.php
+++ b/lib/Twig/Node/Module.php
@@ -21,8 +21,12 @@
*/
class Twig_Node_Module extends Twig_Node
{
+ private $source;
+
public function __construct(Twig_Node $body, Twig_Node_Expression $parent = null, Twig_Node $blocks, Twig_Node $macros, Twig_Node $traits, $embeddedTemplates, Twig_Source $source)
{
+ $this->source = $source;
+
$nodes = array(
'body' => $body,
'blocks' => $blocks,
@@ -40,14 +44,16 @@ class Twig_Node_Module extends Twig_Node
// embedded templates are set as attributes so that they are only visited once by the visitors
parent::__construct($nodes, array(
- 'source' => $source->getCode(),
- 'name' => $source->getName(),
- // filename to be remove in 2.0 (use name instead)
- 'filename' => $source->getName(),
- 'path' => $source->getPath(),
+ // source to be remove in 2.0
+ 'source' => $this->source->getCode(),
+ // filename to be remove in 2.0 (use getName() instead)
+ 'filename' => $this->source->getName(),
'index' => null,
'embedded_templates' => $embeddedTemplates,
), 1);
+
+ // populate the template name of all node children
+ $this->setName($this->source->getName());
}
public function setIndex($index)
@@ -125,7 +131,7 @@ class Twig_Node_Module extends Twig_Node
->raw('$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
- ->repr($this->getAttribute('name'))
+ ->repr($this->source->getName())
->raw(', ')
->repr($parent->getLine())
->raw(')')
@@ -144,8 +150,8 @@ class Twig_Node_Module extends Twig_Node
$compiler
->write("\n\n")
// if the filename contains */, add a blank to avoid a PHP parse error
- ->write('/* '.str_replace('*/', '* /', $this->getAttribute('name'))." */\n")
- ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('name'), $this->getAttribute('index')))
+ ->write('/* '.str_replace('*/', '* /', $this->source->getName())." */\n")
+ ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->source->getName(), $this->getAttribute('index')))
->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
->write("{\n")
->indent()
@@ -170,7 +176,7 @@ class Twig_Node_Module extends Twig_Node
->write('$this->parent = $this->loadTemplate(')
->subcompile($parent)
->raw(', ')
- ->repr($this->getAttribute('name'))
+ ->repr($this->source->getName())
->raw(', ')
->repr($parent->getLine())
->raw(");\n")
@@ -187,7 +193,7 @@ class Twig_Node_Module extends Twig_Node
->write(sprintf('$_trait_%s = $this->loadTemplate(', $i))
->subcompile($node)
->raw(', ')
- ->repr($node->getFilename())
+ ->repr($node->getName())
->raw(', ')
->repr($node->getLine())
->raw(");\n")
@@ -338,7 +344,7 @@ class Twig_Node_Module extends Twig_Node
->write("public function getTemplateName()\n", "{\n")
->indent()
->write('return ')
- ->repr($this->getAttribute('name'))
+ ->repr($this->source->getName())
->raw(";\n")
->outdent()
->write("}\n\n")
@@ -414,7 +420,7 @@ class Twig_Node_Module extends Twig_Node
->write("public function getSource()\n", "{\n")
->indent()
->write('return ')
- ->string($compiler->getEnvironment()->isDebug() ? $this->getAttribute('source') : '')
+ ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
->raw(";\n")
->outdent()
->write("}\n\n")
@@ -427,11 +433,11 @@ class Twig_Node_Module extends Twig_Node
->write("public function getSourceContext()\n", "{\n")
->indent()
->write('return new Twig_Source(')
- ->string($compiler->getEnvironment()->isDebug() ? $this->getAttribute('source') : '')
+ ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
->raw(', ')
- ->string($this->getAttribute('name'))
+ ->string($this->source->getName())
->raw(', ')
- ->string($this->getAttribute('path'))
+ ->string($this->source->getPath())
->raw(");\n")
->outdent()
->write("}\n")
@@ -445,7 +451,7 @@ class Twig_Node_Module extends Twig_Node
->write(sprintf('%s = $this->loadTemplate(', $var))
->subcompile($node)
->raw(', ')
- ->repr($node->getFilename())
+ ->repr($node->getName())
->raw(', ')
->repr($node->getLine())
->raw(");\n")
diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php
index c7dec24c7..16de80246 100644
--- a/lib/Twig/NodeVisitor/Escaper.php
+++ b/lib/Twig/NodeVisitor/Escaper.php
@@ -34,7 +34,7 @@ class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_Module) {
- if ($env->hasExtension('Twig_Extension_Escaper') && $defaultStrategy = $env->getExtension('Twig_Extension_Escaper')->getDefaultStrategy($node->getAttribute('filename'))) {
+ if ($env->hasExtension('Twig_Extension_Escaper') && $defaultStrategy = $env->getExtension('Twig_Extension_Escaper')->getDefaultStrategy($node->getName())) {
$this->defaultStrategy = $defaultStrategy;
}
$this->safeVars = array();
diff --git a/lib/Twig/Profiler/NodeVisitor/Profiler.php b/lib/Twig/Profiler/NodeVisitor/Profiler.php
index 4b0baa82e..781b7909a 100644
--- a/lib/Twig/Profiler/NodeVisitor/Profiler.php
+++ b/lib/Twig/Profiler/NodeVisitor/Profiler.php
@@ -36,7 +36,7 @@ class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor
{
if ($node instanceof Twig_Node_Module) {
$varName = $this->getVarName();
- $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start'))));
+ $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('name'), $varName), $node->getNode('display_start'))));
$node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
} elseif ($node instanceof Twig_Node_Block) {
$varName = $this->getVarName();
diff --git a/lib/Twig/TokenParser/Embed.php b/lib/Twig/TokenParser/Embed.php
index 150f433be..e6332bb1f 100644
--- a/lib/Twig/TokenParser/Embed.php
+++ b/lib/Twig/TokenParser/Embed.php
@@ -48,7 +48,7 @@ class Twig_TokenParser_Embed extends Twig_TokenParser_Include
$stream->expect(Twig_Token::BLOCK_END_TYPE);
- return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
+ return new Twig_Node_Embed($module->getName(), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
}
public function decideBlockEnd(Twig_Token $token)
diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php
index 418d57cf2..ada49a03c 100644
--- a/test/Twig/Tests/ExpressionParserTest.php
+++ b/test/Twig/Tests/ExpressionParserTest.php
@@ -47,7 +47,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
public function testArrayExpression($template, $expected)
{
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
- $stream = $env->tokenize(new Twig_Source($template, 'index'));
+ $stream = $env->tokenize(new Twig_Source($template));
$parser = new Twig_Parser($env);
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
@@ -167,7 +167,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
public function testStringExpression($template, $expected)
{
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
- $stream = $env->tokenize(new Twig_Source($template, 'index'));
+ $stream = $env->tokenize(new Twig_Source($template));
$parser = new Twig_Parser($env);
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
diff --git a/test/Twig/Tests/Fixtures/autoescape/block.test b/test/Twig/Tests/Fixtures/autoescape/block.test
index 4b38b5b6e..1290973a5 100644
--- a/test/Twig/Tests/Fixtures/autoescape/block.test
+++ b/test/Twig/Tests/Fixtures/autoescape/block.test
@@ -16,6 +16,6 @@ blocks and autoescape
--DATA--
return array('br' => '
')
--CONFIG--
-return array('autoescape' => 'filename')
+return array('autoescape' => 'name')
--EXPECT--
<br />
diff --git a/test/Twig/Tests/Fixtures/autoescape/filename.test b/test/Twig/Tests/Fixtures/autoescape/name.test
similarity index 79%
rename from test/Twig/Tests/Fixtures/autoescape/filename.test
rename to test/Twig/Tests/Fixtures/autoescape/name.test
index b091ad34d..801c477ed 100644
--- a/test/Twig/Tests/Fixtures/autoescape/filename.test
+++ b/test/Twig/Tests/Fixtures/autoescape/name.test
@@ -1,5 +1,5 @@
--TEST--
-"filename" autoescape strategy
+"name" autoescape strategy
--TEMPLATE--
{{ br -}}
{{ include('index.html.twig') -}}
@@ -11,7 +11,7 @@
--DATA--
return array('br' => '
')
--CONFIG--
-return array('autoescape' => 'filename')
+return array('autoescape' => 'name')
--EXPECT--
<br />
<br />
diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php
index 2a80d586d..2e6810bda 100644
--- a/test/Twig/Tests/Node/ModuleTest.php
+++ b/test/Twig/Tests/Node/ModuleTest.php
@@ -25,7 +25,7 @@ class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase
$this->assertEquals($blocks, $node->getNode('blocks'));
$this->assertEquals($macros, $node->getNode('macros'));
$this->assertEquals($parent, $node->getNode('parent'));
- $this->assertEquals($source->getName(), $node->getAttribute('name'));
+ $this->assertEquals($source->getName(), $node->getName());
}
public function getTests()