1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2020 Enjoys. |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
declare(strict_types=1); |
28
|
|
|
|
29
|
|
|
namespace Enjoys\Config; |
30
|
|
|
|
31
|
|
|
use Enjoys\Config\Parse; |
32
|
|
|
use Exception; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Description of Config |
36
|
|
|
|
37
|
|
|
* @author Enjoys |
38
|
|
|
*/ |
39
|
|
|
class Config implements \Psr\Log\LoggerAwareInterface |
40
|
|
|
{ |
41
|
|
|
use \Psr\Log\LoggerAwareTrait; |
42
|
|
|
|
43
|
|
|
public const YAML = Parse\YAML::class; |
44
|
|
|
public const INI = Parse\INI::class; |
45
|
|
|
public const JSON = Parse\Json::class; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @var \Psr\Log\LoggerInterface|null |
50
|
|
|
*/ |
51
|
|
|
protected $logger = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @var mixed |
56
|
|
|
*/ |
57
|
|
|
private $config = []; |
58
|
|
|
|
59
|
|
|
public function __construct(?\Psr\Log\LoggerInterface $logger = null) |
60
|
2 |
|
{ |
61
|
|
|
if ($logger !== null) { |
62
|
2 |
|
$this->setLogger($logger); |
63
|
1 |
|
} |
64
|
|
|
} |
65
|
2 |
|
|
66
|
|
|
public function addConfig($config, array $options = [], string $parseClass = self::INI): void |
67
|
2 |
|
{ |
68
|
|
|
$namespace = null; |
69
|
2 |
|
|
70
|
1 |
|
if (!class_exists($parseClass)) { |
71
|
|
|
throw new \Exception(sprintf('Not found parse class: %s', $parseClass)); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
if (is_array($config)) { |
75
|
1 |
|
$namespace = array_key_first($config); |
76
|
|
|
$config = $config[$namespace]; |
77
|
|
|
} |
78
|
1 |
|
|
79
|
1 |
|
|
80
|
|
|
/** @var ParseInterface $parser */ |
81
|
|
|
$parser = new $parseClass($config); |
82
|
1 |
|
$parser->setOptions($options); |
83
|
|
|
|
84
|
1 |
|
|
85
|
1 |
|
if ($this->logger) { |
86
|
|
|
$parser->setLogger($this->logger); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
$result = $parser->parse(); |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
if (is_array($result)) { |
93
|
|
|
if ($namespace === null) { |
94
|
|
|
$this->config = array_merge($this->config, $result); |
95
|
1 |
|
} else { |
96
|
|
|
if (!array_key_exists($namespace, $this->config)) { |
97
|
1 |
|
$this->config[$namespace] = []; |
98
|
1 |
|
} |
99
|
|
|
$this->config[$namespace] = array_merge($this->config[$namespace], $result); |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @param string $key |
107
|
|
|
* @param mixed $default |
108
|
|
|
* @return mixed |
109
|
1 |
|
*/ |
110
|
|
|
public function getConfig(string $key = null, $default = null) |
111
|
|
|
{ |
112
|
|
|
if ($key === null) { |
113
|
|
|
return $this->config; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (is_array($this->config) && array_key_exists($key, $this->config)) { |
117
|
|
|
return $this->config[$key]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// if (is_object($this->config) && property_exists($this->config, $key)) { |
121
|
|
|
// return $this->config->$key; |
122
|
|
|
// } |
123
|
|
|
|
124
|
|
|
return $default; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|