Skip to content

Commit 02f2e11

Browse files
Merge pull request #12 from MauricioFauth/twig39
Add support for Twig 3.9
2 parents 8c36fb5 + d025b88 commit 02f2e11

File tree

5 files changed

+84
-22
lines changed

5 files changed

+84
-22
lines changed

src/Node/TransNode.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace PhpMyAdmin\Twig\Extensions\Node;
1616

17+
use Twig\Attribute\YieldReady;
1718
use Twig\Compiler;
1819
use Twig\Node\CheckToStringNode;
1920
use Twig\Node\Expression\AbstractExpression;
@@ -26,6 +27,7 @@
2627
use Twig\Node\TextNode;
2728

2829
use function array_merge;
30+
use function class_exists;
2931
use function count;
3032
use function sprintf;
3133
use function str_replace;
@@ -36,6 +38,7 @@
3638
*
3739
* Author Fabien Potencier <[email protected]>
3840
*/
41+
#[YieldReady]
3942
class TransNode extends Node
4043
{
4144
/**
@@ -134,7 +137,7 @@ public function compile(Compiler $compiler)
134137

135138
if ($vars) {
136139
$compiler
137-
->raw('echo strtr(' . $function . '(');
140+
->raw($this->echoOrYield() . ' strtr(' . $function . '(');
138141

139142
if ($hasDomain) {
140143
[$domain] = $this->compileString($this->getNode('domain'));
@@ -184,7 +187,7 @@ public function compile(Compiler $compiler)
184187
$compiler->raw("));\n");
185188
} else {
186189
$compiler
187-
->raw('echo ' . $function . '(');
190+
->raw($this->echoOrYield() . ' ' . $function . '(');
188191

189192
if ($hasDomain) {
190193
[$domain] = $this->compileString($this->getNode('domain'));
@@ -314,4 +317,9 @@ protected function getTransFunction(bool $hasPlural, bool $hasContext, bool $has
314317
// gettext($msgid);
315318
return $functionPrefix . ($hasContext ? 'pgettext' : 'gettext');
316319
}
320+
321+
private function echoOrYield(): string
322+
{
323+
return class_exists(YieldReady::class) ? 'yield' : 'echo';
324+
}
317325
}

src/TokenParser/TransTokenParser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ protected function preParse(Token $token): array
9292

9393
$this->checkTransString($body, $lineno);
9494

95+
if ($notes instanceof TextNode) {
96+
// Don't use TextNode for $notes to avoid it getting merged with $body in Twig >= 3.9.0.
97+
$notes = new Node([], ['data' => $notes->getAttribute('data')], $notes->getTemplateLine());
98+
}
99+
100+
if ($context instanceof TextNode) {
101+
// Don't use TextNode for $context to avoid it getting merged with $body in Twig >= 3.9.0.
102+
$context = new Node([], ['data' => $context->getAttribute('data')], $context->getTemplateLine());
103+
}
104+
95105
return [$body, $plural, $count, $context, $notes, $domain, $lineno, $this->getTag()];
96106
}
97107

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Test assigning to a variable with set
3+
--TEMPLATE--
4+
A {% set variable %}{% trans 'text' %}{% endset %} B {{ variable }} C
5+
--DATA--
6+
return []
7+
--CONFIG--
8+
return []
9+
--EXPECT--
10+
A B text C

test/Node/MoTranslatorTransTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@
1515
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;
1616

1717
use PhpMyAdmin\Twig\Extensions\Node\TransNode;
18+
use Twig\Attribute\YieldReady;
1819
use Twig\Node\Expression\ConstantExpression;
1920
use Twig\Node\Expression\NameExpression;
2021
use Twig\Node\Node;
2122
use Twig\Node\PrintNode;
2223
use Twig\Node\TextNode;
2324
use Twig\Test\NodeTestCase;
2425

26+
use function class_exists;
2527
use function sprintf;
2628

2729
class MoTranslatorTransTest extends NodeTestCase
2830
{
31+
private function echoOrYield(): string
32+
{
33+
return class_exists(YieldReady::class) ? 'yield' : 'echo';
34+
}
35+
2936
public static function setUpBeforeClass(): void
3037
{
3138
TransNode::$notesLabel = '// l10n: ';
@@ -82,7 +89,10 @@ public function getTests(): array
8289
new TextNode('coredomain', 0),
8390
], [], 0);
8491
$node = new TransNode($body, null, null, null, null, $domain, 0);
85-
$tests[] = [$node, sprintf('echo _dgettext("coredomain", %s);', $this->getVariableGetter('foo'))];
92+
$tests[] = [
93+
$node,
94+
sprintf($this->echoOrYield() . ' _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
95+
];
8696

8797
$body = new NameExpression('foo', 0);
8898
$domain = new Node([
@@ -94,7 +104,10 @@ public function getTests(): array
94104
$node = new TransNode($body, null, null, $context, null, $domain, 0);
95105
$tests[] = [
96106
$node,
97-
sprintf('echo _dpgettext("coredomain", "The context", %s);', $this->getVariableGetter('foo')),
107+
sprintf(
108+
$this->echoOrYield() . ' _dpgettext("coredomain", "The context", %s);',
109+
$this->getVariableGetter('foo')
110+
),
98111
];
99112

100113
$body = new Node([
@@ -106,7 +119,7 @@ public function getTests(): array
106119
$tests[] = [
107120
$node,
108121
sprintf(
109-
'echo strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
122+
$this->echoOrYield() . ' strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
110123
$this->getVariableGetter('foo')
111124
),
112125
];
@@ -128,7 +141,7 @@ public function getTests(): array
128141
$tests[] = [
129142
$node,
130143
sprintf(
131-
'echo strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
144+
$this->echoOrYield() . ' strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
132145
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
133146
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
134147
$this->getVariableGetter('name'),
@@ -148,7 +161,8 @@ public function getTests(): array
148161
$tests[] = [
149162
$node,
150163
sprintf(
151-
'echo strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
164+
$this->echoOrYield()
165+
. ' strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
152166
$this->getVariableGetter('foo')
153167
),
154168
];
@@ -173,7 +187,8 @@ public function getTests(): array
173187
$tests[] = [
174188
$node,
175189
sprintf(
176-
'echo strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
190+
$this->echoOrYield()
191+
. ' strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
177192
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
178193
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
179194
$this->getVariableGetter('name'),
@@ -196,7 +211,8 @@ public function getTests(): array
196211
$tests[] = [
197212
$node,
198213
sprintf(
199-
'echo strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
214+
$this->echoOrYield()
215+
. ' strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
200216
$this->getVariableGetter('foo')
201217
),
202218
];
@@ -224,7 +240,8 @@ public function getTests(): array
224240
$tests[] = [
225241
$node,
226242
sprintf(
227-
'echo strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
243+
$this->echoOrYield()
244+
. ' strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
228245
. ' "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
229246
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
230247
$this->getVariableGetter('name'),

test/Node/TransTest.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;
1616

1717
use PhpMyAdmin\Twig\Extensions\Node\TransNode;
18+
use Twig\Attribute\YieldReady;
1819
use Twig\Node\Expression\ConstantExpression;
1920
use Twig\Node\Expression\FilterExpression;
2021
use Twig\Node\Expression\NameExpression;
@@ -23,10 +24,16 @@
2324
use Twig\Node\TextNode;
2425
use Twig\Test\NodeTestCase;
2526

27+
use function class_exists;
2628
use function sprintf;
2729

2830
class TransTest extends NodeTestCase
2931
{
32+
private function echoOrYield(): string
33+
{
34+
return class_exists(YieldReady::class) ? 'yield' : 'echo';
35+
}
36+
3037
public function testConstructor(): void
3138
{
3239
$count = new ConstantExpression(12, 0);
@@ -89,7 +96,8 @@ public function testEnableDebugNotEnabled(): void
8996
$this->assertEmpty($compiler->getDebugInfo());
9097
$sourceCode = $compiler->compile($node)->getSource();
9198
$this->assertSame(
92-
'// custom: Notes for translators' . "\n" . 'echo strtr(ngettext("There is 1 pending task",'
99+
'// custom: Notes for translators' . "\n"
100+
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
93101
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
94102
$sourceCode
95103
);
@@ -117,7 +125,8 @@ public function testEnableDebugEnabled(): void
117125
$this->assertEmpty($compiler->getDebugInfo());
118126
$sourceCode = $compiler->compile($node)->getSource();
119127
$this->assertSame(
120-
'// line 80' . "\n" . '// custom: Notes for translators' . "\n" . 'echo strtr(ngettext("There'
128+
'// line 80' . "\n" . '// custom: Notes for translators' . "\n"
129+
. $this->echoOrYield() . ' strtr(ngettext("There'
121130
. ' is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
122131
$sourceCode
123132
);
@@ -138,21 +147,24 @@ public function getTests(): array
138147
new TextNode('coredomain', 0),
139148
], [], 0);
140149
$node = new TransNode($body, null, null, null, null, $domain, 0);
141-
$tests[] = [$node, sprintf('echo dgettext("coredomain", %s);', $this->getVariableGetter('foo'))];
150+
$tests[] = [
151+
$node,
152+
sprintf($this->echoOrYield() . ' dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
153+
];
142154

143155
$body = new NameExpression('foo', 0);
144156
$node = new TransNode($body, null, null, null, null, null, 0);
145-
$tests[] = [$node, sprintf('echo gettext(%s);', $this->getVariableGetter('foo'))];
157+
$tests[] = [$node, sprintf($this->echoOrYield() . ' gettext(%s);', $this->getVariableGetter('foo'))];
146158

147159
$body = new ConstantExpression('Hello', 0);
148160
$node = new TransNode($body, null, null, null, null, null, 0);
149-
$tests[] = [$node, 'echo gettext("Hello");'];
161+
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];
150162

151163
$body = new Node([
152164
new TextNode('Hello', 0),
153165
], [], 0);
154166
$node = new TransNode($body, null, null, null, null, null, 0);
155-
$tests[] = [$node, 'echo gettext("Hello");'];
167+
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];
156168

157169
$body = new Node([
158170
new TextNode('J\'ai ', 0),
@@ -163,7 +175,7 @@ public function getTests(): array
163175
$tests[] = [
164176
$node,
165177
sprintf(
166-
'echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
178+
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
167179
$this->getVariableGetter('foo')
168180
),
169181
];
@@ -185,7 +197,7 @@ public function getTests(): array
185197
$tests[] = [
186198
$node,
187199
sprintf(
188-
'echo strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
200+
$this->echoOrYield() . ' strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
189201
. ' %%count%% apples", abs(12)), array("%%name%%" => %s,'
190202
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
191203
$this->getVariableGetter('name'),
@@ -207,7 +219,7 @@ public function getTests(): array
207219
$tests[] = [
208220
$node,
209221
sprintf(
210-
'echo strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
222+
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
211223
$this->getVariableGetter('foo')
212224
),
213225
];
@@ -216,12 +228,16 @@ public function getTests(): array
216228
$body = new ConstantExpression('Hello', 0);
217229
$notes = new TextNode('Notes for translators', 0);
218230
$node = new TransNode($body, null, null, null, $notes, null, 0);
219-
$tests[] = [$node, "// notes: Notes for translators\necho gettext(\"Hello\");"];
231+
$tests[] = [$node, "// notes: Notes for translators\n" . $this->echoOrYield() . ' gettext("Hello");'];
220232

221233
$body = new ConstantExpression('Hello', 0);
222234
$notes = new TextNode("Notes for translators\nand line breaks", 0);
223235
$node = new TransNode($body, null, null, null, $notes, null, 0);
224-
$tests[] = [$node, "// notes: Notes for translators and line breaks\necho gettext(\"Hello\");"];
236+
$tests[] = [
237+
$node,
238+
"// notes: Notes for translators and line breaks\n"
239+
. $this->echoOrYield() . ' gettext("Hello");',
240+
];
225241

226242
$count = new ConstantExpression(5, 0);
227243
$body = new TextNode('There is 1 pending task', 0);
@@ -234,7 +250,8 @@ public function getTests(): array
234250
$node = new TransNode($body, $plural, $count, null, $notes, null, 0);
235251
$tests[] = [
236252
$node,
237-
'// notes: Notes for translators' . "\n" . 'echo strtr(ngettext("There is 1 pending task",'
253+
'// notes: Notes for translators' . "\n"
254+
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
238255
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));',
239256
];
240257

0 commit comments

Comments
 (0)