Merge branch '4.3' into 4.4
* 4.3: [FrameworkBundle][Config] Ignore exeptions thrown during reflection classes autoload
This commit is contained in:
commit
f4f2fda81f
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
|||||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||||
use Symfony\Component\Cache\Adapter\NullAdapter;
|
use Symfony\Component\Cache\Adapter\NullAdapter;
|
||||||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
|
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
|
||||||
|
use Symfony\Component\Config\Resource\ClassExistenceResource;
|
||||||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,13 +47,13 @@ abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
|
|||||||
{
|
{
|
||||||
$arrayAdapter = new ArrayAdapter();
|
$arrayAdapter = new ArrayAdapter();
|
||||||
|
|
||||||
spl_autoload_register([PhpArrayAdapter::class, 'throwOnRequiredClass']);
|
spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
|
||||||
try {
|
try {
|
||||||
if (!$this->doWarmUp($cacheDir, $arrayAdapter)) {
|
if (!$this->doWarmUp($cacheDir, $arrayAdapter)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
spl_autoload_unregister([PhpArrayAdapter::class, 'throwOnRequiredClass']);
|
spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the ArrayAdapter stores the values serialized
|
// the ArrayAdapter stores the values serialized
|
||||||
@ -68,6 +69,17 @@ abstract class AbstractPhpFileCacheWarmer implements CacheWarmerInterface
|
|||||||
$phpArrayAdapter->warmUp($values);
|
$phpArrayAdapter->warmUp($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final protected function ignoreAutoloadException($class, \Exception $exception)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
ClassExistenceResource::throwOnRequiredClass($class, $exception);
|
||||||
|
} catch (\ReflectionException $e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $cacheDir
|
* @param string $cacheDir
|
||||||
* @param ArrayAdapter $arrayAdapter
|
* @param ArrayAdapter $arrayAdapter
|
||||||
|
@ -68,17 +68,8 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$this->readAllComponents($reader, $class);
|
$this->readAllComponents($reader, $class);
|
||||||
} catch (\ReflectionException $e) {
|
} catch (\Exception $e) {
|
||||||
// ignore failing reflection
|
$this->ignoreAutoloadException($class, $e);
|
||||||
} catch (AnnotationException $e) {
|
|
||||||
/*
|
|
||||||
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
|
|
||||||
* configured or could not be found / read / etc.
|
|
||||||
*
|
|
||||||
* In particular cases, an Annotation in your code can be used and defined only for a specific
|
|
||||||
* environment but is always added to the annotations.map file by some Symfony default behaviors,
|
|
||||||
* and you always end up with a not found Annotation.
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,14 +79,32 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
|
|||||||
private function readAllComponents(Reader $reader, string $class)
|
private function readAllComponents(Reader $reader, string $class)
|
||||||
{
|
{
|
||||||
$reflectionClass = new \ReflectionClass($class);
|
$reflectionClass = new \ReflectionClass($class);
|
||||||
$reader->getClassAnnotations($reflectionClass);
|
|
||||||
|
try {
|
||||||
|
$reader->getClassAnnotations($reflectionClass);
|
||||||
|
} catch (AnnotationException $e) {
|
||||||
|
/*
|
||||||
|
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
|
||||||
|
* configured or could not be found / read / etc.
|
||||||
|
*
|
||||||
|
* In particular cases, an Annotation in your code can be used and defined only for a specific
|
||||||
|
* environment but is always added to the annotations.map file by some Symfony default behaviors,
|
||||||
|
* and you always end up with a not found Annotation.
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
|
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
|
||||||
$reader->getMethodAnnotations($reflectionMethod);
|
try {
|
||||||
|
$reader->getMethodAnnotations($reflectionMethod);
|
||||||
|
} catch (AnnotationException $e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
|
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
|
||||||
$reader->getPropertyAnnotations($reflectionProperty);
|
try {
|
||||||
|
$reader->getPropertyAnnotations($reflectionProperty);
|
||||||
|
} catch (AnnotationException $e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,10 +58,10 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
|
|||||||
foreach ($loader->getMappedClasses() as $mappedClass) {
|
foreach ($loader->getMappedClasses() as $mappedClass) {
|
||||||
try {
|
try {
|
||||||
$metadataFactory->getMetadataFor($mappedClass);
|
$metadataFactory->getMetadataFor($mappedClass);
|
||||||
} catch (\ReflectionException $e) {
|
|
||||||
// ignore failing reflection
|
|
||||||
} catch (AnnotationException $e) {
|
} catch (AnnotationException $e) {
|
||||||
// ignore failing annotations
|
// ignore failing annotations
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->ignoreAutoloadException($mappedClass, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,10 +67,10 @@ class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
|
|||||||
if ($metadataFactory->hasMetadataFor($mappedClass)) {
|
if ($metadataFactory->hasMetadataFor($mappedClass)) {
|
||||||
$metadataFactory->getMetadataFor($mappedClass);
|
$metadataFactory->getMetadataFor($mappedClass);
|
||||||
}
|
}
|
||||||
} catch (\ReflectionException $e) {
|
|
||||||
// ignore failing reflection
|
|
||||||
} catch (AnnotationException $e) {
|
} catch (AnnotationException $e) {
|
||||||
// ignore failing annotations
|
// ignore failing annotations
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->ignoreAutoloadException($mappedClass, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,54 @@ class AnnotationsCacheWarmerTest extends TestCase
|
|||||||
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
|
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is not broken if a class loader
|
||||||
|
* throws an exception (on class / file not found for example).
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadException()
|
||||||
|
{
|
||||||
|
$this->assertFalse(class_exists($annotatedClass = 'C\C\C', false));
|
||||||
|
|
||||||
|
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export([$annotatedClass], true)));
|
||||||
|
$warmer = new AnnotationsCacheWarmer(new AnnotationReader(), tempnam($this->cacheDir, __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classLoader = function ($class) use ($annotatedClass) {
|
||||||
|
if ($class === $annotatedClass) {
|
||||||
|
throw new \DomainException('This exception should be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp($this->cacheDir);
|
||||||
|
|
||||||
|
spl_autoload_unregister($classLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is broken if a class loader throws an
|
||||||
|
* exception but that is unrelated to the class load.
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadExceptionWithUnrelatedException()
|
||||||
|
{
|
||||||
|
$this->expectException(\DomainException::class);
|
||||||
|
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
|
||||||
|
|
||||||
|
$this->assertFalse(class_exists($annotatedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_AnnotationsCacheWarmerTest', false));
|
||||||
|
|
||||||
|
file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export([$annotatedClass], true)));
|
||||||
|
$warmer = new AnnotationsCacheWarmer(new AnnotationReader(), tempnam($this->cacheDir, __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classLoader = function ($class) use ($annotatedClass) {
|
||||||
|
if ($class === $annotatedClass) {
|
||||||
|
eval('class '.$annotatedClass.'{}');
|
||||||
|
throw new \DomainException('This exception should not be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp($this->cacheDir);
|
||||||
|
|
||||||
|
spl_autoload_unregister($classLoader);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return MockObject|Reader
|
* @return MockObject|Reader
|
||||||
*/
|
*/
|
||||||
|
@ -60,4 +60,58 @@ class SerializerCacheWarmerTest extends TestCase
|
|||||||
|
|
||||||
$this->assertFileExists($file);
|
$this->assertFileExists($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is not broken if a class loader
|
||||||
|
* throws an exception (on class / file not found for example).
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadException()
|
||||||
|
{
|
||||||
|
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) {
|
||||||
|
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
|
||||||
|
|
||||||
|
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
|
||||||
|
if ($class === $mappedClass) {
|
||||||
|
throw new \DomainException('This exception should be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp('foo');
|
||||||
|
|
||||||
|
spl_autoload_unregister($classLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is broken if a class loader throws an
|
||||||
|
* exception but that is unrelated to the class load.
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadExceptionWithUnrelatedException()
|
||||||
|
{
|
||||||
|
$this->expectException(\DomainException::class);
|
||||||
|
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
|
||||||
|
|
||||||
|
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) {
|
||||||
|
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
|
||||||
|
|
||||||
|
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
|
||||||
|
if ($class === $mappedClass) {
|
||||||
|
eval('class '.$mappedClass.'{}');
|
||||||
|
throw new \DomainException('This exception should not be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp('foo');
|
||||||
|
|
||||||
|
spl_autoload_unregister($classLoader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,4 +81,54 @@ class ValidatorCacheWarmerTest extends TestCase
|
|||||||
|
|
||||||
$this->assertFileExists($file);
|
$this->assertFileExists($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is not broken if a class loader
|
||||||
|
* throws an exception (on class / file not found for example).
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadException()
|
||||||
|
{
|
||||||
|
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false));
|
||||||
|
|
||||||
|
$validatorBuilder = new ValidatorBuilder();
|
||||||
|
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
|
||||||
|
$warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classloader = function ($class) use ($mappedClass) {
|
||||||
|
if ($class === $mappedClass) {
|
||||||
|
throw new \DomainException('This exception should be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp('foo');
|
||||||
|
|
||||||
|
spl_autoload_unregister($classloader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the cache warming process is broken if a class loader throws an
|
||||||
|
* exception but that is unrelated to the class load.
|
||||||
|
*/
|
||||||
|
public function testClassAutoloadExceptionWithUnrelatedException()
|
||||||
|
{
|
||||||
|
$this->expectException(\DomainException::class);
|
||||||
|
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
|
||||||
|
|
||||||
|
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false));
|
||||||
|
|
||||||
|
$validatorBuilder = new ValidatorBuilder();
|
||||||
|
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
|
||||||
|
$warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__), new ArrayAdapter());
|
||||||
|
|
||||||
|
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
|
||||||
|
if ($class === $mappedClass) {
|
||||||
|
eval('class '.$mappedClass.'{}');
|
||||||
|
throw new \DomainException('This exception should not be caught by the warmer.');
|
||||||
|
}
|
||||||
|
}, true, true);
|
||||||
|
|
||||||
|
$warmer->warmUp('foo');
|
||||||
|
|
||||||
|
spl_autoload_unregister($classLoader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest: ~
|
@ -0,0 +1 @@
|
|||||||
|
AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest: ~
|
@ -19,7 +19,7 @@
|
|||||||
"php": "^7.1.3",
|
"php": "^7.1.3",
|
||||||
"ext-xml": "*",
|
"ext-xml": "*",
|
||||||
"symfony/cache": "^4.4|^5.0",
|
"symfony/cache": "^4.4|^5.0",
|
||||||
"symfony/config": "^4.2|^5.0",
|
"symfony/config": "^4.3.4|^5.0",
|
||||||
"symfony/dependency-injection": "^4.4|^5.0",
|
"symfony/dependency-injection": "^4.4|^5.0",
|
||||||
"symfony/error-renderer": "^4.4|^5.0",
|
"symfony/error-renderer": "^4.4|^5.0",
|
||||||
"symfony/http-foundation": "^4.3|^5.0",
|
"symfony/http-foundation": "^4.3|^5.0",
|
||||||
|
@ -288,7 +288,7 @@ class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInte
|
|||||||
/**
|
/**
|
||||||
* @throws \ReflectionException When $class is not found and is required
|
* @throws \ReflectionException When $class is not found and is required
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal to be removed in Symfony 5.0
|
||||||
*/
|
*/
|
||||||
public static function throwOnRequiredClass($class)
|
public static function throwOnRequiredClass($class)
|
||||||
{
|
{
|
||||||
|
@ -76,10 +76,14 @@ class ClassExistenceResource implements SelfCheckingResourceInterface
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$exists = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
|
$exists = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
|
||||||
} catch (\ReflectionException $e) {
|
} catch (\Exception $e) {
|
||||||
if (0 >= $timestamp) {
|
try {
|
||||||
unset(self::$existsCache[1][$this->resource]);
|
self::throwOnRequiredClass($this->resource, $e);
|
||||||
throw $e;
|
} catch (\ReflectionException $e) {
|
||||||
|
if (0 >= $timestamp) {
|
||||||
|
unset(self::$existsCache[1][$this->resource]);
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
self::$autoloadedClass = $autoloadedClass;
|
self::$autoloadedClass = $autoloadedClass;
|
||||||
@ -109,24 +113,57 @@ class ClassExistenceResource implements SelfCheckingResourceInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \ReflectionException When $class is not found and is required
|
* Throws a reflection exception when the passed class does not exist but is required.
|
||||||
|
*
|
||||||
|
* A class is considered "not required" when it's loaded as part of a "class_exists" or similar check.
|
||||||
|
*
|
||||||
|
* This function can be used as an autoload function to throw a reflection
|
||||||
|
* exception if the class was not found by previous autoload functions.
|
||||||
|
*
|
||||||
|
* A previous exception can be passed. In this case, the class is considered as being
|
||||||
|
* required totally, so if it doesn't exist, a reflection exception is always thrown.
|
||||||
|
* If it exists, the previous exception is rethrown.
|
||||||
|
*
|
||||||
|
* @throws \ReflectionException
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public static function throwOnRequiredClass($class)
|
public static function throwOnRequiredClass($class, \Exception $previous = null)
|
||||||
{
|
{
|
||||||
if (self::$autoloadedClass === $class) {
|
// If the passed class is the resource being checked, we shouldn't throw.
|
||||||
|
if (null === $previous && self::$autoloadedClass === $class) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$e = new \ReflectionException("Class $class not found");
|
|
||||||
|
if (class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) {
|
||||||
|
if (null !== $previous) {
|
||||||
|
throw $previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($previous instanceof \ReflectionException) {
|
||||||
|
throw $previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
$e = new \ReflectionException("Class $class not found", 0, $previous);
|
||||||
|
|
||||||
|
if (null !== $previous) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
$trace = $e->getTrace();
|
$trace = $e->getTrace();
|
||||||
$autoloadFrame = [
|
$autoloadFrame = [
|
||||||
'function' => 'spl_autoload_call',
|
'function' => 'spl_autoload_call',
|
||||||
'args' => [$class],
|
'args' => [$class],
|
||||||
];
|
];
|
||||||
$i = 1 + array_search($autoloadFrame, $trace, true);
|
|
||||||
|
|
||||||
if (isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
|
if (false === $i = array_search($autoloadFrame, $trace, true)) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($trace[++$i]['function']) && !isset($trace[$i]['class'])) {
|
||||||
switch ($trace[$i]['function']) {
|
switch ($trace[$i]['function']) {
|
||||||
case 'get_class_methods':
|
case 'get_class_methods':
|
||||||
case 'get_class_vars':
|
case 'get_class_vars':
|
||||||
|
Reference in New Issue
Block a user