1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Closure; |
7
|
|
|
|
8
|
|
|
class Config implements ArrayAccess |
9
|
|
|
{ |
10
|
|
|
protected $app; |
11
|
|
|
protected $converter; |
12
|
|
|
|
13
|
|
|
public function __construct(Application $app, Framework $framework, Filesystem $fs, Converter $converter) |
14
|
|
|
{ |
15
|
|
|
$this->app = $app; |
16
|
|
|
$this->converter = $converter; |
17
|
|
|
|
18
|
|
|
$filename = $fs->getPath("config.php"); |
19
|
|
|
if (!file_exists($filename)) { |
20
|
|
|
$filename = $framework->getPath('resources/default/config.php'); |
21
|
|
|
} |
22
|
|
|
$data = include $filename; |
23
|
|
|
foreach ($data as $k => $v) { |
24
|
|
|
$this->$k = $v; |
25
|
|
|
if ($v instanceof Closure) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
if (is_array($v) || is_object($v)) { |
29
|
|
|
$this->$k = $converter->toObject($v); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function offsetExists($offset) |
35
|
|
|
{ |
36
|
1 |
|
return $this->getNode($offset) != null; |
37
|
|
|
} |
38
|
|
|
|
39
|
56 |
|
public function offsetGet($offset) |
40
|
|
|
{ |
41
|
56 |
|
$value = $this->getNode($offset); |
42
|
56 |
|
if (!is_object($value)) { |
43
|
56 |
|
return $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
return $this->converter->toArray($value); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function offsetSet($offset, $value) |
50
|
|
|
{ |
51
|
1 |
|
$path = explode('.', $offset); |
52
|
1 |
|
$key = array_pop($path); |
53
|
1 |
|
$parent = $this->getNode(implode('.', $path), true); |
54
|
1 |
|
if (is_array($value) || is_object($value)) { |
55
|
|
|
$value = $this->converter->toObject($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return $parent->$key = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function offsetUnset($offset) |
62
|
|
|
{ |
63
|
1 |
|
$path = explode('.', $offset); |
64
|
1 |
|
$key = array_pop($path); |
65
|
1 |
|
$parent = $this->getNode(implode('.', $path)); |
66
|
1 |
|
unset($parent->$key); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
public function require(string $offset) |
70
|
|
|
{ |
71
|
|
|
if (!isset($this[$offset])) { |
72
|
|
|
throw new Exception("No offset $offset"); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
56 |
|
private function getNode(string $offset, bool $createIfNone = false) |
77
|
|
|
{ |
78
|
56 |
|
if (!$offset) { |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
56 |
|
$current = $this; |
82
|
56 |
|
foreach (explode('.', $offset) as $key) { |
83
|
56 |
|
if (is_object($current)) { |
84
|
56 |
|
if (!property_exists($current, $key)) { |
85
|
1 |
|
if ($createIfNone) { |
86
|
1 |
|
$current->$key = (object) []; |
87
|
|
|
} else { |
88
|
1 |
|
return null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
56 |
|
if ($current->$key instanceof Closure) { |
92
|
56 |
|
$callback = $current->$key; |
93
|
56 |
|
$v = $callback($this->app); |
94
|
|
|
|
95
|
56 |
|
if (is_array($v) || is_object($v)) { |
96
|
56 |
|
$v = $this->converter->toObject($v); |
97
|
|
|
} |
98
|
56 |
|
$current->$key = $v; |
99
|
|
|
} |
100
|
|
|
|
101
|
56 |
|
$current = $current->$key; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
56 |
|
return $current; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|