Skip to content

Commit 6df0cde

Browse files
bug #20830 [FrameworkBundle] Fix validation cache warmer with failing or missing classes (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [FrameworkBundle] Fix validation cache warmer with failing or missing classes | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20739 | License | MIT | Doc PR | - Commits ------- 53234e9 [FrameworkBundle] Fix validation cache warmer with failing or missing classes
2 parents 6d3c2a6 + e071574 commit 6df0cde

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

CacheWarmer/ValidatorCacheWarmer.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,23 @@ public function warmUp($cacheDir)
6666
$loaders = $this->validatorBuilder->getLoaders();
6767
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), new Psr6Cache($arrayPool));
6868

69-
foreach ($this->extractSupportedLoaders($loaders) as $loader) {
70-
foreach ($loader->getMappedClasses() as $mappedClass) {
71-
if ($metadataFactory->hasMetadataFor($mappedClass)) {
72-
$metadataFactory->getMetadataFor($mappedClass);
69+
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
70+
spl_autoload_register($throwingAutoloader);
71+
72+
try {
73+
foreach ($this->extractSupportedLoaders($loaders) as $loader) {
74+
foreach ($loader->getMappedClasses() as $mappedClass) {
75+
try {
76+
if ($metadataFactory->hasMetadataFor($mappedClass)) {
77+
$metadataFactory->getMetadataFor($mappedClass);
78+
}
79+
} catch (\ReflectionException $e) {
80+
// ignore failing reflection
81+
}
7382
}
7483
}
84+
} finally {
85+
spl_autoload_unregister($throwingAutoloader);
7586
}
7687

7788
$values = $arrayPool->getValues();

Tests/Fixtures/Validation/Article.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;
4+
5+
class Article implements NotExistingInterface
6+
{
7+
public $category;
8+
}

Tests/Fixtures/Validation/Resources/person.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</property>
1717
</class>
1818

19-
<class name="Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\NonExistentClass">
20-
<property name="gender">
19+
<class name="Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Article">
20+
<property name="category">
2121
<constraint name="Choice">
2222
<option name="choices">
2323
<value>other</value>

0 commit comments

Comments
 (0)