1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Config; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Iterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Repository |
11
|
|
|
* @package Nip\Config |
12
|
|
|
*/ |
13
|
|
|
class Config implements Countable, Iterator, ArrayAccess |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Whether modifications to configuration data are allowed. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $allowModifications; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* All of the configuration items. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $data = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Used when unsetting values during iteration to ensure we do not skip |
31
|
|
|
* the next element. |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $skipNextIteration; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new configuration repository. |
39
|
|
|
* |
40
|
|
|
* @param array $array |
41
|
|
|
* @param bool $allowModifications |
42
|
|
|
*/ |
43
|
1 |
|
public function __construct(array $array = [], $allowModifications = false) |
44
|
|
|
{ |
45
|
1 |
|
$this->allowModifications = (bool) $allowModifications; |
46
|
|
|
|
47
|
1 |
|
foreach ($array as $key => $value) { |
48
|
1 |
|
$this->setDataItem($key, $value); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $name |
54
|
|
|
* @param $value |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
1 |
|
protected function setDataItem($name, $value) |
58
|
|
|
{ |
59
|
1 |
|
if (is_array($value)) { |
60
|
1 |
|
$value = new static($value, true); |
61
|
|
|
} |
62
|
1 |
|
if (null === $name) { |
63
|
|
|
$this->data[] = $value; |
64
|
|
|
} else { |
65
|
1 |
|
$this->data[$name] = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $name |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function __get($name) |
76
|
|
|
{ |
77
|
|
|
return $this->get($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieve a value and return $default if there is no element set. |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param string $default |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
1 |
|
public function get($key, $default = null) |
88
|
|
|
{ |
89
|
1 |
|
if (strpos($key, '.') === false) { |
90
|
1 |
|
$value = $this->getByKey($key); |
91
|
|
|
} else { |
92
|
1 |
|
$value = $this->getByPath($key); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $value === null ? $default : $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $key |
100
|
|
|
* @return mixed|null |
101
|
|
|
*/ |
102
|
1 |
|
public function getByKey($key) |
103
|
|
|
{ |
104
|
1 |
|
if ($this->hasByKey($key)) { |
105
|
1 |
|
return $this->data[$key]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Determine if the given configuration value exists. |
113
|
|
|
* |
114
|
|
|
* @param string $key |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
1 |
|
public function hasByKey($key) |
118
|
|
|
{ |
119
|
1 |
|
return isset($this->data[$key]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $path |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
View Code Duplication |
protected function getByPath($path) |
|
|
|
|
127
|
|
|
{ |
128
|
1 |
|
$segments = explode('.', $path); |
129
|
1 |
|
$value = $this; |
130
|
1 |
|
foreach ($segments as $segment) { |
131
|
1 |
|
if ($value->hasByKey($segment)) { |
132
|
1 |
|
$value = $value->getByKey($segment); |
133
|
|
|
} else { |
134
|
1 |
|
return null; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $key |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function __isset($key) |
146
|
|
|
{ |
147
|
|
|
return $this->has($key); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine if the given configuration value exists. |
152
|
|
|
* |
153
|
|
|
* @param string $key |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
1 |
|
public function has($key) |
157
|
|
|
{ |
158
|
1 |
|
if (strpos($key, '.') === false) { |
159
|
1 |
|
return $this->hasByKey($key); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $this->hasByPath($key); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $path |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
1 |
View Code Duplication |
public function hasByPath($path) |
|
|
|
|
170
|
|
|
{ |
171
|
1 |
|
$segments = explode('.', $path); |
172
|
1 |
|
$value = $this; |
173
|
1 |
|
foreach ($segments as $segment) { |
174
|
1 |
|
if ($value->hasByKey($segment)) { |
175
|
1 |
|
$value = $value->getByKey($segment); |
176
|
|
|
} else { |
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
return ($value !== null); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Determine if the given configuration option exists. |
186
|
|
|
* |
187
|
|
|
* @param string $key |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function offsetExists($key) |
191
|
|
|
{ |
192
|
|
|
return $this->has($key); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get a configuration option. |
197
|
|
|
* |
198
|
|
|
* @param string $key |
199
|
|
|
* @return mixed |
200
|
|
|
*/ |
201
|
|
|
public function offsetGet($key) |
202
|
|
|
{ |
203
|
|
|
return $this->get($key); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set a configuration option. |
208
|
|
|
* |
209
|
|
|
* @param string $key |
210
|
|
|
* @param mixed $value |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function offsetSet($key, $value) |
214
|
|
|
{ |
215
|
|
|
$this->set($key, $value); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param $name |
220
|
|
|
* @param $value |
221
|
|
|
* @return $this |
222
|
|
|
* @throws Exception\RuntimeException |
223
|
|
|
*/ |
224
|
|
|
public function set($name, $value) |
225
|
|
|
{ |
226
|
|
|
if ($this->isReadOnly()) { |
227
|
|
|
throw new Exception\RuntimeException('Config is read only'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this->setDataItem($name, $value); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns whether this Config object is read only or not. |
235
|
|
|
* |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function isReadOnly() |
239
|
|
|
{ |
240
|
|
|
return !$this->allowModifications; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Unset a configuration option. |
245
|
|
|
* |
246
|
|
|
* @param string $key |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
public function offsetUnset($key) |
250
|
|
|
{ |
251
|
|
|
$this->set($key, null); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $path |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
1 |
|
public function mergeFile($path) |
259
|
|
|
{ |
260
|
1 |
|
$data = Factory::fromFile($path, false); |
261
|
1 |
|
$config = new self($data, $this->allowModifications); |
|
|
|
|
262
|
1 |
|
return $this->merge($config); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Merge another Config with this one. |
267
|
|
|
* |
268
|
|
|
* For duplicate keys, the following will be performed: |
269
|
|
|
* - Nested Configs will be recursively merged. |
270
|
|
|
* - Items in $merge with INTEGER keys will be appended. |
271
|
|
|
* - Items in $merge with STRING keys will overwrite current values. |
272
|
|
|
* |
273
|
|
|
* @param self $merge |
274
|
|
|
* @return $this |
275
|
|
|
*/ |
276
|
1 |
|
public function merge(self $merge) |
277
|
|
|
{ |
278
|
|
|
/** @var self $value */ |
279
|
1 |
|
foreach ($merge as $key => $value) { |
280
|
1 |
|
if (array_key_exists($key, $this->data)) { |
281
|
|
|
if (is_int($key)) { |
282
|
|
|
$this->data[] = $value; |
283
|
|
|
} elseif ($value instanceof self && $this->data[$key] instanceof self) { |
284
|
|
|
$this->data[$key]->merge($value); |
285
|
|
View Code Duplication |
} else { |
|
|
|
|
286
|
|
|
if ($value instanceof self) { |
287
|
|
|
$this->data[$key] = new static($value->toArray(), $this->allowModifications); |
288
|
|
|
} else { |
289
|
|
|
$this->data[$key] = $value; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
View Code Duplication |
} else { |
|
|
|
|
293
|
1 |
|
if ($value instanceof self) { |
294
|
1 |
|
$this->data[$key] = new static($value->toArray(), $this->allowModifications); |
295
|
|
|
} else { |
296
|
1 |
|
$this->data[$key] = $value; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
1 |
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Return an associative array of the stored data. |
306
|
|
|
* |
307
|
|
|
* @return array |
308
|
|
|
*/ |
309
|
1 |
|
public function toArray() |
310
|
|
|
{ |
311
|
1 |
|
$array = []; |
312
|
1 |
|
$data = $this->data; |
313
|
|
|
/** @var self $value */ |
314
|
1 |
|
foreach ($data as $key => $value) { |
315
|
1 |
|
if ($value instanceof self) { |
316
|
1 |
|
$array[$key] = $value->toArray(); |
317
|
|
|
} else { |
318
|
1 |
|
$array[$key] = $value; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
1 |
|
return $array; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* count(): defined by Countable interface. |
327
|
|
|
* |
328
|
|
|
* @see Countable::count() |
329
|
|
|
* @return int |
330
|
|
|
*/ |
331
|
|
|
public function count() |
332
|
|
|
{ |
333
|
|
|
return count($this->data); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* current(): defined by Iterator interface. |
338
|
|
|
* |
339
|
|
|
* @see Iterator::current() |
340
|
|
|
* @return mixed |
341
|
|
|
*/ |
342
|
1 |
|
public function current() |
343
|
|
|
{ |
344
|
1 |
|
$this->skipNextIteration = false; |
345
|
|
|
|
346
|
1 |
|
return current($this->data); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* next(): defined by Iterator interface. |
351
|
|
|
* |
352
|
|
|
* @see Iterator::next() |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
1 |
|
public function next() |
356
|
|
|
{ |
357
|
1 |
|
if ($this->skipNextIteration) { |
358
|
|
|
$this->skipNextIteration = false; |
359
|
|
|
|
360
|
|
|
return; |
361
|
|
|
} |
362
|
1 |
|
next($this->data); |
363
|
1 |
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* rewind(): defined by Iterator interface. |
367
|
|
|
* |
368
|
|
|
* @see Iterator::rewind() |
369
|
|
|
* @return void |
370
|
|
|
*/ |
371
|
1 |
|
public function rewind() |
372
|
|
|
{ |
373
|
1 |
|
$this->skipNextIteration = false; |
374
|
1 |
|
reset($this->data); |
375
|
1 |
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* valid(): defined by Iterator interface. |
379
|
|
|
* |
380
|
|
|
* @see Iterator::valid() |
381
|
|
|
* @return bool |
382
|
|
|
*/ |
383
|
1 |
|
public function valid() |
384
|
|
|
{ |
385
|
1 |
|
return ($this->key() !== null); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* key(): defined by Iterator interface. |
390
|
|
|
* |
391
|
|
|
* @see Iterator::key() |
392
|
|
|
* @return mixed |
393
|
|
|
*/ |
394
|
1 |
|
public function key() |
395
|
|
|
{ |
396
|
1 |
|
return key($this->data); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.