Skip to content

Commit 2ccb5ea

Browse files
[FrameworkBundle] Fix class_exists() checks in PhpArrayAdapter-related cache warmers
1 parent 20abebe commit 2ccb5ea

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
3636

3737
/**
3838
* @param Reader $annotationReader
39-
* @param string $phpArrayFile The PHP file where annotations are cached.
40-
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached.
39+
* @param string $phpArrayFile the PHP file where annotations are cached
40+
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered annotations are cached
4141
*/
4242
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4343
{
@@ -67,9 +67,8 @@ public function warmUp($cacheDir)
6767

6868
$arrayPool = new ArrayAdapter(0, false);
6969
$reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayPool));
70-
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
71-
spl_autoload_register($throwingAutoloader);
7270

71+
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
7372
try {
7473
foreach ($annotatedClasses as $class) {
7574
try {
@@ -88,7 +87,7 @@ public function warmUp($cacheDir)
8887
}
8988
}
9089
} finally {
91-
spl_autoload_unregister($throwingAutoloader);
90+
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
9291
}
9392

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

CacheWarmer/SerializerCacheWarmer.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14+
use Doctrine\Common\Annotations\AnnotationException;
1415
use Psr\Cache\CacheItemPoolInterface;
1516
use Symfony\Component\Cache\Adapter\AdapterInterface;
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -36,9 +37,9 @@ class SerializerCacheWarmer implements CacheWarmerInterface
3637
private $fallbackPool;
3738

3839
/**
39-
* @param LoaderInterface[] $loaders The serializer metadata loaders.
40-
* @param string $phpArrayFile The PHP file where metadata are cached.
41-
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached.
40+
* @param LoaderInterface[] $loaders the serializer metadata loaders
41+
* @param string $phpArrayFile the PHP file where metadata are cached
42+
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
4243
*/
4344
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4445
{
@@ -64,10 +65,21 @@ public function warmUp($cacheDir)
6465

6566
$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayPool);
6667

67-
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
68-
foreach ($loader->getMappedClasses() as $mappedClass) {
69-
$metadataFactory->getMetadataFor($mappedClass);
68+
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
69+
try {
70+
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
71+
foreach ($loader->getMappedClasses() as $mappedClass) {
72+
try {
73+
$metadataFactory->getMetadataFor($mappedClass);
74+
} catch (\ReflectionException $e) {
75+
// ignore failing reflection
76+
} catch (AnnotationException $e) {
77+
// ignore failing annotations
78+
}
79+
}
7080
}
81+
} finally {
82+
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
7183
}
7284

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

CacheWarmer/ValidatorCacheWarmer.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14+
use Doctrine\Common\Annotations\AnnotationException;
1415
use Psr\Cache\CacheItemPoolInterface;
1516
use Symfony\Component\Cache\Adapter\AdapterInterface;
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -38,8 +39,8 @@ class ValidatorCacheWarmer implements CacheWarmerInterface
3839

3940
/**
4041
* @param ValidatorBuilderInterface $validatorBuilder
41-
* @param string $phpArrayFile The PHP file where metadata are cached.
42-
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached.
42+
* @param string $phpArrayFile the PHP file where metadata are cached
43+
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
4344
*/
4445
public function __construct(ValidatorBuilderInterface $validatorBuilder, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4546
{
@@ -66,9 +67,7 @@ public function warmUp($cacheDir)
6667
$loaders = $this->validatorBuilder->getLoaders();
6768
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), new Psr6Cache($arrayPool));
6869

69-
$throwingAutoloader = function ($class) { throw new \ReflectionException(sprintf('Class %s does not exist', $class)); };
70-
spl_autoload_register($throwingAutoloader);
71-
70+
spl_autoload_register(array($adapter, 'throwOnRequiredClass'));
7271
try {
7372
foreach ($this->extractSupportedLoaders($loaders) as $loader) {
7473
foreach ($loader->getMappedClasses() as $mappedClass) {
@@ -78,15 +77,17 @@ public function warmUp($cacheDir)
7877
}
7978
} catch (\ReflectionException $e) {
8079
// ignore failing reflection
80+
} catch (AnnotationException $e) {
81+
// ignore failing annotations
8182
}
8283
}
8384
}
8485
} finally {
85-
spl_autoload_unregister($throwingAutoloader);
86+
spl_autoload_unregister(array($adapter, 'throwOnRequiredClass'));
8687
}
8788

8889
$values = $arrayPool->getValues();
89-
$adapter->warmUp($values);
90+
$adapter->warmUp(array_filter($values));
9091

9192
foreach ($values as $k => $v) {
9293
$item = $this->fallbackPool->getItem($k);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.5.9",
20-
"symfony/cache": "~3.2",
20+
"symfony/cache": "~3.2.2|~3.3",
2121
"symfony/class-loader": "~3.2",
2222
"symfony/dependency-injection": "~3.2.1|~3.3",
2323
"symfony/config": "~2.8|~3.0",

0 commit comments

Comments
 (0)