Skip to content

Commit

Permalink
fix compatibility with Twig 3.12 and 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Aug 16, 2024
1 parent d3e25c3 commit 06f5ef3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion NodeVisitor/TranslationDefaultDomainNodeVisitor.php
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
4 changes: 2 additions & 2 deletions NodeVisitor/TranslationNodeVisitor.php
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 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
33 changes: 24 additions & 9 deletions Tests/NodeVisitor/TranslationNodeVisitorTest.php
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 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

0 comments on commit 06f5ef3

Please sign in to comment.