1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Config |
7
|
|
|
* @package DrMVC\Config |
8
|
|
|
*/ |
9
|
|
|
class Config implements ConfigInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Array with all parameters |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $_config = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Config constructor. |
19
|
|
|
* @param null|string|array $data File with array or array for auto loading |
20
|
|
|
*/ |
21
|
|
|
public function __construct($data = null) |
22
|
|
|
{ |
23
|
|
|
switch (true) { |
24
|
|
|
// If data is string, then need load file |
25
|
|
|
case (\is_string($data)): |
26
|
|
|
$this->load($data); |
27
|
|
|
break; |
28
|
|
|
// If data is array, the need to set |
29
|
|
|
case (\is_array($data)): |
30
|
|
|
$this->setter($data); |
31
|
|
|
break; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load parameters from filesystem |
37
|
|
|
* |
38
|
|
|
* @param string $path |
39
|
|
|
* @return mixed |
40
|
|
|
* @throws Exception |
41
|
|
|
*/ |
42
|
|
|
private function loadFile(string $path) |
43
|
|
|
{ |
44
|
|
|
if (!file_exists($path)) { |
45
|
|
|
throw new Exception('Configuration file "' . $path . '" is not found'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!is_readable($path)) { |
49
|
|
|
throw new Exception('Configuration file "' . $path . '" is not readable'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Include file |
53
|
|
|
$content = include $path; |
54
|
|
|
|
55
|
|
|
if (!\is_array($content)) { |
56
|
|
|
throw new Exception("Passed file \"$path\" is not array"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $content; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Load configuration file, show config path if needed |
64
|
|
|
* |
65
|
|
|
* @param string $path path to file with array |
66
|
|
|
* @param string $key in which subkey this file must be saved |
67
|
|
|
* @return ConfigInterface |
68
|
|
|
*/ |
69
|
|
|
public function load(string $path, string $key = null): ConfigInterface |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
// Read parameters from file |
73
|
|
|
$parameters = $this->loadFile($path); |
74
|
|
|
|
75
|
|
|
// If key is provided then need put parameters into subarray |
76
|
|
|
if (null !== $key) { |
77
|
|
|
$parameters = [$key => $parameters]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Keep configuration |
81
|
|
|
$this->setter($parameters); |
82
|
|
|
|
83
|
|
|
} catch (Exception $e) { |
84
|
|
|
// Error message implemented in exception |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Put keys from array of parameters into internal array |
91
|
|
|
* |
92
|
|
|
* @param array $parameters |
93
|
|
|
*/ |
94
|
|
|
private function setter(array $parameters) |
95
|
|
|
{ |
96
|
|
|
// Parse array and set values |
97
|
|
|
array_map( |
98
|
|
|
function($key, $value) { |
99
|
|
|
$this->set($key, $value); |
100
|
|
|
}, |
101
|
|
|
array_keys($parameters), |
102
|
|
|
$parameters |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set some parameter of configuration |
108
|
|
|
* |
109
|
|
|
* @param string $key |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return ConfigInterface |
112
|
|
|
*/ |
113
|
|
|
public function set(string $key, $value): ConfigInterface |
114
|
|
|
{ |
115
|
|
|
$this->_config[$key] = \is_array($value) |
116
|
|
|
? new Config($value) |
117
|
|
|
: $value; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get single parameter by name or null, or all available parameters |
124
|
|
|
* |
125
|
|
|
* @param string|null $key |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function get(string $key = null) |
129
|
|
|
{ |
130
|
|
|
$result = (null !== $key) |
131
|
|
|
? $this->_config[$key] ?? null |
132
|
|
|
: $this->_config; |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove single value or clean config |
139
|
|
|
* |
140
|
|
|
* @param string|null $key |
141
|
|
|
* @return ConfigInterface |
142
|
|
|
*/ |
143
|
|
|
public function clean(string $key = null): ConfigInterface |
144
|
|
|
{ |
145
|
|
|
if (null !== $key) { |
146
|
|
|
unset($this->_config[$key]); |
147
|
|
|
} else { |
148
|
|
|
$this->_config = []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|