1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\config; |
4
|
|
|
|
5
|
|
|
class Config implements \ArrayAccess |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $config; |
9
|
|
|
private $context = 'default'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Reads the configurations stored at a given path. |
13
|
|
|
* |
14
|
|
|
* Path could either point to a directory or a specific file. In the case of |
15
|
|
|
* a file, the contents of the file are read into the config object. However, |
16
|
|
|
* in the case of a directory, the contents of all the files in the directory |
17
|
|
|
* are read into the config file with configurations from each file prefixed with |
18
|
|
|
* the file name. |
19
|
|
|
* |
20
|
|
|
* @param string $path |
21
|
|
|
* @return Config |
22
|
|
|
*/ |
23
|
4 |
|
public function readPath($path) |
24
|
|
|
{ |
25
|
4 |
|
if (is_dir($path)) { |
26
|
3 |
|
$dir = new Directory($path); |
27
|
3 |
|
$this->config = ['default' => $this->expand($dir->parse())]; |
28
|
3 |
|
foreach ($dir->getContexts() as $context) { |
29
|
2 |
|
$this->config[$context] = array_merge( |
30
|
3 |
|
$this->config['default'], $this->expand($dir->parse($context)) |
31
|
|
|
); |
32
|
|
|
} |
33
|
1 |
|
} else if (is_file($path)) { |
34
|
1 |
|
$this->config[$this->context] = $this->expand(File::read($path)); |
35
|
|
|
} else { |
36
|
|
|
throw new \ntentan\utils\exceptions\FileNotFoundException($path); |
37
|
|
|
} |
38
|
4 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isKeySet($key) |
42
|
|
|
{ |
43
|
|
|
return isset($this->config[$this->context][$key]); |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
public function get($key, $default = null) |
47
|
|
|
{ |
48
|
6 |
|
return isset($this->config[$this->context][$key]) ? $this->config[$this->context][$key] : $default; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function setContext($context) |
52
|
|
|
{ |
53
|
1 |
|
$this->context = $context; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
3 |
|
public function set($key, $value) |
58
|
|
|
{ |
59
|
3 |
|
$keys = explode('.', $key); |
60
|
3 |
|
$this->config[$this->context] = $this->setValue($keys, $value, $this->config[$this->context]); |
61
|
3 |
|
$this->config[$this->context][$key] = $value; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
3 |
|
private function setValue($keys, $value, $config, $nested = [], &$merge = []) |
65
|
|
|
{ |
66
|
3 |
|
if (!empty($keys)) { |
67
|
3 |
|
$key = array_shift($keys); |
68
|
3 |
|
$nested[] = $key; |
69
|
3 |
|
$config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : [], $nested, $merge); |
70
|
3 |
|
if(count($nested) > 1) { |
71
|
3 |
|
$merge[implode('.', $nested)] = $config[$key]; |
72
|
|
|
} |
73
|
3 |
|
else if(count($nested) == 1) { |
74
|
3 |
|
$config = array_merge($config, $merge); |
75
|
|
|
} |
76
|
3 |
|
return $config; |
77
|
|
|
} else { |
78
|
3 |
|
return $value; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
private function expand($array, $prefix = null) |
83
|
|
|
{ |
84
|
4 |
|
$config = []; |
85
|
4 |
|
if (!is_array($array)) |
86
|
4 |
|
return $config; |
87
|
4 |
|
$dottedPrefix = $prefix ? "$prefix." : ""; |
88
|
4 |
|
foreach ($array as $key => $value) { |
89
|
4 |
|
$newPrefix = $dottedPrefix . $key; |
90
|
4 |
|
$config[$newPrefix] = $value; |
91
|
4 |
|
$config += $this->expand($value, $newPrefix); |
92
|
|
|
} |
93
|
4 |
|
if ($prefix) |
94
|
3 |
|
$config[$prefix] = $array; |
95
|
4 |
|
return $config; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function offsetExists($offset): bool |
99
|
|
|
{ |
100
|
|
|
return isset($this->config[$this->context][$offset]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function offsetGet($offset) |
104
|
|
|
{ |
105
|
|
|
return $this->config[$this->context][$offset]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function offsetSet($offset, $value): void |
109
|
|
|
{ |
110
|
|
|
$this->set($offset, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function offsetUnset($offset): void |
114
|
|
|
{ |
115
|
|
|
// Do nothing for now ... |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|