1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 Yahoo Japan Corporation |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace YahooJapan\ConfigCacheBundle\ConfigCache; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
15
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
16
|
|
|
use Symfony\Component\Config\Definition\ArrayNode; |
17
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
18
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Definition\Processor; |
19
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Resource\FileResource; |
20
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Util\ArrayAccessInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ConfigCache manages user-defined configuration files. |
24
|
|
|
*/ |
25
|
|
|
class ConfigCache |
26
|
|
|
{ |
27
|
|
|
const DEFAULT_ID = 'cache'; |
28
|
|
|
const TAG_REGISTER = 'config_cache.register'; |
29
|
|
|
const TAG_CACHE_WARMER = 'config_cache.warmer'; |
30
|
|
|
|
31
|
|
|
protected $cache; |
32
|
|
|
protected $loader; |
33
|
|
|
protected $config = array(); |
34
|
|
|
protected $arrayAccess; |
35
|
|
|
protected $configuration; |
36
|
|
|
protected $resources = array(); |
37
|
|
|
// doctrine cache ID |
38
|
|
|
protected $id; |
39
|
|
|
protected $strict = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param Cache $cache |
45
|
|
|
* @param LoaderInterface $loader |
46
|
|
|
* @param array $config |
47
|
|
|
*/ |
48
|
12 |
|
public function __construct(Cache $cache, LoaderInterface $loader, array $config = array()) |
49
|
|
|
{ |
50
|
12 |
|
$this->cache = $cache; |
51
|
12 |
|
$this->loader = $loader; |
52
|
12 |
|
$this->config = $config; |
53
|
12 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets a loader. |
57
|
|
|
* |
58
|
|
|
* @param LoaderInterface $loader |
59
|
|
|
* |
60
|
|
|
* @return ConfigCache |
61
|
|
|
*/ |
62
|
15 |
|
public function setLoader(LoaderInterface $loader) |
63
|
|
|
{ |
64
|
15 |
|
$this->loader = $loader; |
65
|
|
|
|
66
|
15 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets a ArrayAccess to find array value using dotted key. |
71
|
|
|
* |
72
|
|
|
* @param ArrayAccessInterface $arrayAccess |
73
|
|
|
* |
74
|
|
|
* @return ConfigCache |
75
|
|
|
*/ |
76
|
14 |
|
public function setArrayAccess(ArrayAccessInterface $arrayAccess) |
77
|
|
|
{ |
78
|
14 |
|
$this->arrayAccess = $arrayAccess; |
79
|
|
|
|
80
|
14 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Adds a resource. |
85
|
|
|
* |
86
|
|
|
* @param string $resource |
87
|
|
|
* @param ConfigurationInterface|null $configuration |
88
|
|
|
* |
89
|
|
|
* @return ConfigCache |
90
|
|
|
*/ |
91
|
40 |
|
public function addResource($resource, ConfigurationInterface $configuration = null) |
92
|
|
|
{ |
93
|
40 |
|
$this->resources[] = new FileResource($resource, $configuration); |
94
|
|
|
|
95
|
40 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets a configuration. |
100
|
|
|
* |
101
|
|
|
* @param ConfigurationInterface $configuration |
102
|
|
|
* |
103
|
|
|
* @return ConfigCache |
104
|
|
|
*/ |
105
|
11 |
|
public function setConfiguration(ConfigurationInterface $configuration) |
106
|
|
|
{ |
107
|
11 |
|
$this->configuration = $configuration; |
108
|
|
|
|
109
|
11 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets a doctrine cache ID (only once). |
114
|
|
|
* |
115
|
|
|
* @param string $id |
116
|
|
|
* |
117
|
|
|
* @return ConfigCache |
118
|
|
|
* |
119
|
|
|
* @throws \RuntimeException |
120
|
|
|
*/ |
121
|
11 |
|
public function setId($id) |
122
|
|
|
{ |
123
|
11 |
|
if (!is_null($this->id)) { |
124
|
1 |
|
throw new \RuntimeException('The key must not be set if already set.'); |
125
|
|
|
} |
126
|
|
|
|
127
|
11 |
|
$this->id = $id; |
128
|
|
|
|
129
|
11 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets a strict mode. |
134
|
|
|
* |
135
|
|
|
* @param bool $strict |
136
|
|
|
* |
137
|
|
|
* @return ConfigCache |
138
|
|
|
*/ |
139
|
12 |
|
public function setStrict($strict) |
140
|
|
|
{ |
141
|
12 |
|
$this->strict = $strict; |
142
|
|
|
|
143
|
12 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Finds cached array. |
148
|
|
|
* |
149
|
|
|
* @param string $key |
150
|
|
|
* @param mixed $default |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
1 |
|
public function find($key, $default = array()) |
155
|
|
|
{ |
156
|
1 |
|
return $this->findInternal($this->findAll(), $key, $default); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Finds All cached array. |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
11 |
|
public function findAll() |
165
|
|
|
{ |
166
|
11 |
|
$data = $this->cache->fetch($this->findId()); |
167
|
11 |
|
if (!$data) { |
168
|
10 |
|
$data = $this->createInternal(); |
169
|
10 |
|
} |
170
|
|
|
|
171
|
11 |
|
return $data; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Creates PHP cache file. |
176
|
|
|
*/ |
177
|
4 |
|
public function create() |
178
|
|
|
{ |
179
|
4 |
|
if (!$this->cache->contains($this->findId())) { |
180
|
3 |
|
$this->createInternal(); |
181
|
3 |
|
} |
182
|
4 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Saves PHP cache file to the temporary directory. |
186
|
|
|
*/ |
187
|
2 |
|
public function save() |
188
|
|
|
{ |
189
|
2 |
|
$this->findRestorableCache()->saveToTemp($this->findId()); |
190
|
2 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Restores PHP cache file to the cache directory. |
194
|
|
|
*/ |
195
|
1 |
|
public function restore() |
196
|
|
|
{ |
197
|
1 |
|
$this->findRestorableCache()->restore($this->findId()); |
198
|
1 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Creates PHP cache file internal processing. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
15 |
|
protected function createInternal() |
206
|
|
|
{ |
207
|
15 |
|
$data = $this->load(); |
208
|
15 |
|
$this->cache->save($this->findId(), $data); |
209
|
|
|
|
210
|
15 |
|
return $data; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Finds a doctrine cache ID. |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
26 |
|
protected function findId() |
219
|
|
|
{ |
220
|
26 |
|
return $this->id ?: static::DEFAULT_ID; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Gets a strict mode. |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
28 |
|
protected function getStrict() |
229
|
|
|
{ |
230
|
28 |
|
return $this->strict; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Whether the mode is strict or not. |
235
|
|
|
* |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
27 |
|
protected function isStrict() |
239
|
|
|
{ |
240
|
27 |
|
return !$this->getStrict() && count($this->resources) === 1; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Finds cached array internal processing. |
245
|
|
|
* |
246
|
|
|
* @param array $data |
247
|
|
|
* @param string $key |
248
|
|
|
* @param mixed $default |
249
|
|
|
* |
250
|
|
|
* @return array |
251
|
|
|
*/ |
252
|
7 |
|
protected function findInternal(array $data, $key, $default = array()) |
253
|
|
|
{ |
254
|
7 |
|
if (is_null($this->arrayAccess)) { |
255
|
3 |
|
$result = isset($data[$key]) ? $data[$key] : $default; |
256
|
3 |
|
} else { |
257
|
4 |
|
$result = $this->arrayAccess->replace($data)->get($key, $default); |
258
|
|
|
} |
259
|
|
|
|
260
|
7 |
|
return $result; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Loads config files. |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
28 |
|
protected function load() |
269
|
|
|
{ |
270
|
28 |
|
if ($this->resources === array()) { |
271
|
1 |
|
throw new \Exception('No added resources.'); |
272
|
|
|
} |
273
|
27 |
|
if ($this->isStrict()) { |
274
|
2 |
|
return $this->loadOne(); |
275
|
|
|
} |
276
|
|
|
|
277
|
25 |
|
$loaded = $this->config; |
278
|
25 |
|
$masterNode = $this->createMasterNode(); |
279
|
|
|
|
280
|
25 |
|
foreach ($this->resources as $file) { |
281
|
25 |
|
list($loaded, $masterNode) = $this->processConfiguration( |
282
|
25 |
|
$loaded, |
283
|
25 |
|
$this->loader->load($file->getResource()), |
284
|
25 |
|
$file->getConfiguration(), |
285
|
|
|
$masterNode |
286
|
25 |
|
); |
287
|
25 |
|
} |
288
|
|
|
|
289
|
25 |
|
return $loaded; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Loads a config file which is not strict mode. |
294
|
|
|
* |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
3 |
|
protected function loadOne() |
298
|
|
|
{ |
299
|
3 |
|
return $this->loader->load($this->resources[0]->getResource()); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Processes an array of configurations. |
304
|
|
|
* |
305
|
|
|
* @param array $validated validated array |
306
|
|
|
* @param array $validating validating array |
307
|
|
|
* @param ConfigurationInterface $configuration configuration |
308
|
|
|
* @param ArrayNode $masterNode master node |
309
|
|
|
* |
310
|
|
|
* @return array list of (array, ArrayNode) |
311
|
|
|
*/ |
312
|
28 |
|
protected function processConfiguration( |
313
|
|
|
array $validated, |
314
|
|
|
array $validating, |
315
|
|
|
ConfigurationInterface $configuration, |
316
|
|
|
ArrayNode $masterNode = null |
317
|
|
|
) { |
318
|
28 |
|
$processor = new Processor(); |
319
|
|
|
|
320
|
28 |
|
return $processor->processConfiguration( |
321
|
28 |
|
$validated, |
322
|
28 |
|
$validating, |
323
|
28 |
|
$configuration, |
324
|
|
|
$masterNode |
325
|
28 |
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Creates master node. |
330
|
|
|
* |
331
|
|
|
* @return ArrayNode |
332
|
|
|
*/ |
333
|
27 |
|
protected function createMasterNode() |
334
|
|
|
{ |
335
|
27 |
|
if (is_null($this->configuration)) { |
336
|
24 |
|
return $this->configuration; |
337
|
|
|
} else { |
338
|
3 |
|
return $this->configuration->getConfigTreeBuilder()->buildTree(); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Finds a restorable cache object. |
344
|
|
|
* |
345
|
|
|
* @return RestorablePhpFileCache |
346
|
|
|
* |
347
|
|
|
* @throws \RuntimeException |
348
|
|
|
*/ |
349
|
6 |
|
protected function findRestorableCache() |
350
|
|
|
{ |
351
|
6 |
|
if (!($this->cache instanceof RestorablePhpFileCache)) { |
352
|
1 |
|
throw new \RuntimeException('RestorablePhpFileCache should be set to use save/restore method.'); |
353
|
|
|
} |
354
|
|
|
|
355
|
5 |
|
return $this->cache; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|