8000 bug #59774 [TwigBridge] Fix compatibility with Twig 3.21 (alexandre-d… · symfony/symfony@db15145 · GitHub
[go: up one dir, main page]

Skip to content

Commit db15145

Browse files
committed
bug #59774 [TwigBridge] Fix compatibility with Twig 3.21 (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [TwigBridge] Fix compatibility with Twig 3.21 | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT `Parser::getExpressionParser()` is deprecated since Twig 3.21. Commits ------- 5953ff7 [TwigBridge] Fix compatibility with Twig 3.21
2 parents 88649d9 + 5953ff7 commit db15145

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\Node\DumpNode;
1515
use Twig\Node\Expression\Variable\LocalVariable;
1616
use Twig\Node\Node;
17+
use Twig\Node\Nodes;
1718
use Twig\Token;
1819
use Twig\TokenParser\AbstractTokenParser;
1920

@@ -34,13 +35,28 @@ public function parse(Token $token): Node
3435
{
3536
$values = null;
3637
if (!$this->parser->getStream()->test(Token::BLOCK_END_TYPE)) {
37-
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
38+
$values = method_exists($this->parser, 'parseExpression') ?
39+
$this->parseMultitargetExpression() :
40+
$this->parser->getExpressionParser()->parseMultitargetExpression();
3841
}
3942
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
4043

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

47+
private function parseMultitargetExpression(): Node
48+
{
49+
$targets = [];
50+
while (true) {
51+
$targets[] = $this->parser->parseExpression();
52+
if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ',')) {
53+
break;
54+
}
55+
}
56+
57+
return new Nodes($targets);
58+
}
59+
4460
public function getTag(): string
4561
{
4662
return 'dump';

src/Symfony/Bridge/Twig/TokenParser/FormThemeTokenParser.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ public function parse(Token $token): Node
2929
$lineno = $token->getLine();
3030
$stream = $this->parser->getStream();
3131

32-
$form = $this->parser->getExpressionParser()->parseExpression();
32+
$parseExpression = method_exists($this->parser, 'parseExpression')
33+
? $this->parser->parseExpression(...)
34+
: $this->parser->getExpressionParser()->parseExpression(...);
35+
36+
$form = $parseExpression();
3337
$only = false;
3438

3539
if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
3640
$this->parser->getStream()->next();
37-
$resources = $this->parser->getExpressionParser()->parseExpression();
41+
$resources = $parseExpression();
3842

3943
if ($this->parser->getStream()->nextIf(Token::NAME_TYPE, 'only')) {
4044
$only = true;
4145
}
4246
} else {
4347
$resources = new ArrayExpression([], $stream->getCurrent()->getLine());
4448
do {
45-
$resources->addElement($this->parser->getExpressionParser()->parseExpression());
49+
$resources->addElement($parseExpression());
4650
} while (!$stream->test(Token::BLOCK_END_TYPE));
4751
}
4852

src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public function parse(Token $token): Node
3838
$stream = $this->parser->getStream();
3939

4040
// {% stopwatch 'bar' %}
41-
$name = $this->parser->getExpressionParser()->parseExpression();
41+
$name = method_exists($this->parser, 'parseExpression') ?
42+
$this->parser->parseExpression() :
43+
$this->parser->getExpressionParser()->parseExpression();
4244

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

src/Symfony/Bridge/Twig/TokenParser/TransDefaultDomainTokenParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ final class TransDefaultDomainTokenParser extends AbstractTokenParser
2525
{
2626
public function parse(Token $token): Node
2727
{
28-
$expr = $this->parser->getExpressionParser()->parseExpression();
28+
$expr = method_exists($this->parser, 'parseExpression') ?
29+
$this->parser->parseExpression() :
30+
$this->parser->getExpressionParser()->parseExpression();
2931

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

src/Symfony/Bridge/Twig/TokenParser/TransTokenParser.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,33 @@ public function parse(Token $token): Node
3636
$vars = new ArrayExpression([], $lineno);
3737
$domain = null;
3838
$locale = null;
39+
$parseExpression = method_exists($this->parser, 'parseExpression')
40+
? $this->parser->parseExpression(...)
41+
: $this->parser->getExpressionParser()->parseExpression(...);
42+
3943
if (!$stream->test(Token::BLOCK_END_TYPE)) {
4044
if ($stream->test('count')) {
4145
// {% trans count 5 %}
4246
$stream->next();
43-
$count = $this->parser->getExpressionParser()->parseExpression();
47+
$count = $parseExpression();
4448
}
4549

4650
if ($stream->test('with')) {
4751
// {% trans with vars %}
4852
$stream->next();
49-
$vars = $this->parser->getExpressionParser()->parseExpression();
53+
$vars = $parseExpression();
5054
}
5155

5256
if ($stream->test('from')) {
5357
// {% trans from "messages" %}
5458
$stream->next();
55-
$domain = $this->parser->getExpressionParser()->parseExpression();
59+
$domain = $parseExpression();
5660
}
5761

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

0 commit comments

Comments
 (0)
0