15
15
use Doctrine \Common \Annotations \CachedReader ;
16
16
use Doctrine \Common \Annotations \Reader ;
17
17
use Psr \Cache \CacheItemPoolInterface ;
18
- use Symfony \Component \Cache \Adapter \AdapterInterface ;
19
18
use Symfony \Component \Cache \Adapter \ArrayAdapter ;
20
19
use Symfony \Component \Cache \Adapter \PhpArrayAdapter ;
21
- use Symfony \Component \Cache \Adapter \ProxyAdapter ;
22
20
use Symfony \Component \Cache \DoctrineProvider ;
23
- use Symfony \Component \HttpKernel \CacheWarmer \CacheWarmerInterface ;
24
21
25
22
/**
26
23
* Warms up annotation caches for classes found in composer's autoload class map
27
24
* and declared in DI bundle extensions using the addAnnotatedClassesToCache method.
28
25
*
29
26
* @author Titouan Galopin <galopintitouan@gmail.com>
30
27
*/
31
- class AnnotationsCacheWarmer implements CacheWarmerInterface
28
+ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer
32
29
{
33
30
private $ annotationReader ;
34
- private $ phpArrayFile ;
35
- private $ fallbackPool ;
36
31
37
32
/**
38
33
* @param Reader $annotationReader
@@ -41,71 +36,50 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
41
36
*/
42
37
public function __construct (Reader $ annotationReader , $ phpArrayFile , CacheItemPoolInterface $ fallbackPool )
43
38
{
39
+ parent ::__construct ($ phpArrayFile , $ fallbackPool );
44
40
$ this ->annotationReader = $ annotationReader ;
45
- $ this ->phpArrayFile = $ phpArrayFile ;
46
- if (!$ fallbackPool instanceof AdapterInterface) {
47
- $ fallbackPool = new ProxyAdapter ($ fallbackPool );
48
- }
49
- $ this ->fallbackPool = $ fallbackPool ;
50
41
}
51
42
52
43
/**
53
44
* {@inheritdoc}
54
45
*/
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 )
56
55
{
57
- $ adapter = new PhpArrayAdapter ($ this ->phpArrayFile , $ this ->fallbackPool );
58
56
$ annotatedClassPatterns = $ cacheDir .'/annotations.map ' ;
59
57
60
58
if (!is_file ($ annotatedClassPatterns )) {
61
- $ adapter ->warmUp (array ());
59
+ $ phpArrayAdapter ->warmUp (array ());
62
60
63
- return ;
61
+ return false ;
64
62
}
65
63
66
64
$ 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
+ */
88
81
}
89
- } finally {
90
- spl_autoload_unregister (array ($ adapter , 'throwOnRequiredClass ' ));
91
82
}
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 ;
109
83
}
110
84
111
85
private function readAllComponents (Reader $ reader , $ class )
0 commit comments