File tree 7 files changed +39
-15
lines changed
src/Symfony/Component/Translation
7 files changed +39
-15
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,11 @@ public function extract(iterable|string $resource, MessageCatalogue $catalogue):
46
46
{
47
47
foreach ($ this ->extractFiles ($ resource ) as $ file ) {
48
48
$ traverser = new NodeTraverser ();
49
+
50
+ // This is needed to resolve namespaces in class methods/constants.
51
+ $ nameResolver = new NodeVisitor \NameResolver ();
52
+ $ traverser ->addVisitor ($ nameResolver );
53
+
49
54
/** @var AbstractVisitor&NodeVisitor $visitor */
50
55
foreach ($ this ->visitors as $ visitor ) {
51
56
$ visitor ->initialize ($ catalogue , $ file , $ this ->prefix );
Original file line number Diff line number Diff line change @@ -119,6 +119,17 @@ private function getStringValue(Node $node): ?string
119
119
return $ node ->expr ->value ;
120
120
}
121
121
122
+ if ($ node instanceof Node \Expr \ClassConstFetch) {
123
+ try {
124
+ $ reflection = new \ReflectionClass ($ node ->class ->toString ());
125
+ $ constant = $ reflection ->getReflectionConstant ($ node ->name ->toString ());
126
+ if (false !== $ constant && \is_string ($ constant ->getValue ())) {
127
+ return $ constant ->getValue ();
128
+ }
129
+ } catch (\ReflectionException ) {
130
+ }
131
+ }
132
+
122
133
return null ;
123
134
}
124
135
}
Original file line number Diff line number Diff line change @@ -32,6 +32,11 @@ public function beforeTraverse(array $nodes): ?Node
32
32
}
33
33
34
34
public function enterNode (Node $ node ): ?Node
35
+ {
36
+ return null ;
37
+ }
38
+
39
+ public function leaveNode (Node $ node ): ?Node
35
40
{
36
41
if (!$ node instanceof Node \Expr \New_ && !$ node instanceof Node \Attribute) {
37
42
return null ;
@@ -100,11 +105,6 @@ public function enterNode(Node $node): ?Node
100
105
return null ;
101
106
}
102
107
103
- public function leaveNode (Node $ node ): ?Node
104
- {
105
- return null ;
106
- }
107
-
108
108
public function afterTraverse (array $ nodes ): ?Node
109
109
{
110
110
return null ;
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
25
25
}
26
26
27
27
public function enterNode (Node $ node ): ?Node
28
+ {
29
+ return null ;
30
+ }
31
+
32
+ public function leaveNode (Node $ node ): ?Node
28
33
{
29
34
if (!$ node instanceof Node \Expr \MethodCall && !$ node instanceof Node \Expr \FuncCall) {
30
35
return null ;
@@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
53
58
return null ;
54
59
}
55
60
56
- public function leaveNode (Node $ node ): ?Node
57
- {
58
- return null ;
59
- }
60
-
61
61
public function afterTraverse (array $ nodes ): ?Node
62
62
{
63
63
return null ;
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
25
25
}
26
26
27
27
public function enterNode (Node $ node ): ?Node
28
+ {
29
+ return null ;
30
+ }
31
+
32
+ public function leaveNode (Node $ node ): ?Node
28
33
{
29
34
if (!$ node instanceof Node \Expr \New_) {
30
35
return null ;
@@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
53
58
return null ;
54
59
}
55
60
56
- public function leaveNode (Node $ node ): ?Node
57
- {
58
- return null ;
59
- }
60
-
61
61
public function afterTraverse (array $ nodes ): ?Node
62
62
{
63
63
return null ;
Original file line number Diff line number Diff line change 20
20
21
21
final class PhpAstExtractorTest extends TestCase
22
22
{
23
+ public const OTHER_DOMAIN = 'not_messages ' ;
24
+
23
25
/**
24
26
* @dataProvider resourcesProvider
25
27
*/
@@ -124,6 +126,7 @@ public function testExtraction(iterable|string $resource)
124
126
'variable-assignation-inlined-with-named-arguments-in-trans-method ' => 'prefixvariable-assignation-inlined-with-named-arguments-in-trans-method ' ,
125
127
'mix-named-arguments-without-parameters ' => 'prefixmix-named-arguments-without-parameters ' ,
126
128
'mix-named-arguments-disordered ' => 'prefixmix-named-arguments-disordered ' ,
129
+ 'const-domain ' => 'prefixconst-domain ' ,
127
130
],
128
131
'validators ' => [
129
132
'message-in-constraint-attribute ' => 'prefixmessage-in-constraint-attribute ' ,
Original file line number Diff line number Diff line change 62
62
<?php echo $ view ['translator ' ]->trans ('mix-named-arguments-disordered ' , domain: 'not_messages ' , parameters: []); ?>
63
63
64
64
<?php echo $ view ['translator ' ]->trans (...); // should not fail ?>
65
+
66
+ <?php
67
+ use Symfony \Component
4347
span>\Translation \Tests \Extractor \PhpAstExtractorTest ;
68
+ echo $ view ['translator ' ]->trans ('const-domain ' , [], PhpAstExtractorTest::OTHER_DOMAIN );
69
+ ?>
You can’t perform that action at this time.
0 commit comments