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 ;
20- use Symfony \Component \Cache \Adapter \PhpArrayAdapter ;
21- use Symfony \Component \Cache \Adapter \ProxyAdapter ;
2219use Symfony \Component \Cache \DoctrineProvider ;
23- use Symfony \Component \HttpKernel \CacheWarmer \CacheWarmerInterface ;
2420
2521/**
2622 * Warms up annotation caches for classes found in composer's autoload class map
2723 * and declared in DI bundle extensions using the addAnnotatedClassesToCache method.
2824 *
2925 * @author Titouan Galopin <galopintitouan@gmail.com>
3026 */
31- class AnnotationsCacheWarmer implements CacheWarmerInterface
27+ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
3228{
3329 private $ annotationReader ;
34- private $ phpArrayFile ;
35- private $ fallbackPool ;
3630
3731 /**
3832 * @param Reader $annotationReader
@@ -41,70 +35,41 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
4135 */
4236 public function __construct (Reader $ annotationReader , $ phpArrayFile , CacheItemPoolInterface $ fallbackPool )
4337 {
38+ parent ::__construct ($ phpArrayFile , $ fallbackPool );
4439 $ this ->annotationReader = $ annotationReader ;
45- $ this ->phpArrayFile = $ phpArrayFile ;
46- if (!$ fallbackPool instanceof AdapterInterface) {
47- $ fallbackPool = new ProxyAdapter ($ fallbackPool );
48- }
49- $ this ->fallbackPool = $ fallbackPool ;
5040 }
5141
5242 /**
5343 * {@inheritdoc}
5444 */
55- public function warmUp ($ cacheDir )
45+ protected function doWarmUp ($ cacheDir, ArrayAdapter $ arrayAdapter )
5646 {
57- $ adapter = new PhpArrayAdapter ($ this ->phpArrayFile , $ this ->fallbackPool );
5847 $ annotatedClassPatterns = $ cacheDir .'/annotations.map ' ;
5948
6049 if (!is_file ($ annotatedClassPatterns )) {
61- $ adapter ->warmUp (array ());
62-
63- return ;
50+ return true ;
6451 }
6552
6653 $ 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- }
54+ $ reader = new CachedReader ($ this ->annotationReader , new DoctrineProvider ($ arrayAdapter ));
55+
56+ foreach ($ annotatedClasses as $ class ) {
57+ try {
58+ $ this ->readAllComponents ($ reader , $ class );
59+ } catch (\ReflectionException $ e ) {
60+ // ignore failing reflection
61+ } catch (AnnotationException $ e ) {
62+ /*
63+ * Ignore any AnnotationException to not break the cache warming process if an Annotation is badly
64+ * configured or could not be found / read / etc.
65+ *
66+ * In particular cases, an Annotation in your code can be used and defined only for a specific
67+ * environment but is always added to the annotations.map file by some Symfony default behaviors,
68+ * and you always end up with a not found Annotation.
69+ */
8870 }
89- } finally {
90- spl_autoload_unregister (array ($ adapter , 'throwOnRequiredClass ' ));
91- }
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 ));
9971 }
100- $ this ->fallbackPool ->commit ();
101- }
10272
103- /**
104- * {@inheritdoc}
105- */
106- public function isOptional ()
107- {
10873 return true ;
10974 }
11075
0 commit comments