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

Skip to content

Commit a2decdb

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

10 files changed

+62
-42
lines changed

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: 7 additions & 6 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,11 +75,11 @@ public function testOneVar()
7475
{
7576
if (class_exists(Nodes::class)) {
7677
$vars = new Nodes([
77-
new NameExpression('foo', 7),
78+
class_exists(ContextVariable::class) ? new ContextVariable('foo', 7) : new NameExpression('foo', 7),
7879
]);
7980
} else {
8081
$vars = new Node([
81-
new NameExpression('foo', 7),
82+
class_exists(ContextVariable::class) ? new ContextVariable('foo', 7) : new NameExpression('foo', 7),
8283
]);
8384
}
8485

@@ -104,13 +105,13 @@ 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+
class_exists(ContextVariable::class) ? new ContextVariable('foo', 7) : new NameExpression('foo', 7),
109+
class_exists(ContextVariable::class) ? new ContextVariable('bar', 7) : new NameExpression('bar', 7),
109110
]);
110111
} else {
111112
$vars = new Node([
112-
new NameExpression('foo', 7),
113-
new NameExpression('bar', 7),
113+
class_exists(ContextVariable::class) ? new ContextVariable('foo', 7) : new NameExpression('foo', 7),
114+
class_exists(ContextVariable::class) ? new ContextVariable('bar', 7) : new NameExpression('bar', 7),
114115
]);
115116
}
116117

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

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,11 +33,11 @@ public function testCompileWidget()
3233
{
3334
if (class_exists(Nodes::class)) {
3435
$arguments = new Nodes([
35-
new NameExpression('form', 0),
36+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
3637
]);
3738
} else {
3839
$arguments = new Node([
39-
new NameExpression('form', 0),
40+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
4041
]);
4142
}
4243

@@ -61,15 +62,15 @@ public function testCompileWidgetWithVariables()
6162
{
6263
if (class_exists(Nodes::class)) {
6364
$arguments = new Nodes([
64-
new NameExpression('form', 0),
65+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
6566
new ArrayExpression([
6667
new ConstantExpression('foo', 0),
6768
new ConstantExpression('bar', 0),
6869
], 0),
6970
]);
7071
} else {
7172
$arguments = new Node([
72-
new NameExpression('form', 0),
73+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
7374
new ArrayExpression([
7475
new ConstantExpression('foo', 0),
7576
new ConstantExpression('bar', 0),
@@ -98,12 +99,12 @@ public function testCompileLabelWithLabel()
9899
{
99100
if (class_exists(Nodes::class)) {
100101
$arguments = new Nodes([
101-
new NameExpression('form', 0),
102+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
102103
new ConstantExpression('my label', 0),
103104
]);
104105
} else {
105106
$arguments = new Node([
106-
new NameExpression('form', 0),
107+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
107108
new ConstantExpression('my label', 0),
108109
]);
109110
}
@@ -129,12 +130,12 @@ public function testCompileLabelWithNullLabel()
129130
{
130131
if (class_exists(Nodes::class)) {
131132
$arguments = new Nodes([
132-
new NameExpression('form', 0),
133+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
133134
new ConstantExpression(null, 0),
134135
]);
135136
} else {
136137
$arguments = new Node([
137-
new NameExpression('form', 0),
138+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
138139
new ConstantExpression(null, 0),
139140
]);
140141
}
@@ -162,12 +163,12 @@ public function testCompileLabelWithEmptyStringLabel()
162163
{
163164
if (class_exists(Nodes::class)) {
164165
$arguments = new Nodes([
165-
new NameExpression('form', 0),
166+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
166167
new ConstantExpression('', 0),
167168
]);
168169
} else {
169170
$arguments = new Node([
170-
new NameExpression('form', 0),
171+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
171172
new ConstantExpression('', 0),
172173
]);
173174
}
@@ -195,11 +196,11 @@ public function testCompileLabelWithDefaultLabel()
195196
{
196197
if (class_exists(Nodes::class)) {
197198
$arguments = new Nodes([
198-
new NameExpression('form', 0),
199+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
199200
]);
200201
} else {
201202
$arguments = new Node([
202-
new NameExpression('form', 0),
203+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
203204
]);
204205
}
205206

@@ -224,7 +225,7 @@ public function testCompileLabelWithAttributes()
224225
{
225226
if (class_exists(Nodes::class)) {
226227
$arguments = new Nodes([
227-
new NameExpression('form', 0),
228+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
228229
new ConstantExpression(null, 0),
229230
new ArrayExpression([
230231
new ConstantExpression('foo', 0),
@@ -233,7 +234,7 @@ public function testCompileLabelWithAttributes()
233234
]);
234235
} else {
235236
$arguments = new Node([
236-
new NameExpression('form', 0),
237+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
237238
new ConstantExpression(null, 0),
238239
new ArrayExpression([
239240
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+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
270271
new ConstantExpression('value in argument', 0),
271272
new ArrayExpression([
272273
new ConstantExpression('foo', 0),
@@ -277,7 +278,7 @@ public function testCompileLabelWithLabelAndAttributes()
277278
]);
278279
} else {
279280
$arguments = new Node([
280-
new NameExpression('form', 0),
281+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
281282
new ConstantExpression('value in argument', 0),
282283
new ArrayExpression([
283284
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+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
313314
new ConditionalExpression(
314315
// if
315316
new ConstantExpression(true, 0),
@@ -322,7 +323,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
322323
]);
323324
} else {
324325
$arguments = new Node([
325-
new NameExpression('form', 0),
326+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
326327
new ConditionalExpression(
327328
// if
328329
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+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
364365
new ConditionalExpression(
365366
// if
366367
new ConstantExpression(true, 0),
@@ -379,7 +380,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
379380
]);
380381
} else {
381382
$arguments = new Node([
382-
new NameExpression('form', 0),
383+
class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0),
383384
new ConditionalExpression(
384385
new ConstantExpression(true, 0),
385386
new ConstantExpression(null, 0),
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]);
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,12 +45,12 @@ public function testMessageExtractionWithInvalidDomainNode()
4445
if (class_exists(Nodes::class)) {
4546
$n = new Nodes([
4647
new ArrayExpression([], 0),
47-
new NameExpression('variable', 0),
48+
class_exists(ContextVariable::class) ? new ContextVariable('variable', 0) : new NameExpression('variable', 0),
4849
]);
4950
} else {
5051
$n = new Node([
5152
new ArrayExpression([], 0),
52-
new NameExpression('variable', 0),
53+
class_exists(ContextVariable::class) ? new ContextVariable('variable', 0) : new NameExpression('variable', 0),
5354
]);
5455
}
5556

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),
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function parse(Token $token): Node
4040
}
4141
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
4242

43-
return new DumpNode($this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
43+
return new DumpNode($this->getVarName(), $values, $token->getLine(), $this->getTag());
4444
}
4545

4646
/**
@@ -50,4 +50,9 @@ public function getTag(): string
5050
{
5151
return 'dump';
5252
}
53+
54+
private function getVarName(): string
55+
{
56+
return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true)));
57+
}
5358
}