Merge branch '1.x'

* 1.x:
  deprecated Twig_Node::getFilename() in favor of Twig_Node::getName()
  got rid of filename when we meant template name
This commit is contained in:
Fabien Potencier
2016-10-17 15:12:09 -07:00
17 changed files with 85 additions and 53 deletions
+2
View File
@@ -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()
-4
View File
@@ -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;
+2 -2
View File
@@ -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;
+10 -4
View File
@@ -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;
+7 -7
View File
@@ -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':
+25 -5
View File
@@ -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;
}
}
+6 -4
View File
@@ -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(', ')
+1 -1
View File
@@ -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(')')
+1 -1
View File
@@ -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(')')
+22 -16
View File
@@ -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")
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -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)
+2 -2
View File
@@ -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'));
@@ -16,6 +16,6 @@ blocks and autoescape
--DATA--
return array('br' => '<br />')
--CONFIG--
return array('autoescape' => 'filename')
return array('autoescape' => 'name')
--EXPECT--
&lt;br /&gt;
@@ -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' => '<br />')
--CONFIG--
return array('autoescape' => 'filename')
return array('autoescape' => 'name')
--EXPECT--
&lt;br /&gt;
&lt;br /&gt;
+1 -1
View File
@@ -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()