1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 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_KEY = 'cache'; |
28
|
|
|
|
29
|
|
|
protected $cache; |
30
|
|
|
protected $loader; |
31
|
|
|
protected $config = array(); |
32
|
|
|
protected $arrayAccess; |
33
|
|
|
protected $configuration; |
34
|
|
|
protected $resources = array(); |
35
|
|
|
protected $key; |
36
|
|
|
protected $strict = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param Cache $cache |
42
|
|
|
* @param LoaderInterface $loader |
43
|
|
|
* @param array $config |
44
|
|
|
*/ |
45
|
2 |
|
public function __construct(Cache $cache, LoaderInterface $loader, array $config = array()) |
46
|
|
|
{ |
47
|
2 |
|
$this->cache = $cache; |
48
|
2 |
|
$this->loader = $loader; |
49
|
2 |
|
$this->config = $config; |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets a loader. |
54
|
|
|
* |
55
|
|
|
* @param LoaderInterface $loader |
56
|
|
|
* |
57
|
|
|
* @return ConfigCache |
58
|
|
|
*/ |
59
|
13 |
|
public function setLoader(LoaderInterface $loader) |
60
|
|
|
{ |
61
|
13 |
|
$this->loader = $loader; |
62
|
|
|
|
63
|
13 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets a ArrayAccess to find array value using dotted key. |
68
|
|
|
* |
69
|
|
|
* @param ArrayAccessInterface $arrayAccess |
70
|
|
|
* |
71
|
|
|
* @return ConfigCache |
72
|
|
|
*/ |
73
|
4 |
|
public function setArrayAccess(ArrayAccessInterface $arrayAccess) |
74
|
|
|
{ |
75
|
4 |
|
$this->arrayAccess = $arrayAccess; |
76
|
|
|
|
77
|
4 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds a resource. |
82
|
|
|
* |
83
|
|
|
* @param string $resource |
84
|
|
|
* @param ConfigurationInterface|null $configuration |
85
|
|
|
* |
86
|
|
|
* @return ConfigCache |
87
|
|
|
*/ |
88
|
26 |
|
public function addResource($resource, ConfigurationInterface $configuration = null) |
89
|
|
|
{ |
90
|
26 |
|
$this->resources[] = new FileResource($resource, $configuration); |
91
|
|
|
|
92
|
26 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets a configuration. |
97
|
|
|
* |
98
|
|
|
* @param ConfigurationInterface $configuration |
99
|
|
|
* |
100
|
|
|
* @return ConfigCache |
101
|
|
|
*/ |
102
|
1 |
|
public function setConfiguration(ConfigurationInterface $configuration) |
103
|
|
|
{ |
104
|
1 |
|
$this->configuration = $configuration; |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets a key (only once). |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* |
114
|
|
|
* @return ConfigCache |
115
|
|
|
* |
116
|
|
|
* @throws \RuntimeException |
117
|
|
|
*/ |
118
|
1 |
|
public function setKey($key) |
119
|
|
|
{ |
120
|
1 |
|
if (!is_null($this->key)) { |
121
|
1 |
|
throw new \RuntimeException('The key must not be set if already set.'); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$this->key = $key; |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets a strict mode. |
131
|
|
|
* |
132
|
|
|
* @param bool $strict |
133
|
|
|
* |
134
|
|
|
* @return ConfigCache |
135
|
|
|
*/ |
136
|
2 |
|
public function setStrict($strict) |
137
|
|
|
{ |
138
|
2 |
|
$this->strict = $strict; |
139
|
|
|
|
140
|
2 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Finds cached array. |
145
|
|
|
* |
146
|
|
|
* @param string $key |
147
|
|
|
* @param mixed $default |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
1 |
|
public function find($key, $default = array()) |
152
|
|
|
{ |
153
|
1 |
|
return $this->findInternal($this->findAll(), $key, $default); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Finds All cached array. |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
9 |
|
public function findAll() |
162
|
|
|
{ |
163
|
9 |
|
$data = $this->cache->fetch($this->getKey()); |
164
|
9 |
|
if (!$data) { |
165
|
8 |
|
$data = $this->createInternal(); |
166
|
8 |
|
} |
167
|
|
|
|
168
|
9 |
|
return $data; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Creates PHP cache file. |
173
|
|
|
*/ |
174
|
2 |
|
public function create() |
175
|
|
|
{ |
176
|
2 |
|
if (!$this->cache->contains($this->getKey())) { |
177
|
1 |
|
$this->createInternal(); |
178
|
1 |
|
} |
179
|
2 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Creates PHP cache file internal processing. |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
11 |
|
protected function createInternal() |
187
|
|
|
{ |
188
|
11 |
|
$data = $this->load(); |
189
|
11 |
|
$this->cache->save($this->getKey(), $data); |
190
|
|
|
|
191
|
11 |
|
return $data; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets a key. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
20 |
|
protected function getKey() |
200
|
|
|
{ |
201
|
20 |
|
return $this->key ?: static::DEFAULT_KEY; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Whether the mode is strict or not. |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
22 |
|
protected function isStrict() |
210
|
|
|
{ |
211
|
22 |
|
return $this->strict; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Finds cached array internal processing. |
216
|
|
|
* |
217
|
|
|
* @param array $data |
218
|
|
|
* @param string $key |
219
|
|
|
* @param mixed $default |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
7 |
|
protected function findInternal(array $data, $key, $default = array()) |
224
|
|
|
{ |
225
|
7 |
|
$result = array(); |
|
|
|
|
226
|
|
|
|
227
|
7 |
|
if (is_null($this->arrayAccess)) { |
228
|
3 |
|
$result = isset($data[$key]) ? $data[$key] : $default; |
229
|
3 |
|
} else { |
230
|
4 |
|
$result = $this->arrayAccess->replace($data)->get($key, $default); |
231
|
|
|
} |
232
|
|
|
|
233
|
7 |
|
return $result; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Loads config files. |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
22 |
|
protected function load() |
242
|
|
|
{ |
243
|
22 |
|
if ($this->resources === array()) { |
244
|
1 |
|
throw new \Exception('No added resources.'); |
245
|
|
|
} |
246
|
21 |
|
if (!$this->isStrict() && count($this->resources) === 1) { |
247
|
1 |
|
return $this->loadOne(); |
248
|
|
|
} |
249
|
|
|
|
250
|
20 |
|
$loaded = $this->config; |
251
|
20 |
|
$masterNode = $this->createMasterNode(); |
252
|
|
|
|
253
|
20 |
|
foreach ($this->resources as $file) { |
254
|
20 |
|
list($loaded, $masterNode) = $this->processConfiguration( |
255
|
20 |
|
$loaded, |
256
|
20 |
|
$this->loader->load($file->getResource()), |
257
|
20 |
|
$file->getConfiguration(), |
258
|
|
|
$masterNode |
259
|
20 |
|
); |
260
|
20 |
|
} |
261
|
|
|
|
262
|
20 |
|
return $loaded; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Loads a config file which is not strict mode. |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
2 |
|
protected function loadOne() |
271
|
|
|
{ |
272
|
2 |
|
return $this->loader->load($this->resources[0]->getResource()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Processes an array of configurations. |
277
|
|
|
* |
278
|
|
|
* @param array $validated validated array |
279
|
|
|
* @param array $validating validating array |
280
|
|
|
* @param ConfigurationInterface $configuration configuration |
281
|
|
|
* @param ArrayNode $masterNode master node |
282
|
|
|
* |
283
|
|
|
* @return array list of (array, ArrayNode) |
284
|
|
|
*/ |
285
|
23 |
|
protected function processConfiguration( |
286
|
|
|
array $validated, |
287
|
|
|
array $validating, |
288
|
|
|
ConfigurationInterface $configuration, |
289
|
|
|
ArrayNode $masterNode = null |
290
|
|
|
) { |
291
|
23 |
|
$processor = new Processor(); |
292
|
|
|
|
293
|
23 |
|
return $processor->processConfiguration( |
294
|
23 |
|
$validated, |
295
|
23 |
|
$validating, |
296
|
23 |
|
$configuration, |
297
|
|
|
$masterNode |
298
|
23 |
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Creates master node. |
303
|
|
|
* |
304
|
|
|
* @param ConfigurationInterface $configuration |
|
|
|
|
305
|
|
|
* |
306
|
|
|
* @return ArrayNode |
307
|
|
|
*/ |
308
|
22 |
|
protected function createMasterNode() |
309
|
|
|
{ |
310
|
22 |
|
if (is_null($this->configuration)) { |
311
|
20 |
|
return $this->configuration; |
312
|
|
|
} else { |
313
|
2 |
|
return $this->configuration->getConfigTreeBuilder()->buildTree(); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.