21
21
use Twig \Node \Expression \ConstantExpression ;
22
22
use Twig \Node \Expression \FilterExpression ;
23
23
use 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 ;
25
26
use Twig \Node \ModuleNode ;
26
27
use Twig \Node \Node ;
27
28
use Twig \Node \Nodes ;
34
35
final class TranslationDefaultDomainNodeVisitorimplements NodeVisitorInterface
35
36
{
36
37
private Scope $ scope ;
37
- private int $ nestingLevel =0 ;
38
38
39
39
public function __construct ()
40
40
{
@@ -48,22 +48,25 @@ public function enterNode(Node $node, Environment $env): Node
48
48
}
49
49
50
50
if ($ nodeinstanceof TransDefaultDomainNode) {
51
- ++$ this ->nestingLevel ;
52
-
53
51
if ($ node ->getNode ('expr ' )instanceof ConstantExpression) {
54
52
$ this ->scope ->set ('domain ' ,$ node ->getNode ('expr ' ));
55
53
56
54
return $ node ;
57
55
}
58
56
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
+
59
63
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 ()) );
62
66
63
67
return new SetNode (false ,new Nodes ([$ name ]),new Nodes ([$ node ->getNode ('expr ' )]),$ node ->getTemplateLine ());
64
68
}
65
69
66
- $ var ='__internal_trans_default_domain_ ' .$ this ->nestingLevel ;
67
70
$ name =new AssignNameExpression ($ var ,$ node ->getTemplateLine ());
68
71
$ this ->scope ->set ('domain ' ,new NameExpression ($ var ,$ node ->getTemplateLine ()));
69
72
@@ -105,8 +108,6 @@ public function enterNode(Node $node, Environment $env): Node
105
108
public function leaveNode (Node $ node ,Environment $ env ): ?Node
106
109
{
107
110
if ($ nodeinstanceof TransDefaultDomainNode) {
108
- --$ this ->nestingLevel ;
109
-
110
111
return null ;
111
112
}
112
113