20
20
use Twig \Node \Expression \ConstantExpression ;
21
21
use Twig \Node \Expression \FilterExpression ;
22
22
use 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 ;
25
24
use Twig \Node \ModuleNode ;
26
25
use Twig \Node \Node ;
27
26
use Twig \Node \Nodes ;
33
32
*/
34
33
final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
35
34
{
36
- private const INTERNAL_VAR_NAME = '__internal_trans_default_domain ' ;
37
-
38
35
private Scope $ scope ;
36
+ private int $ nestingLevel = 0 ;
39
37
40
38
public function __construct ()
41
39
{
@@ -49,19 +47,25 @@ public function enterNode(Node $node, Environment $env): Node
49
47
}
50
48
51
49
if ($ node instanceof TransDefaultDomainNode) {
50
+ ++$ this ->nestingLevel ;
51
+
52
52
if ($ node ->getNode ('expr ' ) instanceof ConstantExpression) {
53
53
$ this ->scope ->set ('domain ' , $ node ->getNode ('expr ' ));
54
54
55
55
return $ node ;
56
56
}
57
57
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
-
61
58
if (class_exists (Nodes::class)) {
59
+ $ name = new LocalVariable (null , $ node ->getTemplateLine ());
60
+ $ this ->scope ->set ('domain ' , $ name );
61
+
62
62
return new SetNode (false , new Nodes ([$ name ]), new Nodes ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
63
63
}
64
64
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
+
65
69
return new SetNode (false , new Node ([$ name ]), new Node ([$ node ->getNode ('expr ' )]), $ node ->getTemplateLine ());
66
70
}
67
71
@@ -94,6 +98,8 @@ public function enterNode(Node $node, Environment $env): Node
94
98
public function leaveNode (Node $ node , Environment $ env ): ?Node
95
99
{
96
100
if ($ node instanceof TransDefaultDomainNode) {
101
+ --$ this ->nestingLevel ;
102
+
97
103
return null ;
98
104
}
99
105
0 commit comments