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 TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
35
36
{
36
37
private Scope $ scope ;
37
- private int $ nestingLevel = 0 ;
38
+ private int $ nameCounter = 0 ;
38
39
39
40
public function __construct ()
40
41
{
@@ -48,22 +49,27 @@ public function enterNode(Node $node, Environment $env): Node
48
49
}
49
50
50
51
if ($ node instanceof TransDefaultDomainNode) {
51
- ++$ this ->nestingLevel ;
52
-
53
52
if ($ node ->getNode ('expr ' ) instanceof ConstantExpression) {
54
53
$ this ->scope ->set ('domain ' , $ node ->getNode ('expr ' ));
55
54
56
55
return $ node ;
57
56
}
58
57
58
+ $ var = '__internal_trans_default_domain ' ;
59
+
60
+ if (null !== $ templateName = $ node ->getTemplateName ()) {
61
+ $ var .= hash ('xxh128 ' , $ templateName );
62
+ } else {
63
+ $ var .= ++$ this ->nameCounter ;
64
+ }
65
+
59
66
if (class_exists (Nodes::class)) {
60
- $ name = new LocalVariable ( null , $ node ->getTemplateLine ());
61
- $ this ->scope ->set ('domain ' , $ name );
67
+ $ name = new AssignContextVariable ( $ var , $ node ->getTemplateLine ());
68
+ $ this ->scope ->set ('domain ' , new ContextVariable ( $ var , $ node -> getTemplateLine ()) );
62
69
63
70
return new SetNode (false , new Nodes ([$ name ]), new Nodes ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
64
71
}
65
72
66
- $ var = '__internal_trans_default_domain_ ' .$ this ->nestingLevel ;
67
73
$ name = new AssignNameExpression ($ var , $ node ->getTemplateLine ());
68
74
$ this ->scope ->set ('domain ' , new NameExpression ($ var , $ node ->getTemplateLine ()));
69
75
@@ -105,8 +111,6 @@ public function enterNode(Node $node, Environment $env): Node
105
111
public function leaveNode (Node $ node , Environment $ env ): ?Node
106
112
{
107
113
if ($ node instanceof TransDefaultDomainNode) {
108
- --$ this ->nestingLevel ;
109
-
110
114
return null ;
111
115
}
112
116
0 commit comments