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 | public function offsetExists($offset) |
||
35 | |||
36 | 12 | public function offsetGet($offset) |
|
45 | |||
46 | public function offsetSet($offset, $value) |
||
47 | { |
||
48 | $path = explode('.', $offset); |
||
49 | $key = array_pop($path); |
||
50 | $parent = $this->getNode(implode('.', $path), true); |
||
51 | if (is_array($value) || is_object($value)) { |
||
52 | $value = $this->converter->toObject($value); |
||
53 | } |
||
54 | |||
55 | return $parent->$key = $value; |
||
56 | } |
||
57 | |||
58 | public function offsetUnset($offset) |
||
65 | |||
66 | public function require(string $offset) |
||
67 | { |
||
68 | if (!isset($this[$offset])) { |
||
69 | throw new Exception("No offset $offset"); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | 12 | private function getNode(string $offset, bool $createIfNone = false) |
|
94 | } |
||
95 |