Skip to content

Commit 0409447

Browse files
bug #20959 [FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer (fancyweb)
This PR was squashed before being merged into the 3.2 branch (closes #20959). Discussion ---------- [FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | I ran into a case that led to an `AnnotationException` in the `AnnotationsCacheWarmer`. I deduced that these exceptions should be ignored to not break the cache warming process. The case : * You have a controller in your `AppBundle\Controller\` directory. * You have an annotation in this controller that will never be found (because you made a mistake in the class / annotation name) or not every time : for example your controller is only used in dev env and it uses an annotation that is defined in a vendor bundle that is only under the `require-dev` key in your `composer.json` because you don't need it in your production deployment. So in production, your controller exists but will never be used. However the vendor bundle does not exist. * You dump your autoload files with composer using the `--optimize` option. * You end up with an `AnnotationException`. Why : * In the [`FrameworkExtension`](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L189), the `**Bundle\Controller\` pattern is added as an "annotated class to compile". * In the [`AddClassesToCachePass`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/DependencyInjection/AddClassesToCachePass.php#L50), all the classes defined in the `autoload_classmap.php` file are matched against the previous pattern. * Your controller will match. So even if you don't use it at all in your application, the annotation will still be read and the exception will be thrown. Commits ------- 6749569 [FrameworkBundle] Ignore AnnotationException exceptions in the AnnotationsCacheWarmer
2 parents c2d8a5c + 3391ada commit 0409447

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 10 additions & 0 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 Doctrine\Common\Annotations\CachedReader;
1516
use Doctrine\Common\Annotations\Reader;
1617
use Psr\Cache\CacheItemPoolInterface;
@@ -75,6 +76,15 @@ public function warmUp($cacheDir)
7576
$this->readAllComponents($reader, $class);
7677
} catch (\ReflectionException $e) {
7778
// ignore failing reflection
79+
} catch (AnnotationException $e) {
80+
/*
81+
* Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
82+
* configured or could not be found / read / etc.
83+
*
84+
* In particular cases, an Annotation in your code can be used and defined only for a specific
85+
* environment but is always added to the annotations.map file by some Symfony default behaviors,
86+
* and you always end up with a not found Annotation.
87+
*/
7888
}
7989
}
8090
} finally {

0 commit comments

Comments
 (0)