Skip to content

Commit 1d242be

Browse files
committed
[Validator] Allow load mappings from attributes without doctrine/annotations.
1 parent 02bcd6a commit 1d242be

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,11 +1303,14 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
13031303
$definition->replaceArgument(0, $config['email_validation_mode']);
13041304

13051305
if (\array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
1306-
if (!$this->annotationsConfigEnabled) {
1307-
throw new \LogicException('"enable_annotations" on the validator cannot be set as Annotations support is disabled.');
1306+
if (!$this->annotationsConfigEnabled && \PHP_VERSION_ID < 80000) {
1307+
throw new \LogicException('"enable_annotations" on the validator cannot be set as Doctrine Annotations support is disabled.');
13081308
}
13091309

1310-
$validatorBuilder->addMethodCall('enableAnnotationMapping', [new Reference('annotation_reader')]);
1310+
$validatorBuilder->addMethodCall('enableAnnotationMapping', [true]);
1311+
if ($this->annotationsConfigEnabled) {
1312+
$validatorBuilder->addMethodCall('setDoctrineAnnotationReader', [new Reference('annotation_reader')]);
1313+
}
13111314
}
13121315

13131316
if (\array_key_exists('static_method', $config) && $config['static_method']) {

Tests/CacheWarmer/ValidatorCacheWarmerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testWarmUp()
2626
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
2727
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
2828
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
29-
$validatorBuilder->enableAnnotationMapping();
29+
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();
3030

3131
$file = sys_get_temp_dir().'/cache-validator.php';
3232
@unlink($file);
@@ -46,7 +46,7 @@ public function testWarmUpWithAnnotations()
4646
{
4747
$validatorBuilder = new ValidatorBuilder();
4848
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
49-
$validatorBuilder->enableAnnotationMapping();
49+
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();
5050

5151
$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
5252
@unlink($file);

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ public function testValidation()
870870

871871
$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);
872872

873-
$this->assertCount($annotations ? 7 : 6, $calls);
873+
$this->assertCount($annotations ? 8 : 6, $calls);
874874
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
875875
$this->assertEquals([new Reference('validator.validator_factory')], $calls[0][1]);
876876
$this->assertSame('setTranslator', $calls[1][0]);
@@ -882,6 +882,7 @@ public function testValidation()
882882
$i = 3;
883883
if ($annotations) {
884884
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
885+
$this->assertSame('setDoctrineAnnotationReader', $calls[++$i][0]);
885886
}
886887
$this->assertSame('addMethodMapping', $calls[++$i][0]);
887888
$this->assertSame(['loadValidatorMetadata'], $calls[$i][1]);
@@ -923,13 +924,14 @@ public function testValidationAnnotations()
923924

924925
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
925926

926-
$this->assertCount(7, $calls);
927+
$this->assertCount(8, $calls);
927928
$this->assertSame('enableAnnotationMapping', $calls[4][0]);
928-
$this->assertEquals([new Reference('annotation_reader')], $calls[4][1]);
929-
$this->assertSame('addMethodMapping', $calls[5][0]);
930-
$this->assertSame(['loadValidatorMetadata'], $calls[5][1]);
931-
$this->assertSame('setMappingCache', $calls[6][0]);
932-
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[6][1]);
929+
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
930+
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
931+
$this->assertSame('addMethodMapping', $calls[6][0]);
932+
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
933+
$this->assertSame('setMappingCache', $calls[7][0]);
934+
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
933935
// no cache this time
934936
}
935937

@@ -944,14 +946,15 @@ public function testValidationPaths()
944946

945947
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
946948

947-
$this->assertCount(8, $calls);
949+
$this->assertCount(9, $calls);
948950
$this->assertSame('addXmlMappings', $calls[3][0]);
949951
$this->assertSame('addYamlMappings', $calls[4][0]);
950952
$this->assertSame('enableAnnotationMapping', $calls[5][0]);
951-
$this->assertSame('addMethodMapping', $calls[6][0]);
952-
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
953-
$this->assertSame('setMappingCache', $calls[7][0]);
954-
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
953+
$this->assertSame('setDoctrineAnnotationReader', $calls[6][0]);
954+
$this->assertSame('addMethodMapping', $calls[7][0]);
955+
$this->assertSame(['loadValidatorMetadata'], $calls[7][1]);
956+
$this->assertSame('setMappingCache', $calls[8][0]);
957+
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[8][1]);
955958

956959
$xmlMappings = $calls[3][1][0];
957960
$this->assertCount(3, $xmlMappings);
@@ -1004,11 +1007,12 @@ public function testValidationNoStaticMethod()
10041007

10051008
$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);
10061009

1007-
$this->assertCount($annotations ? 6 : 5, $calls);
1010+
$this->assertCount($annotations ? 7 : 5, $calls);
10081011
$this->assertSame('addXmlMappings', $calls[3][0]);
10091012
$i = 3;
10101013
if ($annotations) {
10111014
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
1015+
$this->assertSame('setDoctrineAnnotationReader', $calls[++$i][0]);
10121016
}
10131017
$this->assertSame('setMappingCache', $calls[++$i][0]);
10141018
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[$i][1]);

0 commit comments

Comments
 (0)