Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1782e42

Browse files
committed
[FrameworkBundle][CacheWarmer] Ignore exeptions thrown during reflection classes autoload
1 parente5bd6ff commit1782e42

File tree

12 files changed

+238
-61
lines changed

12 files changed

+238
-61
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\Cache\Adapter\ArrayAdapter;
1717
useSymfony\Component\Cache\Adapter\PhpArrayAdapter;
1818
useSymfony\Component\Cache\Adapter\ProxyAdapter;
19+
useSymfony\Component\Config\Resource\ClassExistenceResource;
1920
useSymfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
2021

2122
/**
@@ -54,13 +55,13 @@ public function warmUp($cacheDir)
5455
{
5556
$arrayAdapter =newArrayAdapter();
5657

57-
spl_autoload_register([PhpArrayAdapter::class,'throwOnRequiredClass']);
58+
spl_autoload_register([ClassExistenceResource::class,'throwOnRequiredClass']);
5859
try {
5960
if (!$this->doWarmUp($cacheDir,$arrayAdapter)) {
6061
return;
6162
}
6263
}finally {
63-
spl_autoload_unregister([PhpArrayAdapter::class,'throwOnRequiredClass']);
64+
spl_autoload_unregister([ClassExistenceResource::class,'throwOnRequiredClass']);
6465
}
6566

6667
// the ArrayAdapter stores the values serialized

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
usePsr\Cache\CacheItemPoolInterface;
1818
useSymfony\Component\Cache\Adapter\ArrayAdapter;
1919
useSymfony\Component\Cache\DoctrineProvider;
20+
useSymfony\Component\Config\Resource\ClassExistenceResource;
2021

2122
/**
2223
* Warms up annotation caches for classes found in composer's autoload class map
@@ -66,15 +67,13 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
6667
$this->readAllComponents($reader,$class);
6768
}catch (\ReflectionException$e) {
6869
// ignore failing reflection
69-
}catch (AnnotationException$e) {
70-
/*
71-
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
72-
* configured or could not be found / read / etc.
73-
*
74-
* In particular cases, an Annotation in your code can be used and defined only for a specific
75-
* environment but is always added to the annotations.map file by some Symfony default behaviors,
76-
* and you always end up with a not found Annotation.
77-
*/
70+
}catch (\Exception$e) {
71+
try {
72+
ClassExistenceResource::throwOnRequiredClass($class,$e);
73+
74+
throw$e;
75+
}catch (\ReflectionException$e) {
76+
}
7877
}
7978
}
8079

@@ -84,14 +83,32 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
8483
privatefunctionreadAllComponents(Reader$reader,$class)
8584
{
8685
$reflectionClass =new \ReflectionClass($class);
87-
$reader->getClassAnnotations($reflectionClass);
86+
87+
try {
88+
$reader->getClassAnnotations($reflectionClass);
89+
}catch (AnnotationException$e) {
90+
/*
91+
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
92+
* configured or could not be found / read / etc.
93+
*
94+
* In particular cases, an Annotation in your code can be used and defined only for a specific
95+
* environment but is always added to the annotations.map file by some Symfony default behaviors,
96+
* and you always end up with a not found Annotation.
97+
*/
98+
}
8899

89100
foreach ($reflectionClass->getMethods()as$reflectionMethod) {
90-
$reader->getMethodAnnotations($reflectionMethod);
101+
try {
102+
$reader->getMethodAnnotations($reflectionMethod);
103+
}catch (AnnotationException$e) {
104+
}
91105
}
92106

93107
foreach ($reflectionClass->getProperties()as$reflectionProperty) {
94-
$reader->getPropertyAnnotations($reflectionProperty);
108+
try {
109+
$reader->getPropertyAnnotations($reflectionProperty);
110+
}catch (AnnotationException$e) {
111+
}
95112
}
96113
}
97114
}

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useDoctrine\Common\Annotations\AnnotationException;
1515
usePsr\Cache\CacheItemPoolInterface;
1616
useSymfony\Component\Cache\Adapter\ArrayAdapter;
17+
useSymfony\Component\Config\Resource\ClassExistenceResource;
1718
useSymfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
1819
useSymfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1920
useSymfony\Component\Serializer\Mapping\Loader\LoaderChain;
@@ -60,6 +61,13 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
6061
// ignore failing reflection
6162
}catch (AnnotationException$e) {
6263
// ignore failing annotations
64+
}catch (\Exception$e) {
65+
try {
66+
ClassExistenceResource::throwOnRequiredClass($mappedClass,$e);
67+
68+
throw$e;
69+
}catch (\ReflectionException$e) {
70+
}
6371
}
6472
}
6573
}

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
usePsr\Cache\CacheItemPoolInterface;
1616
useSymfony\Component\Cache\Adapter\ArrayAdapter;
1717
useSymfony\Component\Cache\Adapter\PhpArrayAdapter;
18+
useSymfony\Component\Config\Resource\ClassExistenceResource;
1819
useSymfony\Component\Validator\Mapping\Cache\Psr6Cache;
1920
useSymfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
2021
useSymfony\Component\Validator\Mapping\Loader\LoaderChain;
@@ -65,6 +66,13 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
6566
// ignore failing reflection
6667
}catch (AnnotationException$e) {
6768
// ignore failing annotations
69+
}catch (\Exception$e) {
70+
try {
71+
ClassExistenceResource::throwOnRequiredClass($mappedClass,$e);
72+
73+
throw$e;
74+
}catch (\ReflectionException$e) {
75+
}
6876
}
6977
}
7078
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,54 @@ public function testAnnotationsCacheWarmerWithDebugEnabled()
8585
$reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
8686
}
8787

88+
/**
89+
* Test that the cache warming process is not broken if a class loader
90+
* throws an exception (on class / file not found for example).
91+
*/
92+
publicfunctiontestClassAutoloadException()
93+
{
94+
$this->assertFalse(class_exists($annotatedClass ='C\C\C',false));
95+
96+
file_put_contents($this->cacheDir.'/annotations.map',sprintf('<?php return %s;',var_export([$annotatedClass],true)));
97+
$warmer =newAnnotationsCacheWarmer(newAnnotationReader(),tempnam($this->cacheDir,__FUNCTION__),newArrayAdapter());
98+
99+
spl_autoload_register($classLoader =function ($class)use ($annotatedClass) {
100+
if ($class ===$annotatedClass) {
101+
thrownew \DomainException('This exception should be caught by the warmer.');
102+
}
103+
},true,true);
104+
105+
$warmer->warmUp($this->cacheDir);
106+
107+
spl_autoload_unregister($classLoader);
108+
}
109+
110+
/**
111+
* Test that the cache warming process is broken if a class loader throws an
112+
* exception but that is unrelated to the class load.
113+
*
114+
* @expectedException \DomainException
115+
* @expectedExceptionMessage This exception should not be caught by the warmer.
116+
*/
117+
publicfunctiontestClassAutoloadExceptionWithUnrelatedException()
118+
{
119+
$this->assertFalse(class_exists($annotatedClass ='AClassThatDoesNotExist_FWB_CacheWarmer_AnnotationsCacheWarmerTest',false));
120+
121+
file_put_contents($this->cacheDir.'/annotations.map',sprintf('<?php return %s;',var_export([$annotatedClass],true)));
122+
$warmer =newAnnotationsCacheWarmer(newAnnotationReader(),tempnam($this->cacheDir,__FUNCTION__),newArrayAdapter());
123+
124+
spl_autoload_register($classLoader =function ($class)use ($annotatedClass) {
125+
if ($class ===$annotatedClass) {
126+
eval('class'.$annotatedClass.'{}');
127+
thrownew \DomainException('This exception should not be caught by the warmer.');
128+
}
129+
},true,true);
130+
131+
$warmer->warmUp($this->cacheDir);
132+
133+
spl_autoload_unregister($classLoader);
134+
}
135+
88136
/**
89137
* @return \PHPUnit_Framework_MockObject_MockObject|Reader
90138
*/

‎src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,58 @@ public function testWarmUpWithoutLoader()
7777
$this->assertInternalType('array',$values);
7878
$this->assertCount(0,$values);
7979
}
80+
81+
/**
82+
* Test that the cache warming process is not broken if a class loader
83+
* throws an exception (on class / file not found for example).
84+
*/
85+
publicfunctiontestClassAutoloadException()
86+
{
87+
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class,'getMappedClasses') || !method_exists(YamlFileLoader::class,'getMappedClasses')) {
88+
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.');
89+
}
90+
91+
$this->assertFalse(class_exists($mappedClass ='AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest',false));
92+
93+
$warmer =newSerializerCacheWarmer([newYamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')],tempnam(sys_get_temp_dir(),__FUNCTION__),newArrayAdapter());
94+
95+
spl_autoload_register($classLoader =function ($class)use ($mappedClass) {
96+
if ($class ===$mappedClass) {
97+
thrownew \DomainException('This exception should be caught by the warmer.');
98+
}
99+
},true,true);
100+
101+
$warmer->warmUp('foo');
102+
103+
spl_autoload_unregister($classLoader);
104+
}
105+
106+
/**
107+
* Test that the cache warming process is broken if a class loader throws an
108+
* exception but that is unrelated to the class load.
109+
*
110+
* @expectedException \DomainException
111+
* @expectedExceptionMessage This exception should not be caught by the warmer.
112+
*/
113+
publicfunctiontestClassAutoloadExceptionWithUnrelatedException()
114+
{
115+
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class,'getMappedClasses') || !method_exists(YamlFileLoader::class,'getMappedClasses')) {
116+
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.');
117+
}
118+
119+
$this->assertFalse(class_exists($mappedClass ='AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest',false));
120+
121+
$warmer =newSerializerCacheWarmer([newYamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')],tempnam(sys_get_temp_dir(),__FUNCTION__),newArrayAdapter());
122+
123+
spl_autoload_register($classLoader =function ($class)use ($mappedClass) {
124+
if ($class ===$mappedClass) {
125+
eval('class'.$mappedClass.'{}');
126+
thrownew \DomainException('This exception should not be caught by the warmer.');
127+
}
128+
},true,true);
129+
130+
$warmer->warmUp('foo');
131+
132+
spl_autoload_unregister($classLoader);
133+
}
80134
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,54 @@ public function testWarmUpWithoutLoader()
102102
$this->assertInternalType('array',$values);
103103
$this->assertCount(0,$values);
104104
}
105+
106+
/**
107+
* Test that the cache warming process is not broken if a class loader
108+
* throws an exception (on class / file not found for example).
109+
*/
110+
publicfunctiontestClassAutoloadException()
111+
{
112+
$this->assertFalse(class_exists($mappedClass ='AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest',false));
113+
114+
$validatorBuilder =newValidatorBuilder();
115+
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
116+
$warmer =newValidatorCacheWarmer($validatorBuilder,tempnam(sys_get_temp_dir(),__FUNCTION__),newArrayAdapter());
117+
118+
spl_autoload_register($classloader =function ($class)use ($mappedClass) {
119+
if ($class ===$mappedClass) {
120+
thrownew \DomainException('This exception should be caught by the warmer.');
121+
}
122+
},true,true);
123+
124+
$warmer->warmUp('foo');
125+
126+
spl_autoload_unregister($classloader);
127+
}
128+
129+
/**
130+
* Test that the cache warming process is broken if a class loader throws an
131+
* exception but that is unrelated to the class load.
132+
*
133+
* @expectedException \DomainException
134+
* @expectedExceptionMessage This exception should not be caught by the warmer.
135+
*/
136+
publicfunctiontestClassAutoloadExceptionWithUnrelatedException()
137+
{
138+
$this->assertFalse(class_exists($mappedClass ='AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest',false));
139+
140+
$validatorBuilder =newValidatorBuilder();
141+
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
142+
$warmer =newValidatorCacheWarmer($validatorBuilder,tempnam(sys_get_temp_dir(),__FUNCTION__),newArrayAdapter());
143+
144+
spl_autoload_register($classLoader =function ($class)use ($mappedClass) {
145+
if ($class ===$mappedClass) {
146+
eval('class'.$mappedClass.'{}');
147+
thrownew \DomainException('This exception should not be caught by the warmer.');
148+
}
149+
},true,true);
150+
151+
$warmer->warmUp('foo');
152+
153+
spl_autoload_unregister($classLoader);
154+
}
105155
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest:~
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest:~

‎src/Symfony/Bundle/FrameworkBundle/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/cache":"~3.4|~4.0",
2222
"symfony/class-loader":"~3.2",
2323
"symfony/dependency-injection":"^3.4.24|^4.2.5",
24-
"symfony/config":"~3.4|~4.0",
24+
"symfony/config":"^3.4.30|~4.2.11|^4.3.3",
2525
"symfony/debug":"~2.8|~3.0|~4.0",
2626
"symfony/event-dispatcher":"~3.4|~4.0",
2727
"symfony/http-foundation":"^3.3.11|~4.0",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp