File tree Expand file tree Collapse file tree 4 files changed +27
-1
lines changed
src/Symfony/Component/Translation Expand file tree Collapse file tree 4 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -46,13 +46,20 @@ 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
+ $ nodes = $ this ->parser ->parse (file_get_contents ($ file ));
51
+
52
+ // First run is needed to resolve namespaces in class methods/constants.
53
+ $ traverser ->addVisitor (new NodeVisitor \NameResolver ());
54
+ $ traverser ->traverse ($ nodes );
55
+
49
56
/** @var AbstractVisitor&NodeVisitor $visitor */
50
57
foreach ($ this ->visitors as $ visitor ) {
51
58
$ visitor ->initialize ($ catalogue , $ file , $ this ->prefix );
52
59
$ traverser ->addVisitor ($ visitor );
53
60
}
54
61
55
- $ nodes = $ this -> parser -> parse ( file_get_contents ( $ file ));
62
+ // Second run is used for the custom visitors.
56
63
$ traverser ->traverse ($ nodes );
57
64
}
58
65
}
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 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 \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