Skip to content

[TwigBridge] Fix compatibility with Twig 3.21 #59774

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
Feb 14, 2025
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
18 changes: 17 additions & 1 deletion src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bridge\Twig\Node\DumpNode;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

Expand All @@ -34,13 +35,28 @@ public function parse(Token $token): Node
{
$values = null;
if (!$this->parser->getStream()->test(Token::BLOCK_END_TYPE)) {
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
$values = method_exists($this->parser, 'parseExpression') ?
$this->parseMultitargetExpression() :
$this->parser->getExpressionParser()->parseMultitargetExpression();
}
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

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

private function parseMultitargetExpression(): Node
{
$targets = [];
while (true) {
$targets[] = $this->parser->parseExpression();
if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break;
}
}

return new Nodes($targets);
}

public function getTag(): string
{
return 'dump';
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Bridge/Twig/TokenParser/FormThemeTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ public function parse(Token $token): Node
$lineno = $token->getLine();
$stream = $this->parser->getStream();

$form = $this->parser->getExpressionParser()->parseExpression();
$parseExpression = method_exists($this->parser, 'parseExpression')
? $this->parser->parseExpression(...)
: $this->parser->getExpressionParser()->parseExpression(...);

$form = $parseExpression();
$only = false;

if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
$this->parser->getStream()->next();
$resources = $this->parser->getExpressionParser()->parseExpression();
$resources = $parseExpression();

if ($this->parser->getStream()->nextIf(Token::NAME_TYPE, 'only')) {
$only = true;
}
} else {
$resources = new ArrayExpression([], $stream->getCurrent()->getLine());
do {
$resources->addElement($this->parser->getExpressionParser()->parseExpression());
$resources->addElement($parseExpression());
} while (!$stream->test(Token::BLOCK_END_TYPE));
}

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function parse(Token $token): Node
$stream = $this->parser->getStream();

// {% stopwatch 'bar' %}
$name = $this->parser->getExpressionParser()->parseExpression();
$name = method_exists($this->parser, 'parseExpression') ?
$this->parser->parseExpression() :
$this->parser->getExpressionParser()->parseExpression();

$stream->expect(Token::BLOCK_END_TYPE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ final class TransDefaultDomainTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
$expr = $this->parser->getExpressionParser()->parseExpression();
$expr = method_exists($this->parser, 'parseExpression') ?
$this->parser->parseExpression() :
$this->parser->getExpressionParser()->parseExpression();

$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Bridge/Twig/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,33 @@ public function parse(Token $token): Node
$vars = new ArrayExpression([], $lineno);
$domain = null;
$locale = null;
$parseExpression = method_exists($this->parser, 'parseExpression')
? $this->parser->parseExpression(...)
: $this->parser->getExpressionParser()->parseExpression(...);

if (!$stream->test(Token::BLOCK_END_TYPE)) {
if ($stream->test('count')) {
// {% trans count 5 %}
$stream->next();
$count = $this->parser->getExpressionParser()->parseExpression();
$count = $parseExpression();
}

if ($stream->test('with')) {
// {% trans with vars %}
$stream->next();
$vars = $this->parser->getExpressionParser()->parseExpression();
$vars = $parseExpression();
}

if ($stream->test('from')) {
// {% trans from "messages" %}
$stream->next();
$domain = $this->parser->getExpressionParser()->parseExpression();
$domain = $parseExpression();
}

if ($stream->test('into')) {
// {% trans into "fr" %}
$stream->next();
$locale = $this->parser->getExpressionParser()->parseExpression();
$locale = $parseExpression();
} elseif (!$stream->test(Token::BLOCK_END_TYPE)) {
throw new SyntaxError('Unexpected token. Twig was looking for the "with", "from", or "into" keyword.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
Expand Down
Loading