-
Notifications
You must be signed in to change notification settings - Fork 136
/
AbstractConfig.php
299 lines (266 loc) · 6.87 KB
/
AbstractConfig.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace Noodlehaus;
use ArrayAccess;
use Iterator;
/**
* Abstract Config class
*
* @package Config
* @author Jesus A. Domingo <[email protected]>
* @author Hassan Khan <[email protected]>
* @link https://github.com/noodlehaus/config
* @license MIT
*/
abstract class AbstractConfig implements ArrayAccess, ConfigInterface, Iterator
{
/**
* Stores the configuration data
*
* @var array|null
*/
protected $data = null;
/**
* Caches the configuration data
*
* @var array
*/
protected $cache = [];
/**
* Constructor method and sets default options, if any
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = array_merge($this->getDefaults(), $data);
}
/**
* Override this method in your own subclass to provide an array of default
* options and values
*
* @return array
*
* @codeCoverageIgnore
*/
protected function getDefaults()
{
return [];
}
/**
* ConfigInterface Methods
*/
/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
if ($this->has($key)) {
return $this->cache[$key];
}
return $default;
}
/**
* {@inheritDoc}
*/
public function set($key, $value)
{
$segs = explode('.', $key);
$root = &$this->data;
$cacheKey = '';
// Look for the key, creating nested keys if needed
while ($part = array_shift($segs)) {
if ($cacheKey != '') {
$cacheKey .= '.';
}
$cacheKey .= $part;
if (!isset($root[$part]) && count($segs)) {
$root[$part] = [];
}
$root = &$root[$part];
//Unset all old nested cache
if (isset($this->cache[$cacheKey])) {
unset($this->cache[$cacheKey]);
}
//Unset all old nested cache in case of array
if (count($segs) == 0) {
foreach ($this->cache as $cacheLocalKey => $cacheValue) {
if (substr($cacheLocalKey, 0, strlen($cacheKey)) === $cacheKey) {
unset($this->cache[$cacheLocalKey]);
}
}
}
}
// Assign value at target node
$this->cache[$key] = $root = $value;
}
/**
* {@inheritDoc}
*/
public function has($key)
{
// Check if already cached
if (isset($this->cache[$key])) {
return true;
}
$segments = explode('.', $key);
$root = $this->data;
// nested case
foreach ($segments as $segment) {
if (array_key_exists($segment, $root)) {
$root = $root[$segment];
continue;
} else {
return false;
}
}
// Set cache for the given key
$this->cache[$key] = $root;
return true;
}
/**
* Merge config from another instance
*
* @param ConfigInterface $config
* @return ConfigInterface
*/
public function merge(ConfigInterface $config)
{
$this->data = array_replace_recursive($this->data, $config->all());
$this->cache = [];
return $this;
}
/**
* {@inheritDoc}
*/
public function all()
{
return $this->data;
}
/**
* ArrayAccess Methods
*/
/**
* Gets a value using the offset as a key
*
* @param string $offset
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Checks if a key exists
*
* @param string $offset
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Sets a value using the offset as a key
*
* @param string $offset
* @param mixed $value
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Deletes a key and its value
*
* @param string $offset
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->set($offset, null);
}
/**
* Iterator Methods
*/
/**
* Returns the data array element referenced by its internal cursor
*
* @return mixed The element referenced by the data array's internal cursor.
* If the array is empty or there is no element at the cursor, the
* function returns false. If the array is undefined, the function
* returns null
*/
#[\ReturnTypeWillChange]
public function current()
{
return (is_array($this->data) ? current($this->data) : null);
}
/**
* Returns the data array index referenced by its internal cursor
*
* @return mixed The index referenced by the data array's internal cursor.
* If the array is empty or undefined or there is no element at the
* cursor, the function returns null
*/
#[\ReturnTypeWillChange]
public function key()
{
return (is_array($this->data) ? key($this->data) : null);
}
/**
* Moves the data array's internal cursor forward one element
*
* @return mixed The element referenced by the data array's internal cursor
* after the move is completed. If there are no more elements in the
* array after the move, the function returns false. If the data array
* is undefined, the function returns null
*/
#[\ReturnTypeWillChange]
public function next()
{
return (is_array($this->data) ? next($this->data) : null);
}
/**
* Moves the data array's internal cursor to the first element
*
* @return mixed The element referenced by the data array's internal cursor
* after the move is completed. If the data array is empty, the function
* returns false. If the data array is undefined, the function returns
* null
*/
#[\ReturnTypeWillChange]
public function rewind()
{
return (is_array($this->data) ? reset($this->data) : null);
}
/**
* Tests whether the iterator's current index is valid
*
* @return bool True if the current index is valid; false otherwise
*/
#[\ReturnTypeWillChange]
public function valid()
{
return (is_array($this->data) ? key($this->data) !== null : false);
}
/**
* Remove a value using the offset as a key
*
* @param string $key
*
* @return void
*/
public function remove($key)
{
$this->offsetUnset($key);
}
}