1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Config; |
4
|
|
|
|
5
|
|
|
class Config implements ConfigInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Array with all parameters |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
private $_config = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Config constructor. |
15
|
|
|
* @param null $autoload - File with array or array for auto loading |
|
|
|
|
16
|
|
|
*/ |
17
|
|
|
public function __construct($autoload = null) |
18
|
|
|
{ |
19
|
|
|
if (!empty($autoload)) { |
20
|
|
|
// Read file from filesystem |
21
|
|
|
if (is_string($autoload)) { |
22
|
|
|
$this->load("$autoload"); |
23
|
|
|
} |
24
|
|
|
// Parse parameters if array |
25
|
|
|
if (is_array($autoload)) { |
26
|
|
|
$this->setter($autoload); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Load configuration file, show config path if needed |
33
|
|
|
* |
34
|
|
|
* @param string $path - Path to file with array |
35
|
|
|
* @return ConfigInterface |
36
|
|
|
*/ |
37
|
|
|
public function load(string $path): ConfigInterface |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
if (!file_exists($path)) { |
41
|
|
|
throw new ConfigException("Configuration file \"$path\" is not found"); |
42
|
|
|
} |
43
|
|
|
if (!is_readable($path)) { |
44
|
|
|
throw new ConfigException("Configuration file \"$path\" is not readable"); |
45
|
|
|
} |
46
|
|
|
$parameters = include "$path"; |
47
|
|
|
|
48
|
|
|
if (!is_array($parameters)) { |
49
|
|
|
throw new ConfigException("Passed parameters is not array"); |
50
|
|
|
} |
51
|
|
|
$this->setter($parameters); |
52
|
|
|
|
53
|
|
|
} catch (ConfigException $e) { |
54
|
|
|
// Error message implemented in exception |
55
|
|
|
} |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Put keys from array of parameters into internal array |
61
|
|
|
* |
62
|
|
|
* @param array $parameters |
63
|
|
|
*/ |
64
|
|
|
private function setter(array $parameters) |
65
|
|
|
{ |
66
|
|
|
// Parse array and set values |
67
|
|
|
array_map( |
68
|
|
|
function ($key, $value) { |
69
|
|
|
$this->set($key, $value); |
70
|
|
|
}, |
71
|
|
|
array_keys($parameters), |
72
|
|
|
$parameters |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set some parameter of configuration |
78
|
|
|
* |
79
|
|
|
* @param string $key |
80
|
|
|
* @param mixed $value |
81
|
|
|
* @return ConfigInterface |
82
|
|
|
*/ |
83
|
|
|
public function set(string $key, $value): ConfigInterface |
84
|
|
|
{ |
85
|
|
|
$this->_config[$key] = $value; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get single parameter by name, or all available parameters |
91
|
|
|
* |
92
|
|
|
* @param string|null $key |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function get(string $key = null) |
96
|
|
|
{ |
97
|
|
|
return empty($key) |
98
|
|
|
? $this->_config |
99
|
|
|
: $this->_config[$key]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Remove single value or clean config |
104
|
|
|
* |
105
|
|
|
* @param string|null $key |
106
|
|
|
* @return ConfigInterface |
107
|
|
|
*/ |
108
|
|
|
public function clean(string $key = null): ConfigInterface |
109
|
|
|
{ |
110
|
|
|
$this->_config = []; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |