Skip to content

Commit

Permalink
Merge pull request #13 from MauricioFauth/twig-39
Browse files Browse the repository at this point in the history
Drop support for Twig < 3.9.0
  • Loading branch information
MauricioFauth committed Apr 19, 2024
2 parents 02bc04f + d02b2bd commit c600af9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

* Drop support for Twig < 3.9.0
* Drop support for PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0

## [4.1.1] - 2024-04-19
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
},
"require": {
"php": "^8.1",
"twig/twig": "^3.7"
"twig/twig": "^3.9"
},
"require-dev": {
"phpmyadmin/coding-standard": "^4.0",
"phpmyadmin/motranslator": "^5.2",
"phpmyadmin/motranslator": "^6.0-dev",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
Expand Down
14 changes: 3 additions & 11 deletions src/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Twig\Node\TextNode;

use function array_merge;
use function class_exists;
use function count;
use function sprintf;
use function str_replace;
Expand Down Expand Up @@ -122,14 +121,13 @@ public function compile(Compiler $compiler)
if ($this->hasNode('notes')) {
$message = trim($this->getNode('notes')->getAttribute('data'));

// line breaks are not allowed cause we want a single line comment
// line breaks are not allowed because we want a single line comment
$message = str_replace(["\n", "\r"], ' ', $message);
$compiler->raw(static::$notesLabel . $message . "\n");
}

if ($vars) {
$compiler
->raw($this->echoOrYield() . ' strtr(' . $function . '(');
$compiler->raw('yield strtr(' . $function . '(');

if ($hasDomain) {
[$domain] = $this->compileString($this->getNode('domain'));
Expand Down Expand Up @@ -178,8 +176,7 @@ public function compile(Compiler $compiler)

$compiler->raw("));\n");
} else {
$compiler
->raw($this->echoOrYield() . ' ' . $function . '(');
$compiler->raw('yield ' . $function . '(');

if ($hasDomain) {
[$domain] = $this->compileString($this->getNode('domain'));
Expand Down Expand Up @@ -311,9 +308,4 @@ protected function getTransFunction(bool $hasPlural, bool $hasContext, bool $has
// gettext($msgid);
return $functionPrefix . ($hasContext ? 'pgettext' : 'gettext');
}

private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}
}
4 changes: 2 additions & 2 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ protected function preParse(Token $token): array
$this->checkTransString($body, $lineno);

if ($notes instanceof TextNode) {
// Don't use TextNode for $notes to avoid it getting merged with $body in Twig >= 3.9.0.
// Don't use TextNode for $notes to avoid it getting merged with $body when optimizing.
$notes = new Node([], ['data' => $notes->getAttribute('data')], $notes->getTemplateLine());
}

if ($context instanceof TextNode) {
// Don't use TextNode for $context to avoid it getting merged with $body in Twig >= 3.9.0.
// Don't use TextNode for $context to avoid it getting merged with $body when optimizing.
$context = new Node([], ['data' => $context->getAttribute('data')], $context->getTemplateLine());
}

Expand Down
27 changes: 8 additions & 19 deletions test/Node/MoTranslatorTransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;

use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Attribute\YieldReady;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
use Twig\Test\NodeTestCase;

use function class_exists;
use function sprintf;

class MoTranslatorTransTest extends NodeTestCase
{
private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}

public static function setUpBeforeClass(): void
{
TransNode::$notesLabel = '// l10n: ';
Expand Down Expand Up @@ -89,7 +82,7 @@ public function getTests(): array
$node = new TransNode($body, null, null, null, null, $domain, 0);
$tests[] = [
$node,
sprintf($this->echoOrYield() . ' _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
sprintf('yield _dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
Expand All @@ -103,7 +96,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' _dpgettext("coredomain", "The context", %s);',
'yield _dpgettext("coredomain", "The context", %s);',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -117,7 +110,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
'yield strtr(_gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -139,7 +132,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
'yield strtr(_ngettext("Hey %%name%%, I have one apple", "Hey %%name%%,'
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -159,8 +152,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield()
. ' strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
'yield strtr(_pgettext("The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -185,8 +177,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield()
. ' strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
'yield strtr(_npgettext("The context", "Hey %%name%%, I have one apple", "Hey %%name%%,'
. ' I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -209,8 +200,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield()
. ' strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
'yield strtr(_dpgettext("mydomain", "The context", "J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand Down Expand Up @@ -238,8 +228,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield()
. ' strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
'yield strtr(_dnpgettext("mydomain", "The context", "Hey %%name%%, I have one apple",'
. ' "Hey %%name%%, I have %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand Down
31 changes: 12 additions & 19 deletions test/Node/TransTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace PhpMyAdmin\Tests\Twig\Extensions\Node;

use PhpMyAdmin\Twig\Extensions\Node\TransNode;
use Twig\Attribute\YieldReady;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
Expand All @@ -24,16 +23,10 @@
use Twig\Node\TextNode;
use Twig\Test\NodeTestCase;

use function class_exists;
use function sprintf;

class TransTest extends NodeTestCase
{
private function echoOrYield(): string
{
return class_exists(YieldReady::class) ? 'yield' : 'echo';
}

public function testConstructor(): void
{
$count = new ConstantExpression(12, 0);
Expand Down Expand Up @@ -97,7 +90,7 @@ public function testEnableDebugNotEnabled(): void
$sourceCode = $compiler->compile($node)->getSource();
$this->assertSame(
'// custom: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
. 'yield strtr(ngettext("There is 1 pending task",'
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
$sourceCode,
);
Expand Down Expand Up @@ -126,7 +119,7 @@ public function testEnableDebugEnabled(): void
$sourceCode = $compiler->compile($node)->getSource();
$this->assertSame(
'// line 80' . "\n" . '// custom: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There'
. 'yield strtr(ngettext("There'
. ' is 1 pending task", "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));' . "\n",
$sourceCode,
);
Expand All @@ -147,22 +140,22 @@ public function getTests(): array
$node = new TransNode($body, null, null, null, null, $domain, 0);
$tests[] = [
$node,
sprintf($this->echoOrYield() . ' dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
sprintf('yield dgettext("coredomain", %s);', $this->getVariableGetter('foo')),
];

$body = new NameExpression('foo', 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, sprintf($this->echoOrYield() . ' gettext(%s);', $this->getVariableGetter('foo'))];
$tests[] = [$node, sprintf('yield gettext(%s);', $this->getVariableGetter('foo'))];

$body = new ConstantExpression('Hello', 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];
$tests[] = [$node, 'yield gettext("Hello");'];

$body = new Node([
new TextNode('Hello', 0),
], [], 0);
$node = new TransNode($body, null, null, null, null, null, 0);
$tests[] = [$node, $this->echoOrYield() . ' gettext("Hello");'];
$tests[] = [$node, 'yield gettext("Hello");'];

$body = new Node([
new TextNode('J\'ai ', 0),
Expand All @@ -173,7 +166,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
'yield strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -195,7 +188,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
'yield strtr(ngettext("Hey %%name%%, I have one apple", "Hey %%name%%, I have'
. ' %%count%% apples", abs(12)), array("%%name%%" => %s,'
. ' "%%name%%" => %s, "%%count%%" => abs(12), ));',
$this->getVariableGetter('name'),
Expand All @@ -217,7 +210,7 @@ public function getTests(): array
$tests[] = [
$node,
sprintf(
$this->echoOrYield() . ' strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
'yield strtr(gettext("J\'ai %%foo%% pommes"), array("%%foo%%" => %s, ));',
$this->getVariableGetter('foo'),
),
];
Expand All @@ -226,15 +219,15 @@ public function getTests(): array
$body = new ConstantExpression('Hello', 0);
$notes = new TextNode('Notes for translators', 0);
$node = new TransNode($body, null, null, null, $notes, null, 0);
$tests[] = [$node, "// notes: Notes for translators\n" . $this->echoOrYield() . ' gettext("Hello");'];
$tests[] = [$node, "// notes: Notes for translators\n" . 'yield gettext("Hello");'];

$body = new ConstantExpression('Hello', 0);
$notes = new TextNode("Notes for translators\nand line breaks", 0);
$node = new TransNode($body, null, null, null, $notes, null, 0);
$tests[] = [
$node,
"// notes: Notes for translators and line breaks\n"
. $this->echoOrYield() . ' gettext("Hello");',
. 'yield gettext("Hello");',
];

$count = new ConstantExpression(5, 0);
Expand All @@ -249,7 +242,7 @@ public function getTests(): array
$tests[] = [
$node,
'// notes: Notes for translators' . "\n"
. $this->echoOrYield() . ' strtr(ngettext("There is 1 pending task",'
. 'yield strtr(ngettext("There is 1 pending task",'
. ' "There are %count% pending tasks", abs(5)), array("%count%" => abs(5), ));',
];

Expand Down

0 comments on commit c600af9

Please sign in to comment.