Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72d5b21

Browse files
committed
fix compatibility with Twig 3.12 and 4.0
1 parent 14b1b4d commit 72d5b21

File tree

5 files changed

+91
-23
lines changed

5 files changed

+91
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function enterNode(Node $node, Environment $env): Node
6161
return $node;
6262
}
6363

64-
if ($node instanceof FilterExpression && 'trans' === $node->getNode('filter')->getAttribute('value')) {
64+
if ($node instanceof FilterExpression && 'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value'))) {
6565
$arguments = $node->getNode('arguments');
6666
if ($this->isNamedArguments($arguments)) {
6767
if (!$arguments->hasNode('domain') && !$arguments->hasNode(1)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function enterNode(Node $node, Environment $env): Node
5757

5858
if (
5959
$node instanceof FilterExpression &&
60-
'trans' === $node->getNode('filter')->getAttribute('value') &&
60+
'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value')) &&
6161
$node->getNode('node') instanceof ConstantExpression
6262
) {
6363
// extract constant nodes with a trans filter
@@ -85,7 +85,7 @@ public function enterNode(Node $node, Environment $env): Node
8585
];
8686
} elseif (
8787
$node instanceof FilterExpression &&
88-
'trans' === $node->getNode('filter')->getAttribute('value') &&
88+
'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value')) &&
8989
$node->getNode('node') instanceof ConcatBinary &&
9090
$message = $this->getConcatValueFromNode($node->getNode('node'), null)
9191
) {

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

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
16+
use Twig\Attribute\FirstClassTwigCallableReady;
1617
use Twig\Compiler;
1718
use Twig\Environment;
1819
use Twig\Extension\CoreExtension;
@@ -22,6 +23,7 @@
2223
use Twig\Node\Expression\ConstantExpression;
2324
use Twig\Node\Expression\NameExpression;
2425
use Twig\Node\Node;
26+
use Twig\TwigFunction;
2527

2628
class SearchAndRenderBlockNodeTest extends TestCase
2729
{
@@ -31,7 +33,11 @@ public function testCompileWidget()
3133
new NameExpression('form', 0),
3234
]);
3335

34-
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
36+
if (class_exists(FirstClassTwigCallableReady::class)) {
37+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
38+
} else {
39+
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
40+
}
3541

3642
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
3743

@@ -54,7 +60,11 @@ public function testCompileWidgetWithVariables()
5460
], 0),
5561
]);
5662

57-
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
63+
if (class_exists(FirstClassTwigCallableReady::class)) {
64+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
65+
} else {
66+
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
67+
}
5868

5969
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
6070

@@ -74,7 +84,11 @@ public function testCompileLabelWithLabel()
7484
new ConstantExpression('my label', 0),
7585
]);
7686

77-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
87+
if (class_exists(FirstClassTwigCallableReady::class)) {
88+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
89+
} else {
90+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
91+
}
7892

7993
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
8094

@@ -94,7 +108,11 @@ public function testCompileLabelWithNullLabel()
94108
new ConstantExpression(null, 0),
95109
]);
96110

97-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
111+
if (class_exists(FirstClassTwigCallableReady::class)) {
112+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
113+
} else {
114+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
115+
}
98116

99117
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
100118

@@ -116,7 +134,11 @@ public function testCompileLabelWithEmptyStringLabel()
116134
new ConstantExpression('', 0),
117135
]);
118136

119-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
137+
if (class_exists(FirstClassTwigCallableReady::class)) {
138+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
139+
} else {
140+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
141+
}
120142

121143
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
122144

@@ -137,7 +159,11 @@ public function testCompileLabelWithDefaultLabel()
137159
new NameExpression('form', 0),
138160
]);
139161

140-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
162+
if (class_exists(FirstClassTwigCallableReady::class)) {
163+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
164+
} else {
165+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
166+
}
141167

142168
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
143169

@@ -161,7 +187,11 @@ public function testCompileLabelWithAttributes()
161187
], 0),
162188
]);
163189

164-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
190+
if (class_exists(FirstClassTwigCallableReady::class)) {
191+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
192+
} else {
193+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
194+
}
165195

166196
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
167197

@@ -190,7 +220,11 @@ public function testCompileLabelWithLabelAndAttributes()
190220
], 0),
191221
]);
192222

193-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
223+
if (class_exists(FirstClassTwigCallableReady::class)) {
224+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
225+
} else {
226+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
227+
}
194228

195229
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
196230

@@ -218,7 +252,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
218252
),
219253
]);
220254

221-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
255+
if (class_exists(FirstClassTwigCallableReady::class)) {
256+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
257+
} else {
258+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
259+
}
222260

223261
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
224262

@@ -256,7 +294,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
256294
], 0),
257295
]);
258296

259-
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
297+
if (class_exists(FirstClassTwigCallableReady::class)) {
298+
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
299+
} else {
300+
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
301+
}
260302

261303
$compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));
262304

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
16+
use Twig\Attribute\FirstClassTwigCallableReady;
1617
use Twig\Environment;
1718
use Twig\Loader\LoaderInterface;
1819
use Twig\Node\Expression\ArrayExpression;
1920
use Twig\Node\Expression\ConstantExpression;
2021
use Twig\Node\Expression\FilterExpression;
2122
use Twig\Node\Expression\NameExpression;
2223
use Twig\Node\Node;
24+
use Twig\TwigFilter;
25+
use Twig\TwigFunction;
2326

2427
class TranslationNodeVisitorTest extends TestCase
2528
{
@@ -38,15 +41,27 @@ public function testMessageExtractionWithInvalidDomainNode()
3841
{
3942
$message = 'new key';
4043

41-
$node = new FilterExpression(
42-
new ConstantExpression($message, 0),
43-
new ConstantExpression('trans', 0),
44-
new Node([
45-
new ArrayExpression([], 0),
46-
new NameExpression('variable', 0),
47-
]),
48-
0
49-
);
44+
if (class_exists(FirstClassTwigCallableReady::class)) {
45+
$node = new FilterExpression(
46+
new ConstantExpression($message, 0),
47+
new TwigFilter('trans'),
48+
new Node([
49+
new ArrayExpression([], 0),
50+
new NameExpression('variable', 0),
51+
]),
52+
0
53+
);
54+
} else {
55+
$node = new FilterExpression(
56+
new ConstantExpression($message, 0),
57+
new ConstantExpression('trans', 0),
58+
new Node([
59+
new ArrayExpression([], 0),
60+
new NameExpression('variable', 0),
61+
]),
62+
0
63+
);
64+
}
5065

5166
$this->testMessagesExtraction($node, [[$message, TranslationNodeVisitor::UNDEFINED_DOMAIN]]);
5267
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
use Symfony\Bridge\Twig\Node\TransDefaultDomainNode;
1515
use Symfony\Bridge\Twig\Node\TransNode;
16+
use Twig\Attribute\FirstClassTwigCallableReady;
1617
use Twig\Node\BodyNode;
1718
use Twig\Node\Expression\ArrayExpression;
1819
use Twig\Node\Expression\ConstantExpression;
1920
use Twig\Node\Expression\FilterExpression;
2021
use Twig\Node\ModuleNode;
2122
use Twig\Node\Node;
2223
use Twig\Source;
24+
use Twig\TwigFilter;
2325

2426
class TwigNodeProvider
2527
{
@@ -45,9 +47,18 @@ public static function getTransFilter($message, $domain = null, $arguments = nul
4547
] : [];
4648
}
4749

50+
if (!class_exists(FirstClassTwigCallableReady::class)) {
51+
return new FilterExpression(
52+
new ConstantExpression($message, 0),
53+
new ConstantExpression('trans', 0),
54+
new Node($arguments),
55+
0
56+
);
57+
}
58+
4859
return new FilterExpression(
4960
new ConstantExpression($message, 0),
50-
new ConstantExpression('trans', 0),
61+
new TwigFilter('trans'),
5162
new Node($arguments),
5263
0
5364
);

0 commit comments

Comments
 (0)