1515use Doctrine \Common \Annotations \CachedReader ;
1616use Doctrine \Common \Annotations \Reader ;
1717use Psr \Cache \CacheItemPoolInterface ;
18- use Symfony \Component \Cache \Adapter \AdapterInterface ;
1918use Symfony \Component \Cache \Adapter \ArrayAdapter ;
2019use Symfony \Component \Cache \Adapter \PhpArrayAdapter ;
21- use Symfony \Component \Cache \Adapter \ProxyAdapter ;
2220use Symfony \Component \Cache \DoctrineProvider ;
23- use Symfony \Component \HttpKernel \CacheWarmer \CacheWarmerInterface ;
2421
2522/**
2623 * Warms up annotation caches for classes found in composer's autoload class map
2724 * and declared in DI bundle extensions using the addAnnotatedClassesToCache method.
2825 *
2926 * @author Titouan Galopin <galopintitouan@gmail.com>
3027 */
31- class AnnotationsCacheWarmer implements CacheWarmerInterface
28+ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
3229{
3330 private $ annotationReader ;
34- private $ phpArrayFile ;
35- private $ fallbackPool ;
3631
3732 /**
3833 * @param Reader $annotationReader
@@ -41,71 +36,50 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
4136 */
4237 public function __construct (Reader $ annotationReader , $ phpArrayFile , CacheItemPoolInterface $ fallbackPool )
4338 {
39+ parent ::__construct ($ phpArrayFile , $ fallbackPool );
4440 $ this ->annotationReader = $ annotationReader ;
45- $ this ->phpArrayFile = $ phpArrayFile ;
46- if (!$ fallbackPool instanceof AdapterInterface) {
47- $ fallbackPool = new ProxyAdapter ($ fallbackPool );
48- }
49- $ this ->fallbackPool = $ fallbackPool ;
5041 }
5142
5243 /**
5344 * {@inheritdoc}
5445 */
55- public function warmUp ($ cacheDir )
46+ public function isOptional ()
47+ {
48+ return true ;
49+ }
50+
51+ /**
52+ * {@inheritdoc}
53+ */
54+ protected function doWarmUp ($ cacheDir , PhpArrayAdapter $ phpArrayAdapter , ArrayAdapter $ arrayAdapter )
5655 {
57- $ adapter = new PhpArrayAdapter ($ this ->phpArrayFile , $ this ->fallbackPool );
5856 $ annotatedClassPatterns = $ cacheDir .'/annotations.map ' ;
5957
6058 if (!is_file ($ annotatedClassPatterns )) {
61- $ adapter ->warmUp (array ());
59+ $ phpArrayAdapter ->warmUp (array ());
6260
63- return ;
61+ return false ;
6462 }
6563
6664 $ annotatedClasses = include $ annotatedClassPatterns ;
67-
68- $ arrayPool = new ArrayAdapter (0 , false );
69- $ reader = new CachedReader ($ this ->annotationReader , new DoctrineProvider ($ arrayPool ));
70-
71- spl_autoload_register (array ($ adapter , 'throwOnRequiredClass ' ));
72- try {
73- foreach ($ annotatedClasses as $ class ) {
74- try {
75- $ this ->readAllComponents ($ reader , $ class );
76- } catch (\ReflectionException $ e ) {
77- // ignore failing reflection
78- } catch (AnnotationException $ e ) {
79- /*
80- * Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
81- * configured or could not be found / read / etc.
82- *
83- * In particular cases, an Annotation in your code can be used and defined only for a specific
84- * environment but is always added to the annotations.map file by some Symfony default behaviors,
85- * and you always end up with a not found Annotation.
86- */
87- }
65+ $ reader = new CachedReader ($ this ->annotationReader , new DoctrineProvider ($ arrayAdapter ));
66+
67+ foreach ($ annotatedClasses as $ class ) {
68+ try {
69+ $ this ->readAllComponents ($ reader , $ class );
70+ } catch (\ReflectionException $ e ) {
71+ // ignore failing reflection
72+ } catch (AnnotationException $ e ) {
73+ /*
74+ * Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
75+ * configured or could not be found / read / etc.
76+ *
77+ * In particular cases, an Annotation in your code can be used and defined only for a specific
78+ * environment but is always added to the annotations.map file by some Symfony default behaviors,
79+ * and you always end up with a not found Annotation.
80+ */
8881 }
89- } finally {
90- spl_autoload_unregister (array ($ adapter , 'throwOnRequiredClass ' ));
9182 }
92-
93- $ values = $ arrayPool ->getValues ();
94- $ adapter ->warmUp ($ values );
95-
96- foreach ($ values as $ k => $ v ) {
97- $ item = $ this ->fallbackPool ->getItem ($ k );
98- $ this ->fallbackPool ->saveDeferred ($ item ->set ($ v ));
99- }
100- $ this ->fallbackPool ->commit ();
101- }
102-
103- /**
104- * {@inheritdoc}
105- */
106- public function isOptional ()
107- {
108- return true ;
10983 }
11084
11185 private function readAllComponents (Reader $ reader , $ class )
0 commit comments