Skip to content

[TwigBridge] fix compatibility with Twig 3.12 and 4.0 #58020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function enterNode(Node $node, Environment $env): Node
return $node;
}

if ($node instanceof FilterExpression && 'trans' === $node->getNode('filter')->getAttribute('value')) {
if ($node instanceof FilterExpression && 'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value'))) {
$arguments = $node->getNode('arguments');
if ($this->isNamedArguments($arguments)) {
if (!$arguments->hasNode('domain') && !$arguments->hasNode(1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function enterNode(Node $node, Environment $env): Node

if (
$node instanceof FilterExpression &&
'trans' === $node->getNode('filter')->getAttribute('value') &&
'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value')) &&
$node->getNode('node') instanceof ConstantExpression
) {
// extract constant nodes with a trans filter
Expand Down Expand Up @@ -85,7 +85,7 @@ public function enterNode(Node $node, Environment $env): Node
];
} elseif (
$node instanceof FilterExpression &&
'trans' === $node->getNode('filter')->getAttribute('value') &&
'trans' === ($node->hasAttribute('twig_callable') ? $node->getAttribute('twig_callable')->getName() : $node->getNode('filter')->getAttribute('value')) &&
$node->getNode('node') instanceof ConcatBinary &&
$message = $this->getConcatValueFromNode($node->getNode('node'), null)
) {
Expand Down
62 changes: 52 additions & 10 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
use Twig\Attribute\FirstClassTwigCallableReady;
use Twig\Compiler;
use Twig\Environment;
use Twig\Extension\CoreExtension;
Expand All @@ -22,6 +23,7 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\TwigFunction;

class SearchAndRenderBlockNodeTest extends TestCase
{
Expand All @@ -31,7 +33,11 @@ public function testCompileWidget()
new NameExpression('form', 0),
]);

$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
}

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

Expand All @@ -54,7 +60,11 @@ public function testCompileWidgetWithVariables()
], 0),
]);

$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_widget'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
}

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

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

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand All @@ -94,7 +108,11 @@ public function testCompileLabelWithNullLabel()
new ConstantExpression(null, 0),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand All @@ -116,7 +134,11 @@ public function testCompileLabelWithEmptyStringLabel()
new ConstantExpression('', 0),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

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

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand All @@ -161,7 +187,11 @@ public function testCompileLabelWithAttributes()
], 0),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand Down Expand Up @@ -190,7 +220,11 @@ public function testCompileLabelWithLabelAndAttributes()
], 0),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand Down Expand Up @@ -218,7 +252,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand Down Expand Up @@ -256,7 +294,11 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
], 0),
]);

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new SearchAndRenderBlockNode(new TwigFunction('form_label'), $arguments, 0);
} else {
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
use Twig\Attribute\FirstClassTwigCallableReady;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\TwigFilter;
use Twig\TwigFunction;

class TranslationNodeVisitorTest extends TestCase
{
Expand All @@ -38,15 +41,27 @@ public function testMessageExtractionWithInvalidDomainNode()
{
$message = 'new key';

$node = new FilterExpression(
new ConstantExpression($message, 0),
new ConstantExpression('trans', 0),
new Node([
new ArrayExpression([], 0),
new NameExpression('variable', 0),
]),
0
);
if (class_exists(FirstClassTwigCallableReady::class)) {
$node = new FilterExpression(
new ConstantExpression($message, 0),
new TwigFilter('trans'),
new Node([
new ArrayExpression([], 0),
new NameExpression('variable', 0),
]),
0
);
} else {
$node = new FilterExpression(
new ConstantExpression($message, 0),
new ConstantExpression('trans', 0),
new Node([
new ArrayExpression([], 0),
new NameExpression('variable', 0),
]),
0
);
}

$this->testMessagesExtraction($node, [[$message, TranslationNodeVisitor::UNDEFINED_DOMAIN]]);
}
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

use Symfony\Bridge\Twig\Node\TransDefaultDomainNode;
use Symfony\Bridge\Twig\Node\TransNode;
use Twig\Attribute\FirstClassTwigCallableReady;
use Twig\Node\BodyNode;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Source;
use Twig\TwigFilter;

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

if (!class_exists(FirstClassTwigCallableReady::class)) {
return new FilterExpression(
new ConstantExpression($message, 0),
new ConstantExpression('trans', 0),
new Node($arguments),
0
);
}

return new FilterExpression(
new ConstantExpression($message, 0),
new ConstantExpression('trans', 0),
new TwigFilter('trans'),
new Node($arguments),
0
);
Expand Down
Loading