1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Logikos\Util; |
4
|
|
|
|
5
|
|
|
use Logikos\Util\Config\PathEval; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is largely inspired by \Phalcon\Config - https://docs.phalconphp.com/hr/3.2/api/Phalcon_Config |
9
|
|
|
* NOTICE: \Phalcon\Config will be much faster than this class and you are encouraged to use it |
10
|
|
|
* @see ../docs/config/README.md |
11
|
|
|
*/ |
12
|
|
|
abstract class Config extends Registry { |
13
|
|
|
private $locked = false; |
14
|
|
|
|
15
|
50 |
|
public function __construct(array $arrayConfig = []) { |
16
|
50 |
|
parent::__construct(); |
17
|
50 |
|
foreach($arrayConfig as $key => $value) |
18
|
45 |
|
$this->offsetSet($key, $value); |
19
|
|
|
|
20
|
50 |
|
$this->onConstruct(); |
21
|
50 |
|
} |
22
|
|
|
|
23
|
|
|
// override this if you want to |
24
|
|
|
protected function onConstruct() {} |
25
|
|
|
|
26
|
45 |
|
public function isLocked() { |
27
|
45 |
|
return $this->locked; |
28
|
|
|
} |
29
|
|
|
|
30
|
8 |
|
public function get($key, $default = null) { |
31
|
8 |
|
return $this->offsetExists($key) ? $this->offsetGet($key) : $default; |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function has($key) { |
35
|
1 |
|
return $this->offsetExists($key); |
36
|
|
|
} |
37
|
|
|
|
38
|
6 |
|
public function toArray() { |
39
|
6 |
|
return array_map( |
40
|
6 |
|
function ($value) { |
41
|
6 |
|
return $this->hasToArray($value) ? $value->toArray() : $value; |
42
|
6 |
|
}, |
43
|
6 |
|
$this->rawValues() |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
private function hasToArray($value): bool { |
48
|
6 |
|
return is_object($value) |
49
|
6 |
|
&& method_exists($value, 'toArray'); |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
public function path($path, $default = null, $delimiter = '.') { |
53
|
5 |
|
return (new PathEval($this))->find($path, $default, $delimiter); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
# ArrayAccess |
58
|
45 |
|
public function offsetSet($offset, $value) { |
59
|
45 |
|
$this->blockIfLocked(); |
60
|
45 |
|
parent::offsetSet( |
61
|
45 |
|
$offset, |
62
|
45 |
|
is_array($value) ? new static($value) : $value |
63
|
|
|
); |
64
|
45 |
|
} |
65
|
|
|
|
66
|
4 |
|
public function offsetUnset($offset) { |
67
|
4 |
|
$this->blockIfLocked(); |
68
|
2 |
|
parent::offsetUnset($offset); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
1 |
|
public static function __set_state(array $data): Config { |
73
|
1 |
|
return new static($data); |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
public function lock() { |
77
|
12 |
|
$this->locked = true; |
78
|
12 |
|
} |
79
|
|
|
|
80
|
5 |
|
protected function isConfigObject($value) { |
81
|
5 |
|
return $value instanceof Config; |
82
|
|
|
} |
83
|
|
|
|
84
|
45 |
|
private function blockIfLocked() { |
85
|
45 |
|
if ($this->isLocked()) |
|
|
|
|
86
|
6 |
|
throw new CanNotMutateException(); |
87
|
|
|
} |
88
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.