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 = \array_merge_recursive_distinct($this->config, $result); |
88
|
|
|
} else { |
89
|
1 |
|
$this->config = \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] = \array_merge_recursive_distinct((array)$this->config[$namespace], $result); |
97
|
|
|
} else { |
98
|
2 |
|
$this->config[$namespace] = \array_merge_recursive_distinct($result, (array)$this->config[$namespace]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
7 |
|
} |
103
|
|
|
|
104
|
7 |
|
|
105
|
|
|
/** |
106
|
7 |
|
* |
107
|
7 |
|
* @param string $key |
108
|
7 |
|
* @param mixed $default |
109
|
|
|
* @return mixed |
110
|
4 |
|
*/ |
111
|
1 |
|
public function getConfig(string $key = null, $default = null) |
112
|
1 |
|
{ |
113
|
|
|
if ($key === null) { |
114
|
1 |
|
return $this->config; |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
if (array_key_exists($key, $this->config)) { |
118
|
|
|
return $this->config[$key]; |
119
|
|
|
} |
120
|
4 |
|
|
121
|
4 |
|
return $default; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setLogger(LoggerInterface $logger): void |
125
|
|
|
{ |
126
|
|
|
$this->logger = $logger; |
127
|
7 |
|
} |
128
|
|
|
} |
129
|
|
|
|