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

Skip to content

Commit 13ae2bc

Browse files
committed
Compute constraint names list in a compiler pass
1 parent 4250860 commit 13ae2bc

File tree

7 files changed

+30
-94
lines changed

7 files changed

+30
-94
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ CHANGELOG
44
6.2
55
---
66

7-
* Deprecate `PhpExtractor` in favor of `PhpAstExtractor`
8-
* Add `PhpAstExtractor` (which require [nikic/php-parser](https://github.com/nikic/php-parser) to be installed)
7+
* Deprecate `PhpExtractor` in favor of `PhpAstExtractor`
8+
* Add `PhpAstExtractor` (requires [nikic/php-parser](https://github.com/nikic/php-parser) to be installed)
99

1010
6.1
1111
---

src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,16 @@ public function process(ContainerBuilder $container)
7070
$definition->replaceArgument(7, $paths);
7171
}
7272
}
73+
74+
if ($container->hasDefinition('validator') && $container->hasDefinition('translation.extractor.visitor.constraint')) {
75+
$definition = $container->findDefinition('translation.extractor.visitor.constraint');
76+
$constraintClassNames = [];
77+
78+
foreach (array_keys($container->findTaggedServiceIds('validator.constraint_validator', true)) as $serviceId) {
79+
$constraintClassNames[] = str_replace('Validator', '', $serviceId);
80+
}
81+
82+
$definition->setArgument(0, $constraintClassNames);
83+
}
7384
}
7485
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* PhpExtractor extracts translation messages from a PHP template.
2121
*
2222
* @author Michel Salib <[email protected]>
23+
* @deprecated since Symfony 6.2, use the PhpAstExtractor instead
2324
*/
2425
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
2526
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author Mathieu Santostefano <[email protected]>
1919
*/
20-
abstract class AbstractVisitor implements Visitor
20+
abstract class AbstractVisitor
2121
{
2222
protected MessageCatalogue $catalogue;
2323
protected \SplFileInfo $file;

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

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,10 @@
2121
*/
2222
final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
2323
{
24-
public const CONSTRAINT_CLASS_NAMES = [
25-
'AbstractComparison',
26-
'All',
27-
'Bic',
28-
'Blank',
29-
'Callback',
30-
'CardScheme',
31-
'Choice',
32-
'Collection',
33-
'Composite',
34-
'Count',
35-
'Country',
36-
'Currency',
37-
'Date',
38-
'DateTime',
39-
'DisableAutoMapping',
40-
'DivisibleBy',
41-
'Email',
42-
'EnableAutoMapping',
43-
'EqualTo',
44-
'Existence',
45-
'Expression',
46-
'File',
47-
'GreaterThan',
48-
'GreaterThanOrEqual',
49-
'GroupSequence',
50-
'GroupSequenceProvider',
51-
'Iban',
52-
'IdenticalTo',
53-
'Image',
54-
'Ip',
55-
'Isbn',
56-
'IsFalse',
57-
'IsNull',
58-
'Issn',
59-
'IsTrue',
60-
'Json',
61-
'Language',
62-
'Length',
63-
'LessThan',
64-
'LessThanOrEqual',
65-
'Locale',
66-
'Luhn',
67-
'Negative',
68-
'NegativeOrZero',
69-
'NotBlank',
70-
'NotCompromisedPassword',
71-
'NotEqualTo',
72-
'NotIdenticalTo',
73-
'NotNull',
74-
'NumberConstraintTrait',
75-
'Optional',
76-
'Positive',
77-
'PositiveOrZero',
78-
'Range',
79-
'Regex',
80-
'Required',
81-
'Time',
82-
'Timezone',
83-
'Traverse',
84-
'Type',
85-
'Unique',
86-
'Url',
87-
'Uuid',
88-
'Valid',
89-
];
24+
public function __construct(
25+
private readonly array $constraintClassNames = []
26+
) {
27+
}
9028

9129
public function beforeTraverse(array $nodes): ?Node
9230
{
@@ -108,7 +46,7 @@ public function enterNode(Node $node): ?Node
10846
$isConstraintClass = false;
10947

11048
foreach ($parts as $part) {
111-
if (\in_array($part, self::CONSTRAINT_CLASS_NAMES, true)) {
49+
if (\in_array($part, $this->constraintClassNames, true)) {
11250
$isConstraintClass = true;
11351

11452
break;

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public function testExtraction(iterable|string $resource)
2828
$extractor = new PhpAstExtractor([
2929
new TransMethodVisitor(),
3030
new TranslatableMessageVisitor(),
31-
new ConstraintVisitor(),
31+
new ConstraintVisitor([
32+
'NotBlank',
33+
'Isbn',
34+
'Length',
35+
]),
3236
]);
3337
$extractor->setPrefix('prefix');
3438
$catalogue = new MessageCatalogue('en');
@@ -150,7 +154,11 @@ public function testExtractionFromIndentedHeredocNowdoc()
150154
$extractor = new PhpAstExtractor([
151155
new TransMethodVisitor(),
152156
new TranslatableMessageVisitor(),
153-
new ConstraintVisitor(),
157+
new ConstraintVisitor([
158+
'NotBlank',
159+
'Isbn',
160+
'Length',
161+
]),
154162
]);
155163
$extractor->setPrefix('prefix');
156164
$extractor->extract(__DIR__.'/../fixtures/extractor-7.3/translation.html.php', $catalogue);

0 commit comments

Comments
 (0)