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