1 | <?php |
||
7 | class Config implements ArrayAccess |
||
8 | { |
||
9 | private $converter; |
||
10 | |||
11 | 12 | public function __construct(Framework $framework, Filesystem $fs, Converter $converter) |
|
12 | { |
||
13 | 12 | $this->converter = $converter; |
|
14 | |||
15 | 12 | $filename = $fs->getPath("config.php"); |
|
16 | 12 | if (!file_exists($filename)) { |
|
17 | $filename = $framework->getPath('resources/default/config.php'); |
||
18 | } |
||
19 | 12 | $data = include $filename; |
|
20 | 12 | foreach ($data as $k => $v) { |
|
21 | 12 | if (is_callable($v)) { |
|
22 | 12 | $v = $v(); |
|
23 | } |
||
24 | 12 | $this->$k = $v; |
|
25 | 12 | if (is_array($v) || is_object($v)) { |
|
26 | 12 | $this->$k = $converter->toObject($v); |
|
27 | } |
||
28 | } |
||
29 | 12 | } |
|
30 | |||
31 | 1 | public function offsetExists($offset) |
|
32 | { |
||
33 | 1 | return $this->getNode($offset) != null; |
|
34 | } |
||
35 | |||
36 | 12 | public function offsetGet($offset) |
|
45 | |||
46 | 1 | public function offsetSet($offset, $value) |
|
57 | |||
58 | 1 | public function offsetUnset($offset) |
|
59 | { |
||
60 | 1 | $path = explode('.', $offset); |
|
61 | 1 | $key = array_pop($path); |
|
62 | 1 | $parent = $this->getNode(implode('.', $path)); |
|
63 | 1 | unset($parent->$key); |
|
64 | 1 | } |
|
65 | |||
66 | public function require(string $offset) |
||
72 | |||
73 | 12 | private function getNode(string $offset, bool $createIfNone = false) |
|
74 | { |
||
94 | } |
||
95 |