1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flying\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base implementation of configurable class |
7
|
|
|
*/ |
8
|
|
|
abstract class AbstractConfig implements ConfigurableInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Mapping table between class name and its configuration options set |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private static $configCache = [ |
16
|
|
|
'classes_map' => [], |
17
|
|
|
'config' => [], |
18
|
|
|
'lazy_init' => [], |
19
|
|
|
]; |
20
|
|
|
/** |
21
|
|
|
* Configuration options |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
/** |
27
|
|
|
* List of configuration options that are not yet initialized |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $configPendingLazyInit = []; |
32
|
|
|
/** |
33
|
|
|
* TRUE if configuration options bootstrap is being performed, FALSE otherwise |
34
|
|
|
* |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
private $configInBootstrap = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
2 |
|
public function isConfigExists($name) |
43
|
|
|
{ |
44
|
2 |
|
if (!is_array($this->config)) { |
45
|
1 |
|
$this->bootstrapConfig(); |
46
|
1 |
|
} |
47
|
2 |
|
if (is_string($name) && ($name !== self::CLASS_ID_KEY)) { |
48
|
2 |
|
return array_key_exists($name, $this->config); |
49
|
|
|
} |
50
|
2 |
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Bootstrap object configuration options |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
63 |
|
protected function bootstrapConfig() |
59
|
|
|
{ |
60
|
63 |
|
if (is_array($this->config) || $this->configInBootstrap) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
63 |
|
$this->configInBootstrap = true; |
64
|
63 |
|
$id = $this->getConfigClassId(); |
65
|
63 |
|
if (!array_key_exists($id, self::$configCache['config'])) { |
66
|
63 |
|
$this->initConfig(); |
67
|
57 |
|
$lazy = []; |
68
|
57 |
|
foreach ($this->config as $name => $value) { |
69
|
57 |
|
if ($value === null) { |
70
|
26 |
|
$lazy[$name] = true; |
71
|
26 |
|
} |
72
|
57 |
|
} |
73
|
57 |
|
self::$configCache['config'][$id] = $this->config; |
74
|
57 |
|
self::$configCache['lazy_init'][$id] = $lazy; |
75
|
57 |
|
} |
76
|
57 |
|
$this->config = self::$configCache['config'][$id]; |
77
|
57 |
|
$this->configPendingLazyInit = self::$configCache['lazy_init'][$id]; |
78
|
57 |
|
$this->configInBootstrap = false; |
79
|
57 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get Id of configuration class that is used for given class |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
43 |
|
protected function getConfigClassId() |
87
|
|
|
{ |
88
|
43 |
|
$class = get_class($this); |
89
|
43 |
|
if (!array_key_exists($class, self::$configCache['classes_map'])) { |
90
|
|
|
// Determine which class actually defines configuration for given class |
91
|
|
|
// It is highly uncommon, but still possible situation when class |
92
|
|
|
// have no initConfig() method, so its configuration is completely inherited from parent, |
93
|
|
|
// but has validateConfig() method, so initial state of configuration can be different |
94
|
|
|
// from its parent. |
95
|
|
|
// To handle this properly we need to find earliest parent class that have either initConfig() |
96
|
|
|
// ot validateConfig() method and use is as a mapping target for current class |
97
|
|
|
try { |
98
|
43 |
|
$reflection = new \ReflectionClass($class); |
99
|
43 |
|
} catch (\ReflectionException $e) { |
100
|
|
|
return \stdClass::class; |
101
|
|
|
} |
102
|
43 |
|
$c = $class; |
103
|
|
|
do { |
104
|
43 |
|
if (($reflection->getMethod('initConfig')->getDeclaringClass()->getName() === $c) || |
105
|
3 |
|
($reflection->getMethod('validateConfig')->getDeclaringClass()->getName() === $c) |
106
|
43 |
|
) { |
107
|
43 |
|
break; |
108
|
|
|
} |
109
|
1 |
|
$reflection = $reflection->getParentClass(); |
110
|
1 |
|
$c = $reflection->getName(); |
111
|
1 |
|
} while ($reflection); |
112
|
43 |
|
self::$configCache['classes_map'][$class] = $reflection->getName(); |
113
|
43 |
|
} |
114
|
43 |
|
return self::$configCache['classes_map'][$class]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Initialize list of configuration options |
119
|
|
|
* |
120
|
|
|
* This method is mean to be overridden to provide configuration options set. |
121
|
|
|
* To allow inheritance of configuration options sets across several levels |
122
|
|
|
* of inherited classes - this method in nested classes should look like this: |
123
|
|
|
* |
124
|
|
|
* <code> |
125
|
|
|
* parent::initConfig(); |
126
|
|
|
* $this->mergeConfig([ |
127
|
|
|
* 'option' => 'default value', |
128
|
|
|
* ]); |
129
|
|
|
* </code> |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
63 |
|
protected function initConfig() |
134
|
|
|
{ |
135
|
63 |
|
$this->config = []; |
136
|
63 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
10 |
|
public function modifyConfig(array $config, $modification, $value = null) |
142
|
|
|
{ |
143
|
|
|
// Call getConfig() for given configuration options, but only if it is necessary |
144
|
|
|
// Without this check it is possible to get infinite recursion loop in a case |
145
|
|
|
// if getConfig() is overridden and calls modifyConfig() by itself |
146
|
10 |
|
if ((!array_key_exists(self::CLASS_ID_KEY, $config)) || |
147
|
10 |
|
($config[self::CLASS_ID_KEY] !== $this->getConfigClassId()) |
148
|
10 |
|
) { |
149
|
2 |
|
$config = $this->getConfig($config); |
150
|
2 |
|
} |
151
|
10 |
|
$modification = $this->configToArray($modification, $value, true); |
152
|
10 |
|
if ((!is_array($modification)) || (!count($modification))) { |
153
|
2 |
|
return $config; |
154
|
|
|
} |
155
|
8 |
|
foreach ($modification as $mName => $mValue) { |
156
|
8 |
|
if (!array_key_exists($mName, $this->config)) { |
157
|
4 |
|
continue; |
158
|
|
|
} |
159
|
4 |
|
if ($this->validateConfig($mName, $mValue)) { |
160
|
4 |
|
$config[$mName] = $mValue; |
161
|
4 |
|
} |
162
|
8 |
|
} |
163
|
8 |
|
return $config; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
* @param string|array|null $config |
169
|
|
|
* @param boolean $export |
170
|
|
|
* @return mixed |
171
|
|
|
* @throws \RuntimeException |
172
|
|
|
*/ |
173
|
54 |
|
public function getConfig($config = null, $export = false) |
174
|
|
|
{ |
175
|
54 |
|
if (!is_array($this->config)) { |
176
|
23 |
|
$this->bootstrapConfig(); |
177
|
19 |
|
} |
178
|
50 |
|
if (is_string($config)) { |
179
|
|
|
// This is request for configuration option value |
180
|
10 |
|
if (array_key_exists($config, $this->config)) { |
181
|
10 |
|
if (array_key_exists($config, $this->configPendingLazyInit)) { |
182
|
3 |
|
$this->resolveLazyConfigInit($config); |
183
|
3 |
|
} |
184
|
10 |
|
return $this->config[$config]; |
185
|
|
|
} |
186
|
2 |
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
43 |
|
if ($config === null) { |
190
|
|
|
// This is request for complete configuration options set |
191
|
37 |
|
$this->resolveLazyConfigInit(); |
192
|
36 |
|
$config = $this->config; |
193
|
36 |
|
if (!$export) { |
194
|
36 |
|
$config[self::CLASS_ID_KEY] = $this->getConfigClassId(); |
195
|
36 |
|
} |
196
|
36 |
|
return $config; |
197
|
|
|
} |
198
|
|
|
|
199
|
14 |
|
if (is_array($config) && |
200
|
14 |
|
array_key_exists(self::CLASS_ID_KEY, $config) && |
201
|
14 |
|
($config[self::CLASS_ID_KEY] === $this->getConfigClassId())) { |
202
|
|
|
// This is repetitive call to getConfig() |
203
|
2 |
|
return $config; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// This is request for configuration (with possible merging) |
207
|
12 |
|
$config = $this->configToArray($config); |
208
|
12 |
|
if (!is_array($config)) { |
209
|
2 |
|
$config = []; |
210
|
2 |
|
} |
211
|
12 |
|
$this->resolveLazyConfigInit(); |
212
|
12 |
|
$result = $this->config; |
213
|
12 |
|
if (!$export) { |
214
|
12 |
|
$result[self::CLASS_ID_KEY] = $this->getConfigClassId(); |
215
|
12 |
|
} |
216
|
12 |
|
foreach ($config as $name => $value) { |
217
|
10 |
|
if ($name !== self::CLASS_ID_KEY && array_key_exists($name, $result) && $this->validateConfig($name, $value)) { |
218
|
10 |
|
$result[$name] = $value; |
219
|
10 |
|
} |
220
|
12 |
|
} |
221
|
12 |
|
return $result; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
35 |
|
public function setConfig($config, $value = null) |
228
|
|
|
{ |
229
|
35 |
|
if (!is_array($this->config)) { |
230
|
13 |
|
$this->bootstrapConfig(); |
231
|
13 |
|
} |
232
|
35 |
|
$config = $this->configToArray($config, $value, true); |
233
|
35 |
|
if (is_array($config)) { |
234
|
35 |
|
foreach ($config as $ck => $cv) { |
235
|
22 |
|
if (array_key_exists($ck, $this->config) && $this->validateConfig($ck, $cv)) { |
236
|
16 |
|
$this->config[$ck] = $cv; |
237
|
16 |
|
unset($this->configPendingLazyInit[$ck]); |
238
|
16 |
|
$this->onConfigChange($ck, $cv); |
239
|
16 |
|
} |
240
|
33 |
|
} |
241
|
33 |
|
} |
242
|
33 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Attempt to convert given configuration information to array |
246
|
|
|
* |
247
|
|
|
* @param mixed $config Value to convert to array |
248
|
|
|
* @param mixed $value OPTIONAL Array entry value for inline array entry |
249
|
|
|
* @param boolean $inline OPTIONAL TRUE to allow treating given string values as array entry |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
45 |
|
protected function configToArray($config, $value = null, $inline = false) |
253
|
|
|
{ |
254
|
45 |
|
if (is_object($config)) { |
255
|
10 |
|
if (is_callable([$config, 'toArray'])) { |
256
|
4 |
|
$config = $config->toArray(); |
257
|
10 |
|
} elseif ($config instanceof \Iterator) { |
258
|
6 |
|
$config = iterator_to_array($config, true); |
259
|
10 |
|
} elseif ($config instanceof \ArrayAccess) { |
260
|
8 |
|
$temp = []; |
261
|
8 |
|
foreach ($this->config as $k => $v) { |
262
|
8 |
|
if ($k !== ConfigurableInterface::CLASS_ID_KEY && $config->offsetExists($k)) { |
263
|
4 |
|
$temp[$k] = $config->offsetGet($k); |
264
|
4 |
|
} |
265
|
8 |
|
} |
266
|
8 |
|
$config = $temp; |
267
|
8 |
|
} |
268
|
10 |
|
} |
269
|
45 |
|
if ($inline && is_string($config) && '' !== $config) { |
270
|
15 |
|
$config = [$config => $value]; |
271
|
15 |
|
} |
272
|
45 |
|
return $config; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Check that given value of configuration option is valid |
277
|
|
|
* |
278
|
|
|
* This method is mean to be overridden in a case if additional validation |
279
|
|
|
* of configuration option value should be performed before using it |
280
|
|
|
* Method should validate and, if required, normalize given value |
281
|
|
|
* of configuration option and return true if option can be used and false if not |
282
|
|
|
* It is important that this method will be: |
283
|
|
|
* - as simple as possible to optimize performance |
284
|
|
|
* - will not call other methods that attempts to modify or merge object configuration |
285
|
|
|
* to avoid infinite loop |
286
|
|
|
* Normally this method should look like this: |
287
|
|
|
* |
288
|
|
|
* <code> |
289
|
|
|
* switch($name) { |
290
|
|
|
* case 'option': |
291
|
|
|
* // $value validation and normalization code |
292
|
|
|
* break; |
293
|
|
|
* default: |
294
|
|
|
* return parent::validateConfig($name, $value); |
295
|
|
|
* break; |
296
|
|
|
* } |
297
|
|
|
* </code> |
298
|
|
|
* |
299
|
|
|
* @param string $name Configuration option name |
300
|
|
|
* @param mixed $value Option value (passed by reference) |
301
|
|
|
* @return boolean |
302
|
|
|
*/ |
303
|
2 |
|
protected function validateConfig($name, &$value) |
304
|
|
|
{ |
305
|
2 |
|
return true; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Perform required operations when configuration option value is changed |
310
|
|
|
* |
311
|
|
|
* This method is mean to be overridden in a case if some kind of additional logic |
312
|
|
|
* is required to be performed upon setting value of configuration option. |
313
|
|
|
* |
314
|
|
|
* @param string $name Configuration option name |
315
|
|
|
* @param mixed $value Configuration option value |
316
|
|
|
* @return void |
317
|
|
|
*/ |
318
|
10 |
|
protected function onConfigChange($name, $value) |
319
|
|
|
{ |
320
|
10 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Resolve lazy initialization of configuration options |
324
|
|
|
* |
325
|
|
|
* @param string $name OPTIONAL Configuration option to perform lazy initialization of |
326
|
|
|
* @throws \RuntimeException |
327
|
|
|
* @return void |
328
|
|
|
*/ |
329
|
45 |
|
protected function resolveLazyConfigInit($name = null) |
330
|
|
|
{ |
331
|
45 |
|
if (!count($this->configPendingLazyInit)) { |
332
|
32 |
|
return; |
333
|
|
|
} |
334
|
18 |
|
if ($name !== null) { |
335
|
3 |
|
$options = array_key_exists($name, $this->configPendingLazyInit) ? [$name] : []; |
336
|
3 |
|
} else { |
337
|
16 |
|
$options = array_keys($this->configPendingLazyInit); |
338
|
|
|
} |
339
|
18 |
|
foreach ($options as $oName) { |
340
|
18 |
|
$value = $this->lazyConfigInit($oName); |
341
|
18 |
|
if ($this->validateConfig($oName, $value)) { |
342
|
18 |
|
$this->config[$oName] = $value; |
343
|
18 |
|
unset($this->configPendingLazyInit[$oName]); |
344
|
18 |
|
} else { |
345
|
1 |
|
throw new \RuntimeException('Lazily initialized configuration option "' . $oName . '" is not passed validation check'); |
346
|
|
|
} |
347
|
18 |
|
} |
348
|
17 |
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Perform "lazy initialization" of configuration option with given name |
352
|
|
|
* |
353
|
|
|
* @param string $name Configuration option name |
354
|
|
|
* @return mixed |
355
|
|
|
*/ |
356
|
|
|
protected function lazyConfigInit($name) |
357
|
|
|
{ |
358
|
|
|
return null; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Merge given configuration options with current configuration options |
363
|
|
|
* |
364
|
|
|
* @param array $config Configuration options to merge |
365
|
|
|
* @throws \RuntimeException |
366
|
|
|
* @throws \InvalidArgumentException |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
65 |
|
protected function mergeConfig(array $config) |
370
|
|
|
{ |
371
|
65 |
|
if (!$this->configInBootstrap) { |
372
|
2 |
|
throw new \RuntimeException('mergeConfig() can only be used for configuration initialization'); |
373
|
|
|
} |
374
|
63 |
|
if (is_int(key($config)) && (array_keys($config) === range(0, count($config) - 1))) { |
375
|
|
|
// Configuration is defined as array of keys with lazy initialization |
376
|
26 |
|
if (array_reduce($config, function ($n, $v) { |
377
|
26 |
|
return $n || !is_string($v); |
378
|
26 |
|
}, false)) { |
379
|
2 |
|
throw new \InvalidArgumentException('Lazy configuration should be list of string configuration keys'); |
380
|
|
|
} |
381
|
24 |
|
$this->config = array_merge($this->config, array_fill_keys($config, null)); |
382
|
24 |
|
} else { |
383
|
|
|
// Configuration is given as normal key->value array |
384
|
37 |
|
foreach ($config as $key => $value) { |
385
|
37 |
|
if ($value !== null) { |
386
|
37 |
|
if ((!is_scalar($value)) && (!is_array($value))) { |
387
|
2 |
|
throw new \InvalidArgumentException(sprintf('Non-scalar initial value for configuration option "%s" for class "%s"', $key, get_class($this))); |
388
|
|
|
} |
389
|
35 |
|
if (!$this->validateConfig($key, $value)) { |
390
|
2 |
|
throw new \RuntimeException(sprintf('Invalid initial value for configuration option "%s" for class "%s"', $key, get_class($this))); |
391
|
|
|
} |
392
|
35 |
|
} |
393
|
35 |
|
$this->config[$key] = $value; |
394
|
35 |
|
} |
395
|
|
|
} |
396
|
59 |
|
} |
397
|
|
|
} |
398
|
|
|
|