1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padam87\AttributeBundle\CacheWarmer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
7
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
8
|
|
|
use Symfony\Component\Config\ConfigCache; |
9
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
10
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
11
|
|
|
|
12
|
|
|
class EntityAnnotationCacheWarmer implements CacheWarmerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ManagerRegistry |
16
|
|
|
*/ |
17
|
|
|
private $doctrine; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $debug; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ManagerRegistry $doctrine |
26
|
|
|
* @param $debug |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ManagerRegistry $doctrine, $debug) |
29
|
|
|
{ |
30
|
|
|
$this->doctrine = $doctrine; |
31
|
|
|
$this->debug = $debug; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function isOptional() |
38
|
|
|
{ |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function warmUp($cacheDir) |
46
|
|
|
{ |
47
|
|
|
$filename = $cacheDir . '/padam87/attribute_bundle/Entity.cache.php'; |
48
|
|
|
|
49
|
|
|
$cache = new ConfigCache($filename, $this->debug); |
50
|
|
|
|
51
|
|
|
if (!$cache->isFresh()) { |
52
|
|
|
$content = '<?php return ' . var_export($this->getEntities(), true) . ';'; |
53
|
|
|
$cache->write($content, $this->getResources()); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getEntities() |
58
|
|
|
{ |
59
|
|
|
$entities = []; |
60
|
|
|
|
61
|
|
|
$reader = new AnnotationReader(); |
62
|
|
|
|
63
|
|
|
/** @var ClassMetadata $metadata */ |
64
|
|
|
foreach ($this->getMetadata() as $metadata) { |
65
|
|
|
$refl = $metadata->getReflectionClass(); |
66
|
|
|
|
67
|
|
|
if ($refl === null) { |
68
|
|
|
$refl = new \ReflectionClass($metadata->getName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($reader->getClassAnnotation($refl, 'Padam87\AttributeBundle\Annotation\Entity') != null) { |
72
|
|
|
$entities[$metadata->getName()] = true; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $entities; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function getResources() |
83
|
|
|
{ |
84
|
|
|
$res = []; |
85
|
|
|
|
86
|
|
|
/** @var ClassMetadata $m */ |
87
|
|
|
foreach ($this->getMetadata() as $m) { |
88
|
|
|
$res[] = new FileResource($m->getReflectionClass()->getFileName()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $res; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function getMetadata() |
98
|
|
|
{ |
99
|
|
|
$em = $this->doctrine->getManager(); |
100
|
|
|
|
101
|
|
|
return $em->getMetadataFactory()->getAllMetadata(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|