8000 ensure compatibility with Twig 3.15 · symfony/symfony@f7a94f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7a94f0

Browse files
committed
ensure compatibility with Twig 3.15
1 parent 706d862 commit f7a94f0

12 files changed

+68
-35
lines changed

src/Symfony/Bridge/Twig/Node/DumpNode.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Twig\Attribute\FirstClassTwigCallableReady;
1515
use Twig\Attribute\YieldReady;
1616
use Twig\Compiler;
17+
use Twig\Node\Expression\Variable\LocalVariable;
1718
use Twig\Node\Node;
1819

1920
/**
@@ -22,10 +23,20 @@
2223
#[YieldReady]
2324
final class DumpNode extends Node
2425
{
26+
/**
27+
* @var LocalVariable|string
28+
*/
2529
private $varPrefix;
2630

27-
public function __construct(string $varPrefix, ?Node $values, int $lineno, ?string $tag = null)
31+
/**
32+
* @param LocalVariable|string $varPrefix
33+
*/
34+
public function __construct($varPrefix, ?Node $values, int $lineno, ?string $tag = null)
2835
{
36+
if (!\is_string($varPrefix) && !$varPrefix instanceof LocalVariable) {
37+
throw new \TypeError(sprintf('Expected a string or an instance of "%s", but got "%s".', LocalVariable::class, get_debug_type($varPrefix)));
38+
}
39+
2940
$nodes = [];
3041
if (null !== $values) {
3142
$nodes['values'] = $values;
@@ -42,25 +53,31 @@ public function __construct(string $varPrefix, ?Node $values, int $lineno, ?stri
4253

4354
public function compile(Compiler $compiler): void
4455
{
56+
if ($this->varPrefix instanceof LocalVariable) {
57+
$varPrefix = $this->varPrefix->getAttribute('name');
58+
} else {
59+
$varPrefix = $this->varPrefix;
60+
}
61+
4562
$compiler
4663
->write("if (\$this->env->isDebug()) {\n")
4764
->indent();
4865

4966
if (!$this->hasNode('values')) {
5067
// remove embedded templates (macros) from the context
5168
$compiler
52-
->write(sprintf('$%svars = [];'."\n", $this->varPrefix))
53-
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $this->varPrefix))
69+
->write(sprintf('$%svars = [];'."\n", $varPrefix))
70+
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $varPrefix))
5471
->indent()
55-
->write(sprintf('if (!$%sval instanceof \Twig\Template) {'."\n", $this->varPrefix))
72+
->write(sprintf('if (!$%sval instanceof \Twig\Template) {'."\n", $varPrefix))
5673
->indent()
57-
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $this->varPrefix))
74+
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $varPrefix))
5875
->outdent()
5976
->write("}\n")
6077
->outdent()
6178
->write("}\n")
6279
->addDebugInfo($this)
63-
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
80+
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $varPrefix));
6481
} elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
6582
$compiler
6683
->addDebugInfo($this)

src/Symfony/Bridge/Twig/Node/StopwatchNode.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Twig\Attribute\YieldReady;
1616
use Twig\Compiler;
1717
use Twig\Node\Expression\AssignNameExpression;
18+
use Twig\Node\Expression\Variable\LocalVariable;
1819
use Twig\Node\Node;
1920

2021
/**
@@ -25,8 +26,12 @@
2526
#[YieldReady]
2627
final class StopwatchNode extends Node
2728
{
28-
public function __construct(Node $name, Node $body, AssignNameExpression $var, int $lineno = 0, ?string $tag = null)
29+
public function __construct(Node $name, Node $body, $var, int $lineno = 0, ?string $tag = null)
2930
{
31+
if (!$var instanceof AssignNameExpression && !$var instanceof LocalVariable) {
32+
throw new \TypeError(sprintf('Expected an instance of "%s" or "%s", but got "%s".', AssignNameExpression::class, LocalVariable::class, get_debug_type($var)));
33+
}
34+
3035
if (class_exists(FirstClassTwigCallableReady::class)) {
3136
parent::__construct(['body' => $body, 'name' => $name, 'var' => $var], [], $lineno);
3237
} else {

src/Symfony/Bridge/Twig/Node/TransNode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Twig\Node\Expression\ArrayExpression;
1919
use Twig\Node\Expression\ConstantExpression;
2020
use Twig\Node\Expression\NameExpression;
21+
use Twig\Node\Expression\Variable\ContextVariable;
2122
use Twig\Node\Node;
2223
use Twig\Node\TextNode;
2324

@@ -126,7 +127,7 @@ private function compileString(Node $body, ArrayExpression $vars, bool $ignoreSt
126127
if ('count' === $var && $this->hasNode('count')) {
127128
$vars->addElement($this->getNode('count'), $key);
128129
} else {
129-
$varExpr = new NameExpression($var, $body->getTemplateLine());
130+
$varExpr = class_exists(ContextVariable::class) ? new ContextVariable($var, $body->getTemplateLine()) : new NameExpression($var, $body->getTemplateLine());
130131
$varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
131132
$vars->addElement($varExpr, $key);
132133
}

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Twig\Node\Expression\ConstantExpression;
2121
use Twig\Node\Expression\FilterExpression;
2222
use Twig\Node\Expression\NameExpression;
23+
use Twig\Node\Expression\Variable\AssignContextVariable;
24+
use Twig\Node\Expression\Variable\ContextVariable;
2325
use Twig\Node\ModuleNode;
2426
use Twig\Node\Node;
2527
use Twig\Node\Nodes;
@@ -51,8 +53,8 @@ public function enterNode(Node $node, Environment $env): Node
5153
return $node;
5254
} else {
5355
$var = $this->getVarName();
54-
$name = new AssignNameExpression($var, $node->getTemplateLine());
55-
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));
56+
$name = class_exists(AssignContextVariable::class) ? new AssignContextVariable($var, $node->getTemplateLine()) : new AssignNameExpression($var, $node->getTemplateLine());
57+
$this->scope->set('domain', class_exists(ContextVariable::class) ? new ContextVariable($var, $node->getTemplateLine()) : new NameExpression($var, $node->getTemplateLine()));
5658

5759
if (class_exists(Nodes::class)) {
5860
return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine());

src/Symfony/Bridge/Twig/Tests/Node/DumpNodeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Twig\Environment;
1818
use Twig\Loader\LoaderInterface;
1919
use Twig\Node\Expression\NameExpression;
20+
use Twig\Node\Expression\Variable\ContextVariable;
2021
use Twig\Node\Node;
2122
use Twig\Node\Nodes;
2223

@@ -74,7 +75,7 @@ public function testOneVar()
7475
{
7576
if (class_exists(Nodes::class)) {
7677
$vars = new Nodes([
77-
new NameExpression('foo', 7),
78+
new ContextVariable('foo', 7),
7879
]);
7980
} else {
8081
$vars = new Node([
@@ -104,8 +105,8 @@ public function testMultiVars()
104105
{
105106
if (class_exists(Nodes::class)) {
106107
$vars = new Nodes([
107-
new NameExpression('foo', 7),
108-
new NameExpression('bar', 7),
108+
new ContextVariable('foo', 7),
109+
new ContextVariable('bar', 7),
109110
]);
110111
} else {
111112
$vars = new Node([

src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Twig\Node\Expression\ArrayExpression;
2323
use Twig\Node\Expression\ConstantExpression;
2424
use Twig\Node\Expression\NameExpression;
25+
use Twig\Node\Expression\Variable\ContextVariable;
2526
use Twig\Node\Node;
2627
use Twig\Node\Nodes;
2728

@@ -31,7 +32,7 @@ class FormThemeTest extends TestCase
3132

3233
public function testConstructor()
3334
{
34-
$form = new NameExpression('form', 0);
35+
$form = class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0);
3536
if (class_exists(Nodes::class)) {
3637
$resources = new Nodes([
3738
new ConstantExpression('tpl1', 0),
@@ -53,7 +54,7 @@ public function testConstructor()
5354

5455
public function testCompile()
5556
{
56-
$form = new NameExpression('form', 0);
57+
$form = class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0);
5758
$resources = new ArrayExpression([
5859
new ConstantExpression(1, 0),
5960
new ConstantExpression('tpl1', 0),

src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Twig\Node\Expression\ConditionalExpression;
2323
use Twig\Node\Expression\ConstantExpression;
2424
use Twig\Node\Expression\NameExpression;
25+
use Twig\Node\Expression\Variable\ContextVariable;
2526
use Twig\Node\Node;
2627
use Twig\Node\Nodes;
2728
use Twig\TwigFunction;
@@ -32,7 +33,7 @@ public function testCompileWidget()
3233
{
3334
if (class_exists(Nodes::class)) {
3435
$arguments = new Nodes([
35-
new NameExpression('form', 0),
36+
new ContextVariable('form', 0),
3637
]);
3738
} else {
3839
$arguments = new Node([
@@ -61,7 +62,7 @@ public function testCompileWidgetWithVariables()
6162
{
6263
if (class_exists(Nodes::class)) {
6364
$arguments = new Nodes([
64-
new NameExpression('form', 0),
65+
new ContextVariable('form', 0),
6566
new ArrayExpression([
6667
new ConstantExpression('foo', 0),
6768
new ConstantExpression('bar', 0),
@@ -98,7 +99,7 @@ public function testCompileLabelWithLabel()
9899
{
99100
if (class_exists(Nodes::class)) {
100101
$arguments = new Nodes([
101-
new NameExpression('form', 0),
102+
new ContextVariable('form', 0),
102103
new ConstantExpression('my label', 0),
103104
]);
104105
} else {
@@ -129,7 +130,7 @@ public function testCompileLabelWithNullLabel()
129130
{
130131
if (class_exists(Nodes::class)) {
131132
$arguments = new Nodes([
132-
new NameExpression('form', 0),
133+
new ContextVariable('form', 0),
133134
new ConstantExpression(null, 0),
134135
]);
135136
} else {
@@ -162,7 +163,7 @@ public function testCompileLabelWithEmptyStringLabel()
162163
{
163164
if (class_exists(Nodes::class)) {
164165
$arguments = new Nodes([
165-
new NameExpression('form', 0),
166+
new ContextVariable('form', 0),
166167
new ConstantExpression('', 0),
167168
]);
168169
} else {
@@ -195,7 +196,7 @@ public function testCompileLabelWithDefaultLabel()
195196
{
196197
if (class_exists(Nodes::class)) {
197198
$arguments = new Nodes([
198-
new NameExpression('form', 0),
199+
new ContextVariable('form', 0),
199200
]);
200201
} else {
201202
$arguments = new Node([
@@ -224,7 +225,7 @@ public function testCompileLabelWithAttributes()
224225
{
225226
if (class_exists(Nodes::class)) {
226227
$arguments = new Nodes([
227-
new NameExpression('form', 0),
228+
new ContextVariable('form', 0),
228229
new ConstantExpression(null, 0),
229230
new ArrayExpression([
230231
new ConstantExpression('foo', 0),
@@ -266,7 +267,7 @@ public function testCompileLabelWithLabelAndAttributes()
266267
{
267268
if (class_exists(Nodes::class)) {
268269
$arguments = new Nodes([
269-
new NameExpression('form', 0),
270+
new ContextVariable('form', 0),
270271
new ConstantExpression('value in argument', 0),
271272
new ArrayExpression([
272273
new ConstantExpression('foo', 0),
@@ -309,7 +310,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
309310
{
310311
if (class_exists(Nodes::class)) {
311312
$arguments = new Nodes([
312-
new NameExpression('form', 0),
313+
new ContextVariable('form', 0),
313314
new ConditionalExpression(
314315
// if
315316
new ConstantExpression(true, 0),
@@ -360,7 +361,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
360361
{
361362
if (class_exists(Nodes::class)) {
362363
$arguments = new Nodes([
363-
new NameExpression('form', 0),
364+
new ContextVariable('form', 0),
364365
new ConditionalExpression(
365366
// if
366367
new ConstantExpression(true, 0),

src/Symfony/Bridge/Twig/Tests/Node/TransNodeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Twig\Environment;
1919
use Twig\Loader\LoaderInterface;
2020
use Twig\Node\Expression\NameExpression;
21+
use Twig\Node\Expression\Variable\ContextVariable;
2122
use Twig\Node\TextNode;
2223

2324
/**
@@ -28,7 +29,7 @@ class TransNodeTest extends TestCase
2829
public function testCompileStrict()
2930
{
3031
$body = new TextNode('trans %var%', 0);
31-
$vars = new NameExpression('foo', 0);
32+
$vars = class_exists(ContextVariable::class) ? new ContextVariable('foo', 0) : new NameExpression('foo', 0);
3233
$node = new TransNode($body, null, null, $vars);
3334

3435
$env = new Environment($this->createMock(LoaderInterface::class), ['strict_variables' => true]);

src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Twig\Node\Expression\ConstantExpression;
2121
use Twig\Node\Expression\FilterExpression;
2222
use Twig\Node\Expression\NameExpression;
23+
use Twig\Node\Expression\Variable\ContextVariable;
2324
use Twig\Node\Node;
2425
use Twig\Node\Nodes;
2526
use Twig\TwigFilter;
@@ -44,7 +45,7 @@ public function testMessageExtractionWithInvalidDomainNode()
4445
if (class_exists(Nodes::class)) {
4546
$n = new Nodes([
4647
new ArrayExpression([], 0),
47-
new NameExpression('variable', 0),
48+
new ContextVariable('variable', 0),
4849
]);
4950
} else {
5051
$n = new Node([

src/Symfony/Bridge/Twig/Tests/TokenParser/FormThemeTokenParserTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Twig\Node\Expression\ArrayExpression;
2121
use Twig\Node\Expression\ConstantExpression;
2222
use Twig\Node\Expression\NameExpression;
23+
use Twig\Node\Expression\Variable\ContextVariable;
2324
use Twig\Parser;
2425
use Twig\Source;
2526

@@ -51,7 +52,7 @@ public static function getTestsForFormTheme()
5152
[
5253
'{% form_theme form "tpl1" %}',
5354
new FormThemeNode(
54-
new NameExpression('form', 1),
55+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
5556
new ArrayExpression([
5657
new ConstantExpression(0, 1),
5758
new ConstantExpression('tpl1', 1),
@@ -63,7 +64,7 @@ public static function getTestsForFormTheme()
6364
[
6465
'{% form_theme form "tpl1" "tpl2" %}',
6566
new FormThemeNode(
66-
new NameExpression('form', 1),
67+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
6768
new ArrayExpression([
6869
new ConstantExpression(0, 1),
6970
new ConstantExpression('tpl1', 1),
@@ -77,7 +78,7 @@ public static function getTestsForFormTheme()
7778
[
7879
'{% form_theme form with "tpl1" %}',
7980
new FormThemeNode(
80-
new NameExpression('form', 1),
81+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
8182
new ConstantExpression('tpl1', 1),
8283
1,
8384
'form_theme'
@@ -86,7 +87,7 @@ public static function getTestsForFormTheme()
8687
[
8788
'{% form_theme form with ["tpl1"] %}',
8889
new FormThemeNode(
89-
new NameExpression('form', 1),
90+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
9091
new ArrayExpression([
9192
new ConstantExpression(0, 1),
9293
new ConstantExpression('tpl1', 1),
@@ -98,7 +99,7 @@ public static function getTestsForFormTheme()
9899
[
99100
'{% form_theme form with ["tpl1", "tpl2"] %}',
100101
new FormThemeNode(
101-
new NameExpression('form', 1),
102+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
102103
new ArrayExpression([
103104
new ConstantExpression(0, 1),
104105
new ConstantExpression('tpl1', 1),
@@ -112,7 +113,7 @@ public static function getTestsForFormTheme()
112113
[
113114
'{% form_theme form with ["tpl1", "tpl2"] only %}',
114115
new FormThemeNode(
115-
new NameExpression('form', 1),
116+
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
116117
new ArrayExpression([
117118
new ConstantExpression(0, 1),
118119
new ConstantExpression('tpl1', 1),

src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\TokenParser;
1313

1414
use Symfony\Bridge\Twig\Node\DumpNode;
15+
use Twig\Node\Expression\Variable\LocalVariable;
1516
use Twig\Node\Node;
1617
use Twig\Token;
1718
use Twig\TokenParser\AbstractTokenParser;
@@ -40,7 +41,7 @@ public function parse(Token $token): Node
4041
}
4142
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
4243

43-
return new DumpNode($this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
44+
return new DumpNode(class_exists(LocalVariable::class) ? new LocalVariable(null, $token->getLine()) : $this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
4445
}
4546

4647
/**

0 commit comments

Comments
 (0)
0