|
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
|
|
|
/** |
|
16
|
|
|
* Config constructor. |
|
17
|
|
|
* @param array $arrayConfig |
|
18
|
|
|
* @throws CanNotMutateException |
|
19
|
|
|
*/ |
|
20
|
67 |
|
public function __construct(array $arrayConfig = []) { |
|
21
|
67 |
|
parent::__construct(); |
|
22
|
|
|
|
|
23
|
67 |
|
$this->import($this->defaults()); |
|
24
|
67 |
|
$this->import($arrayConfig); |
|
25
|
67 |
|
$this->onConstruct(); |
|
26
|
67 |
|
} |
|
27
|
|
|
|
|
28
|
67 |
|
public function import(array $data) { |
|
29
|
67 |
|
foreach ($data as $key=>$value) $this->offsetSet($key, $value); |
|
30
|
67 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
// override this to have a base config |
|
33
|
65 |
|
protected function defaults(): array { |
|
34
|
65 |
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// override this if you want to |
|
38
|
|
|
protected function onConstruct() {} |
|
39
|
|
|
|
|
40
|
57 |
|
public function isLocked() { |
|
41
|
57 |
|
return $this->locked; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
18 |
|
public function get($key, $default = null) { |
|
45
|
18 |
|
return $this->offsetExists($key) ? $this->offsetGet($key) : $default; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function has($key) { |
|
49
|
4 |
|
return $this->offsetExists($key); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
public function toArray() { |
|
53
|
6 |
|
return array_map( |
|
54
|
6 |
|
function ($value) { |
|
55
|
|
|
// $value may be another Config object, if so then we toArray() it also. |
|
56
|
6 |
|
return $this->hasToArray($value) ? $value->toArray() : $value; |
|
57
|
6 |
|
}, |
|
58
|
6 |
|
$this->rawValues() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
private function hasToArray($value): bool { |
|
63
|
6 |
|
return is_object($value) |
|
64
|
6 |
|
&& method_exists($value, 'toArray'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
public function path($path, $default = null, $delimiter = '.') { |
|
68
|
5 |
|
return (new PathEval($this))->find($path, $default, $delimiter); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
# ArrayAccess |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $offset |
|
76
|
|
|
* @param $value |
|
77
|
|
|
* @throws CanNotMutateException |
|
78
|
|
|
*/ |
|
79
|
57 |
|
public function offsetSet($offset, $value) { |
|
80
|
57 |
|
$this->blockIfLocked(); |
|
81
|
57 |
|
parent::offsetSet($offset, $this->settableValue($value)); |
|
82
|
57 |
|
} |
|
83
|
|
|
|
|
84
|
57 |
|
protected function settableValue($value) { |
|
85
|
57 |
|
if (is_array($value)) return $this->subConfig($value); |
|
86
|
57 |
|
return $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function subConfig($arrayConfig = []) { |
|
90
|
|
|
return new class($arrayConfig) extends Config {}; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $offset |
|
95
|
|
|
* @throws CanNotMutateException |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function offsetUnset($offset) { |
|
98
|
6 |
|
$this->blockIfLocked(); |
|
99
|
4 |
|
parent::offsetUnset($offset); |
|
100
|
4 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
1 |
|
public static function __set_state(array $data): Config { |
|
104
|
1 |
|
return new static($data); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
14 |
|
public function lock() { |
|
108
|
14 |
|
$this->locked = true; |
|
109
|
14 |
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
protected function isConfigObject($value) { |
|
112
|
5 |
|
return $value instanceof Config; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @throws CanNotMutateException |
|
117
|
|
|
*/ |
|
118
|
57 |
|
private function blockIfLocked() { |
|
119
|
57 |
|
if ($this->isLocked()) |
|
120
|
6 |
|
throw new CanNotMutateException(); |
|
121
|
|
|
} |
|
122
|
|
|
} |