Skip to content

Commit

Permalink
[TwigBridge] Fix compat with Twig v3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Feb 15, 2024
1 parent 2abddb1 commit 14a73aa
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Node/DumpNode.php
Expand Up @@ -11,12 +11,14 @@

namespace Symfony\Bridge\Twig\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Node;

/**
* @author Julien Galenski <julien.galenski@gmail.com>
*/
#[YieldReady]
final class DumpNode extends Node
{
private $varPrefix;
Expand Down
2 changes: 2 additions & 0 deletions Node/FormThemeNode.php
Expand Up @@ -12,12 +12,14 @@
namespace Symfony\Bridge\Twig\Node;

use Symfony\Component\Form\FormRenderer;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Node;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
#[YieldReady]
final class FormThemeNode extends Node
{
public function __construct(Node $form, Node $resources, int $lineno, ?string $tag = null, bool $only = false)
Expand Down
2 changes: 2 additions & 0 deletions Node/StopwatchNode.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Twig\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Node;
Expand All @@ -20,6 +21,7 @@
*
* @author Wouter J <wouter@wouterj.nl>
*/
#[YieldReady]
final class StopwatchNode extends Node
{
public function __construct(Node $name, Node $body, AssignNameExpression $var, int $lineno = 0, ?string $tag = null)
Expand Down
2 changes: 2 additions & 0 deletions Node/TransDefaultDomainNode.php
Expand Up @@ -11,13 +11,15 @@

namespace Symfony\Bridge\Twig\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Node;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
#[YieldReady]
final class TransDefaultDomainNode extends Node
{
public function __construct(AbstractExpression $expr, int $lineno = 0, ?string $tag = null)
Expand Down
5 changes: 4 additions & 1 deletion Node/TransNode.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Twig\Node;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ArrayExpression;
Expand All @@ -22,6 +23,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
#[YieldReady]
final class TransNode extends Node
{
public function __construct(Node $body, ?Node $domain = null, ?AbstractExpression $count = null, ?AbstractExpression $vars = null, ?AbstractExpression $locale = null, int $lineno = 0, ?string $tag = null)
Expand Down Expand Up @@ -53,9 +55,10 @@ public function compile(Compiler $compiler): void
$vars = null;
}
[$msg, $defaults] = $this->compileString($this->getNode('body'), $defaults, (bool) $vars);
$display = class_exists(YieldReady::class) ? 'yield' : 'echo';

$compiler
->write('echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->trans(')
->write($display.' $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->trans(')
->subcompile($msg)
;

Expand Down
4 changes: 3 additions & 1 deletion Tests/Node/TransNodeTest.php
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\TransNode;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
Expand All @@ -35,7 +36,8 @@ public function testCompileStrict()

$this->assertEquals(
sprintf(
'echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->trans("trans %%var%%", array_merge(["%%var%%" => %s], %s), "messages");',
'%s $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->trans("trans %%var%%", array_merge(["%%var%%" => %s], %s), "messages");',
class_exists(YieldReady::class) ? 'yield' : 'echo',
$this->getVariableGetterWithoutStrictCheck('var'),
$this->getVariableGetterWithStrictCheck('foo')
),
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.2.5",
"symfony/polyfill-php80": "^1.16",
"symfony/translation-contracts": "^1.1|^2|^3",
"twig/twig": "^2.13|~3.8.0"
"twig/twig": "^2.13|^3.0.4"
},
"require-dev": {
"doctrine/annotations": "^1.12|^2",
Expand Down

0 comments on commit 14a73aa

Please sign in to comment.