-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheMetadataFactory.php
268 lines (219 loc) · 8.01 KB
/
CacheMetadataFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Metadata;
use Klipper\Component\DoctrineExtra\Util\ClassUtils;
use Klipper\Component\Metadata\Loader\ChoiceNameCollection;
use Klipper\Component\Metadata\Loader\ObjectMetadataNameCollection;
use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
/**
* @author François Pluchino <[email protected]>
*/
class CacheMetadataFactory implements MetadataFactoryInterface, WarmableInterface
{
protected MetadataFactoryInterface $factory;
/**
* @var array
*/
protected $options = [
'cache_dir' => null,
'debug' => false,
];
private ?ConfigCacheFactoryInterface $configCacheFactory = null;
/**
* @var null|ObjectMetadataInterface[]
*/
private ?array $metadatas = null;
private ?ObjectMetadataNameCollection $names = null;
/**
* @var null|ChoiceInterface[]
*/
private ?array $choices = null;
private ?ChoiceNameCollection $choiceNames = null;
/**
* @param MetadataFactoryInterface $factory The metadata factory
* @param array $options An array of options
*/
public function __construct(MetadataFactoryInterface $factory, array $options = [])
{
$this->factory = $factory;
$this->options = array_merge($this->options, $options);
}
public function getManagedClasses(): ObjectMetadataNameCollection
{
if (null === $this->options['cache_dir']) {
return $this->factory->getManagedClasses();
}
if (null === $this->names) {
$self = $this;
$cache = $this->getConfigCacheFactory()->cache(
$this->options['cache_dir'].'/names.php',
static function (ConfigCacheInterface $cache) use ($self): void {
$classes = $self->factory->getManagedClasses();
$content = sprintf(
'unserialize(%s)',
var_export(serialize($classes), true)
);
$cache->write($self->getContent($content), $classes->getResources());
}
);
$this->names = require $cache->getPath();
}
return $this->names;
}
public function isManagedClass(string $class): bool
{
return $this->factory->isManagedClass($class);
}
public function isManagedByName(string $name): bool
{
$names = $this->getManagedClasses()->all();
return isset($names[$name]);
}
public function getManagedClass(string $class): string
{
return $this->factory->getManagedClass($class);
}
public function create(string $class): ObjectMetadataInterface
{
if (null === $this->options['cache_dir']) {
return $this->factory->create($class);
}
$self = $this;
$names = $this->getManagedClasses()->all();
$class = ClassUtils::getRealClass($class);
$class = $this->getManagedClass($names[$class] ?? $class);
if (!isset($this->metadatas[$class])) {
$filename = $this->options['cache_dir'].'/class_'.str_replace('\\', '_', $class).'.php';
$cache = $this->getConfigCacheFactory()->cache(
$filename,
static function (ConfigCacheInterface $cache) use ($class, $self): void {
$metadata = $self->factory->create($class);
$content = sprintf(
'unserialize(%s, %s)',
var_export(serialize($metadata), true),
var_export([$metadata->getClass()], true)
);
$cache->write($self->getContent($content), $metadata->getResources());
}
);
$this->metadatas[$class] = require $cache->getPath();
}
return $this->metadatas[$class];
}
public function createChoice(string $name): ChoiceInterface
{
if (null === $this->options['cache_dir']) {
return $this->factory->createChoice($name);
}
$self = $this;
if (!isset($this->choices[$name])) {
$filename = $this->options['cache_dir'].'/choices_'.$name.'.php';
$cache = $this->getConfigCacheFactory()->cache(
$filename,
static function (ConfigCacheInterface $cache) use ($name, $self): void {
$metadata = $self->factory->createChoice($name);
$content = sprintf(
'unserialize(%s, %s)',
var_export(serialize($metadata), true),
var_export([Choice::class], true)
);
$cache->write($self->getContent($content), $metadata->getResources());
}
);
$this->choices[$name] = require $cache->getPath();
}
return $this->choices[$name];
}
public function getChoiceNames(): ChoiceNameCollection
{
if (null === $this->options['cache_dir']) {
return $this->factory->getChoiceNames();
}
if (null === $this->choiceNames) {
$self = $this;
$cache = $this->getConfigCacheFactory()->cache(
$this->options['cache_dir'].'/choice_names.php',
static function (ConfigCacheInterface $cache) use ($self): void {
$choiceNames = $self->factory->getChoiceNames();
$content = sprintf(
'unserialize(%s)',
var_export(serialize($choiceNames), true)
);
$cache->write($self->getContent($content), $choiceNames->getResources());
foreach ($choiceNames as $choiceName) {
$self->createChoice($choiceName);
}
}
);
$this->choiceNames = require $cache->getPath();
}
return $this->choiceNames;
}
public function isChoiceManaged(string $name): bool
{
$names = $this->getChoiceNames()->all();
return \in_array($name, $names, true);
}
/**
* @param mixed $cacheDir
*/
public function warmUp($cacheDir): void
{
// skip warmUp when metadata manager doesn't use cache
if (null === $this->options['cache_dir']) {
return;
}
$this->metadatas = null;
$this->names = null;
$this->choices = null;
foreach ($this->getManagedClasses()->all() as $class) {
$this->create($class);
}
foreach ($this->getChoiceNames()->all() as $name) {
$this->createChoice($name);
}
}
/**
* Set the config cache factory.
*
* @param ConfigCacheFactoryInterface $configCacheFactory The config cache factory
*/
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory): void
{
$this->configCacheFactory = $configCacheFactory;
}
/**
* Provides the ConfigCache factory implementation, falling back to a
* default implementation if necessary.
*/
private function getConfigCacheFactory(): ConfigCacheFactoryInterface
{
if (!$this->configCacheFactory) {
$this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
}
return $this->configCacheFactory;
}
/**
* @param string $content The content
*/
private function getContent(string $content): string
{
return sprintf(
<<<'EOF'
<?php
return %s;
EOF,
$content
);
}
}