2020use Twig \Node \Expression \ConstantExpression ;
2121use Twig \Node \Expression \FilterExpression ;
2222use Twig \Node \Expression \NameExpression ;
23- use Twig \Node \Expression \Variable \AssignContextVariable ;
24- use Twig \Node \Expression \Variable \ContextVariable ;
23+ use Twig \Node \Expression \Variable \LocalVariable ;
2524use Twig \Node \ModuleNode ;
2625use Twig \Node \Node ;
2726use Twig \Node \Nodes ;
3332 */
3433final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
3534{
36- private const INTERNAL_VAR_NAME = '__internal_trans_default_domain ' ;
37-
3835 private Scope $ scope ;
36+ private int $ nestingLevel = 0 ;
3937
4038 public function __construct ()
4139 {
@@ -49,19 +47,25 @@ public function enterNode(Node $node, Environment $env): Node
4947 }
5048
5149 if ($ node instanceof TransDefaultDomainNode) {
50+ ++$ this ->nestingLevel ;
51+
5252 if ($ node ->getNode ('expr ' ) instanceof ConstantExpression) {
5353 $ this ->scope ->set ('domain ' , $ node ->getNode ('expr ' ));
5454
5555 return $ node ;
5656 }
5757
58- $ name = class_exists (AssignContextVariable::class) ? new AssignContextVariable (self ::INTERNAL_VAR_NAME , $ node ->getTemplateLine ()) : new AssignNameExpression (self ::INTERNAL_VAR_NAME , $ node ->getTemplateLine ());
59- $ this ->scope ->set ('domain ' , class_exists (ContextVariable::class) ? new ContextVariable (self ::INTERNAL_VAR_NAME , $ node ->getTemplateLine ()) : new NameExpression (self ::INTERNAL_VAR_NAME , $ node ->getTemplateLine ()));
60-
6158 if (class_exists (Nodes::class)) {
59+ $ name = new LocalVariable (null , $ node ->getTemplateLine ());
60+ $ this ->scope ->set ('domain ' , $ name );
61+
6262 return new SetNode (false , new Nodes ([$ name ]), new Nodes ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
6363 }
6464
65+ $ var = '__internal_trans_default_domain_ ' .$ this ->nestingLevel ;
66+ $ name = new AssignNameExpression ($ var , $ node ->getTemplateLine ());
67+ $ this ->scope ->set ('domain ' , new NameExpression ($ var , $ node ->getTemplateLine ()));
68+
6569 return new SetNode (false , new Node ([$ name ]), new Node ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
6670 }
6771
@@ -94,6 +98,8 @@ public function enterNode(Node $node, Environment $env): Node
9498 public function leaveNode (Node $ node , Environment $ env ): ?Node
9599 {
96100 if ($ node instanceof TransDefaultDomainNode) {
101+ --$ this ->nestingLevel ;
102+
97103 return null ;
98104 }
99105
0 commit comments