2121use Twig \Node \Expression \ConstantExpression ;
2222use Twig \Node \Expression \FilterExpression ;
2323use Twig \Node \Expression \NameExpression ;
24- use Twig \Node \Expression \Variable \LocalVariable ;
24+ use Twig \Node \Expression \Variable \AssignContextVariable ;
25+ use Twig \Node \Expression \Variable \ContextVariable ;
2526use Twig \Node \ModuleNode ;
2627use Twig \Node \Node ;
2728use Twig \Node \Nodes ;
3435final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
3536{
3637 private Scope $ scope ;
37- private int $ nestingLevel = 0 ;
3838
3939 public function __construct ()
4040 {
@@ -48,22 +48,25 @@ public function enterNode(Node $node, Environment $env): Node
4848 }
4949
5050 if ($ node instanceof TransDefaultDomainNode) {
51- ++$ this ->nestingLevel ;
52-
5351 if ($ node ->getNode ('expr ' ) instanceof ConstantExpression) {
5452 $ this ->scope ->set ('domain ' , $ node ->getNode ('expr ' ));
5553
5654 return $ node ;
5755 }
5856
57+ if (null === $ templateName = $ node ->getTemplateName ()) {
58+ throw new \LogicException ('Cannot traverse a node without a template name. ' );
59+ }
60+
61+ $ var = '__internal_trans_default_domain ' .hash ('xxh128 ' , $ templateName );
62+
5963 if (class_exists (Nodes::class)) {
60- $ name = new LocalVariable ( null , $ node ->getTemplateLine ());
61- $ this ->scope ->set ('domain ' , $ name );
64+ $ name = new AssignContextVariable ( $ var , $ node ->getTemplateLine ());
65+ $ this ->scope ->set ('domain ' , new ContextVariable ( $ var , $ node -> getTemplateLine ()) );
6266
6367 return new SetNode (false , new Nodes ([$ name ]), new Nodes ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
6468 }
6569
66- $ var = '__internal_trans_default_domain_ ' .$ this ->nestingLevel ;
6770 $ name = new AssignNameExpression ($ var , $ node ->getTemplateLine ());
6871 $ this ->scope ->set ('domain ' , new NameExpression ($ var , $ node ->getTemplateLine ()));
6972
@@ -105,8 +108,6 @@ public function enterNode(Node $node, Environment $env): Node
105108 public function leaveNode (Node $ node , Environment $ env ): ?Node
106109 {
107110 if ($ node instanceof TransDefaultDomainNode) {
108- --$ this ->nestingLevel ;
109-
110111 return null ;
111112 }
112113
0 commit comments