|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Config; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Config. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Config implements ConfigInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $pathMap = []; |
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
private $rawConfig; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Config constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $config |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $config) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->rawConfig = $config; |
|
34
|
|
|
$this->processConfig('', $config); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Processes the config into a one dimensional array. |
|
39
|
|
|
* |
|
40
|
|
|
* @param $path |
|
41
|
|
|
* @param $config |
|
42
|
|
|
*/ |
|
43
|
|
|
private function processConfig($path, $config) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!is_array($config)) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
foreach ($config as $key => $value) { |
|
50
|
|
|
$this->pathMap[$path.$key] = $value; |
|
51
|
|
|
|
|
52
|
|
|
if (is_array($value) && !empty($value)) { |
|
53
|
|
|
$this->processConfig($path.$key.'/', $value); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Checks whether the key-path does exist or not. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function has($path) |
|
66
|
|
|
{ |
|
67
|
|
|
return isset($this->pathMap[$path]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieves configuration data for a specific key. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* |
|
75
|
|
|
* @throws InvalidArgumentException |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getData($path) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$this->has($path)) { |
|
82
|
|
|
throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->pathMap[$path]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the configuration instance for a specific key. Also add the possibility to merge additional information |
|
90
|
|
|
* into it. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $path |
|
93
|
|
|
* |
|
94
|
|
|
* @throws InvalidArgumentException |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getConfig($path) |
|
99
|
|
|
{ |
|
100
|
|
|
if (!$this->has($path)) { |
|
101
|
|
|
throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return new self($this->getData($path)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the stored raw config array. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function toArray() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->rawConfig; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|