Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a357f58

Browse files
Fix constant domain resolution
1 parent 0294563 commit a357f58

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/Symfony/Component/Translation/Extractor/PhpAstExtractor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ public function extract(iterable|string $resource, MessageCatalogue $catalogue):
4646
{
4747
foreach ($this->extractFiles($resource) as $file) {
4848
$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+
4956
/** @var AbstractVisitor&NodeVisitor $visitor */
5057
foreach ($this->visitors as $visitor) {
5158
$visitor->initialize($catalogue, $file, $this->prefix);
5259
$traverser->addVisitor($visitor);
5360
}
5461

55-
$nodes = $this->parser->parse(file_get_contents($file));
62+
// Second run is used for the custom visitors.
5663
$traverser->traverse($nodes);
5764
}
5865
}

src/Symfony/Component/Translation/Extractor/Visitor/AbstractVisitor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ private function getStringValue(Node $node): ?string
119119
return $node->expr->value;
120120
}
121121

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+
122133
return null;
123134
}
124135
}

src/Symfony/Component/Translation/Tests/Extractor/PhpAstExtractorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
final class PhpAstExtractorTest extends TestCase
2222
{
23+
public const OTHER_DOMAIN = 'not_messages';
24+
2325
/**
2426
* @dataProvider resourcesProvider
2527
*/
@@ -124,6 +126,7 @@ public function testExtraction(iterable|string $resource)
124126
'variable-assignation-inlined-with-named-arguments-in-trans-method' => 'prefixvariable-assignation-inlined-with-named-arguments-in-trans-method',
125127
'mix-named-arguments-without-parameters' => 'prefixmix-named-arguments-without-parameters',
126128
'mix-named-arguments-disordered' => 'prefixmix-named-arguments-disordered',
129+
'const-domain' => 'prefixconst-domain',
127130
],
128131
'validators' => [
129132
'message-in-constraint-attribute' => 'prefixmessage-in-constraint-attribute',

src/Symfony/Component/Translation/Tests/Fixtures/extractor-ast/translation.html.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@
6262
<?php echo $view['translator']->trans('mix-named-arguments-disordered', domain: 'not_messages', parameters: []); ?>
6363

6464
<?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+
?>

0 commit comments

Comments
 (0)