Skip to content

Commit ad2c2c8

Browse files
[TwigHooks] Fix Twig 3 deprecations (#311)
This PR fixes the Twig 3 deprecations reported in #307
2 parents 6c5b7cc + 2e352de commit ad2c2c8

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

src/TwigHooks/src/Twig/Node/HookNode.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,43 @@
1414
namespace Sylius\TwigHooks\Twig\Node;
1515

1616
use Sylius\TwigHooks\Twig\Runtime\HooksRuntime;
17+
use Twig\Attribute\YieldReady;
1718
use Twig\Compiler;
1819
use Twig\Node\Expression\ArrayExpression;
1920
use Twig\Node\Node;
2021

22+
#[YieldReady]
2123
final class HookNode extends Node
2224
{
2325
public function __construct(
2426
Node $name,
2527
?Node $context,
2628
bool $only,
2729
int $lineno,
28-
?string $tag = null,
2930
) {
31+
if (\func_num_args() > 4) {
32+
trigger_deprecation('sylius/twig-hooks', '0.11.0', \sprintf('The "tag" constructor argument of the "%s" class is deprecated and ignored (check which TokenParser class set it to "%s"), the tag is now automatically set by the Parser when needed.', static::class, func_get_arg(4) ?: 'null'));
33+
}
34+
35+
// Remove when twig < 3.12 support is dropped
36+
if (!class_exists(\Twig\Node\Expression\FunctionNode\EnumCasesFunction::class)) {
37+
$tag = func_get_arg(4);
38+
39+
parent::__construct(
40+
[
41+
'name' => $name,
42+
'hook_level_context' => $context ?? new ArrayExpression([], $lineno),
43+
],
44+
[
45+
'only' => $only,
46+
],
47+
$lineno,
48+
$tag,
49+
);
50+
51+
return;
52+
}
53+
3054
parent::__construct(
3155
[
3256
'name' => $name,
@@ -36,7 +60,6 @@ public function __construct(
3660
'only' => $only,
3761
],
3862
$lineno,
39-
$tag,
4063
);
4164
}
4265

@@ -49,7 +72,7 @@ public function compile(Compiler $compiler): void
4972
HooksRuntime::class,
5073
))->raw("\n");
5174

52-
$compiler->raw('echo $hooksRuntime->renderHook(');
75+
$compiler->raw(sprintf('%s $hooksRuntime->renderHook(', class_exists(YieldReady::class) ? 'yield' : 'echo'));
5376
$compiler->subcompile($this->getNode('name'));
5477
$compiler->raw(', ');
5578
$compiler->subcompile($this->getNode('hook_level_context'));

src/TwigHooks/src/Twig/TokenParser/HookTokenParser.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Sylius\TwigHooks\Twig\Node\HookNode;
1717
use Twig\Node\Node;
18+
use Twig\Node\Nodes;
1819
use Twig\Token;
1920
use Twig\TokenParser\AbstractTokenParser;
2021

@@ -26,11 +27,21 @@ public function parse(Token $token): Node
2627
{
2728
$lineno = $token->getLine();
2829
$stream = $this->parser->getStream();
29-
$hooksNames = $this->parser->getExpressionParser()->parseExpression();
30+
if (method_exists($this->parser, 'parseExpression')) {
31+
$hooksNames = $this->parser->parseExpression();
32+
} else {
33+
// Remove when Twig 3.21 support is dropped
34+
$hooksNames = $this->parser->getExpressionParser()->parseExpression();
35+
}
3036

3137
$hookContext = null;
3238
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
33-
$hookContext = $this->parser->getExpressionParser()->parseMultitargetExpression();
39+
if (method_exists($this->parser, 'parseExpression')) {
40+
$hookContext = $this->parseMultitargetExpression();
41+
} else {
42+
// Remove when Twig 3.21 support is dropped
43+
$hookContext = $this->parser->getExpressionParser()->parseMultitargetExpression();
44+
}
3445
}
3546

3647
$only = false;
@@ -40,11 +51,29 @@ public function parse(Token $token): Node
4051

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

54+
if (class_exists(\Twig\Node\Expression\FunctionNode\EnumCasesFunction::class)) {
55+
return new HookNode($hooksNames, $hookContext, $only, $lineno);
56+
}
57+
58+
// Remove when twig < 3.12 support is dropped
4359
return new HookNode($hooksNames, $hookContext, $only, $lineno, $this->getTag());
4460
}
4561

4662
public function getTag(): string
4763
{
4864
return self::TAG;
4965
}
66+
67+
private function parseMultitargetExpression(): Nodes
68+
{
69+
$targets = [];
70+
while (true) {
71+
$targets[] = $this->parser->parseExpression();
72+
if (!$this->parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ',')) {
73+
break;
74+
}
75+
}
76+
77+
return new Nodes($targets);
78+
}
5079
}

0 commit comments

Comments
 (0)