1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Config; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
final class Config |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public const YAML = Parse\YAML::class; |
15
|
|
|
public const INI = Parse\INI::class; |
16
|
|
|
public const JSON = Parse\Json::class; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
private array $config = []; |
20
|
|
|
|
21
|
|
|
private string $separator = '->'; |
22
|
|
|
|
23
|
|
|
private LoggerInterface $logger; |
24
|
|
|
|
25
|
|
|
public function __construct(?LoggerInterface $logger = null) |
26
|
|
|
{ |
27
|
|
|
$this->logger = $logger ?? new NullLogger(); |
28
|
|
|
} |
29
|
8 |
|
|
30
|
|
|
/** |
31
|
8 |
|
* |
32
|
8 |
|
* @param array|string $params |
33
|
|
|
* @param array $options |
34
|
|
|
* @param class-string<ParseInterface> $parseClass |
|
|
|
|
35
|
|
|
* @param bool $replace |
36
|
|
|
* @return void |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function addConfig($params, array $options = [], string $parseClass = self::INI, bool $replace = true): void |
40
|
|
|
{ |
41
|
|
|
$params = (array)$params; |
42
|
|
|
|
43
|
8 |
|
if (!class_exists($parseClass)) { |
44
|
|
|
throw new \Exception(sprintf('Not found parse class: %s', $parseClass)); |
45
|
8 |
|
} |
46
|
|
|
$parser = new $parseClass(); |
47
|
8 |
|
$parser->setOptions($options); |
48
|
1 |
|
$parser->setLogger($this->logger); |
49
|
|
|
|
50
|
|
|
/** @var string|string[] $config */ |
51
|
7 |
|
foreach ($params as $namespace => $config) { |
52
|
7 |
|
if (is_int($namespace)) { |
53
|
7 |
|
$namespace = null; |
54
|
|
|
} |
55
|
7 |
|
|
56
|
7 |
|
if (is_string($namespace)) { |
57
|
1 |
|
$namespace = \trim($namespace); |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
if (is_array($config)) { |
61
|
6 |
|
foreach ($config as $_config) { |
62
|
|
|
$this->parse($parser, $_config, $namespace, $replace); |
63
|
|
|
} |
64
|
7 |
|
continue; |
65
|
2 |
|
} |
66
|
2 |
|
$this->parse($parser, $config, $namespace, $replace); |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
|
70
|
5 |
|
private function parse( |
71
|
|
|
ParseInterface $parser, |
72
|
7 |
|
string $config, |
73
|
|
|
?string $namespace = null, |
74
|
7 |
|
bool $replace = true |
75
|
|
|
): void { |
76
|
|
|
$parser->addConfigSource($config); |
77
|
|
|
|
78
|
|
|
$result = $parser->parse(); |
79
|
|
|
|
80
|
7 |
|
if (is_array($result)) { |
81
|
|
|
if ($namespace === null) { |
82
|
7 |
|
if ($replace === true) { |
83
|
|
|
$this->config = \array_merge_recursive_distinct($this->config, $result); |
84
|
7 |
|
} else { |
85
|
7 |
|
$this->config = \array_merge_recursive_distinct($result, $this->config); |
86
|
1 |
|
} |
87
|
1 |
|
} else { |
88
|
|
|
if (!array_key_exists($namespace, $this->config)) { |
89
|
1 |
|
$this->config[$namespace] = []; |
90
|
|
|
} |
91
|
|
|
if ($replace === true) { |
92
|
6 |
|
$this->config[$namespace] = \array_merge_recursive_distinct( |
93
|
6 |
|
(array)$this->config[$namespace], |
94
|
|
|
$result |
95
|
6 |
|
); |
96
|
6 |
|
} else { |
97
|
|
|
$this->config[$namespace] = \array_merge_recursive_distinct( |
98
|
2 |
|
$result, |
99
|
|
|
(array)$this->config[$namespace] |
100
|
|
|
); |
101
|
|
|
} |
102
|
7 |
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @param string|null $key |
110
|
|
|
* @param mixed $default |
111
|
7 |
|
* @return mixed |
112
|
|
|
*/ |
113
|
7 |
|
public function getConfig(string $key = null, $default = null) |
114
|
7 |
|
{ |
115
|
|
|
if ($key === null) { |
116
|
|
|
return $this->config; |
117
|
1 |
|
} |
118
|
1 |
|
|
119
|
|
|
$parts = explode($this->separator, $key); |
120
|
|
|
|
121
|
1 |
|
try { |
122
|
|
|
return $this->getValue($parts, $this->config); |
123
|
|
|
} catch (Exception $e) { |
124
|
1 |
|
return $default; |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
1 |
|
|
128
|
|
|
/** |
129
|
|
|
* @param string|null $key |
130
|
|
|
* @param mixed $default |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function get(string $key = null, $default = null) |
134
|
|
|
{ |
135
|
|
|
return $this->getConfig($key, $default); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string[] $parts |
140
|
|
|
* @param mixed $array |
141
|
|
|
* @return mixed |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
|
|
private function getValue(array $parts, $array) |
145
|
|
|
{ |
146
|
|
|
if (!is_array($array)){ |
147
|
|
|
throw new Exception(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$key = array_shift($parts); |
151
|
|
|
|
152
|
|
|
if (!array_key_exists($key, $array)) { |
153
|
|
|
throw new Exception(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (count($parts) > 0) { |
157
|
|
|
return $this->getValue($parts, $array[$key]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $array[$key]; |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setLogger(LoggerInterface $logger): void |
166
|
|
|
{ |
167
|
|
|
$this->logger = $logger; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $separator |
172
|
|
|
*/ |
173
|
|
|
public function setSeparator(string $separator): void |
174
|
|
|
{ |
175
|
|
|
$this->separator = $separator; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|