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