deprecated Node::setTemplateName()

This commit is contained in:
Fabien Potencier
2019-04-12 12:38:34 +02:00
parent 330b524733
commit 71bc632ab2
4 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 2.8.1 (2019-XX-XX)
* n/a
* deprecated Node::setTemplateName() in favor of Node::setSourceContext()
* 2.8.0 (2019-04-16)
-1
View File
@@ -58,7 +58,6 @@ class ModuleNode extends Node
], 1);
// populate the template name of all node children
$this->setTemplateName($source->getName());
$this->setSourceContext($source);
}
+13 -3
View File
@@ -168,17 +168,25 @@ class Node implements \Countable, \IteratorAggregate
return new \ArrayIterator($this->nodes);
}
public function setTemplateName($name)
/**
* @deprecated since 2.8 (to be removed in 3.0)
*/
public function setTemplateName($name/*, $triggerDeprecation = true */)
{
$triggerDeprecation = 2 > \func_num_args() || \func_get_arg(1);
if ($triggerDeprecation) {
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use setSourceContext() instead.', E_USER_DEPRECATED);
}
$this->name = $name;
foreach ($this->nodes as $node) {
$node->setTemplateName($name);
$node->setTemplateName($name, $triggerDeprecation);
}
}
public function getTemplateName()
{
return $this->name;
return $this->sourceContext ? $this->sourceContext->getName() : null;
}
public function setSourceContext(Source $source)
@@ -187,6 +195,8 @@ class Node implements \Countable, \IteratorAggregate
foreach ($this->nodes as $node) {
$node->setSourceContext($source);
}
$this->setTemplateName($source->getName(), false);
}
public function getSourceContext()
+4 -3
View File
@@ -17,6 +17,7 @@ use Twig\Node\Expression\FunctionExpression;
use Twig\Node\IfNode;
use Twig\Node\Node;
use Twig\Test\NodeTestCase;
use Twig\Source;
use Twig\TwigFunction;
class Twig_Tests_Node_DeprecatedTest extends NodeTestCase
@@ -35,7 +36,7 @@ class Twig_Tests_Node_DeprecatedTest extends NodeTestCase
$expr = new ConstantExpression('This section is deprecated', 1);
$node = new DeprecatedNode($expr, 1, 'deprecated');
$node->setTemplateName('foo.twig');
$node->setSourceContext(new Source('', 'foo.twig'));
$tests[] = [$node, <<<EOF
// line 1
@@ -48,7 +49,7 @@ EOF
new DeprecatedNode($expr, 2, 'deprecated'),
], [], 1);
$node = new IfNode($t, null, 1);
$node->setTemplateName('foo.twig');
$node->setSourceContext(new Source('', 'foo.twig'));
$tests[] = [$node, <<<EOF
// line 1
@@ -64,7 +65,7 @@ EOF
$expr = new FunctionExpression('foo', new Node(), 1);
$node = new DeprecatedNode($expr, 1, 'deprecated');
$node->setTemplateName('foo.twig');
$node->setSourceContext(new Source('', 'foo.twig'));
$compiler = $this->getCompiler($environment);
$varName = $compiler->getVarName();