|
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
|
44 |
|
public function __construct(Application $app, Framework $framework, Filesystem $fs, Converter $converter) |
|
14
|
|
|
{ |
|
15
|
44 |
|
$this->app = $app; |
|
16
|
44 |
|
$this->converter = $converter; |
|
17
|
|
|
|
|
18
|
44 |
|
$filename = $fs->getPath("config.php"); |
|
19
|
44 |
|
if (!file_exists($filename)) { |
|
20
|
|
|
$filename = $framework->getPath('resources/default/config.php'); |
|
21
|
|
|
} |
|
22
|
44 |
|
$data = include $filename; |
|
23
|
44 |
|
foreach ($data as $k => $v) { |
|
24
|
44 |
|
$this->$k = $v; |
|
25
|
44 |
|
if ($v instanceof Closure) { |
|
26
|
44 |
|
continue; |
|
27
|
|
|
} |
|
28
|
44 |
|
if (is_array($v) || is_object($v)) { |
|
29
|
44 |
|
$this->$k = $converter->toObject($v); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
44 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function offsetExists($offset) |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->getNode($offset) != null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
44 |
|
public function offsetGet($offset) |
|
40
|
|
|
{ |
|
41
|
44 |
|
$value = $this->getNode($offset); |
|
42
|
44 |
|
if (!is_object($value)) { |
|
43
|
44 |
|
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
|
44 |
|
private function getNode(string $offset, bool $createIfNone = false) |
|
77
|
|
|
{ |
|
78
|
44 |
|
if (!$offset) { |
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
44 |
|
$current = $this; |
|
82
|
44 |
|
foreach (explode('.', $offset) as $key) { |
|
83
|
44 |
|
if (is_object($current)) { |
|
84
|
44 |
|
if (!property_exists($current, $key)) { |
|
85
|
1 |
|
if ($createIfNone) { |
|
86
|
1 |
|
$current->$key = (object) []; |
|
87
|
|
|
} else { |
|
88
|
1 |
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
44 |
|
if ($current->$key instanceof Closure) { |
|
92
|
44 |
|
$callback = $current->$key; |
|
93
|
44 |
|
$v = $callback($this->app); |
|
94
|
|
|
|
|
95
|
44 |
|
if (is_array($v) || is_object($v)) { |
|
96
|
44 |
|
$v = $this->converter->toObject($v); |
|
97
|
|
|
} |
|
98
|
44 |
|
$current->$key = $v; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
44 |
|
$current = $current->$key; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
44 |
|
return $current; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|