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