1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\Component\Configuration; |
12
|
|
|
|
13
|
|
|
class Configuration |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Delimiter for configuration keys |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const DELIMITER = '.'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The configuration storage |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bootstraps the configuration |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
*/ |
34
|
16 |
|
public function __construct(array $config = []) |
35
|
|
|
{ |
36
|
16 |
|
$this->setConfiguration($config); |
37
|
16 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the configuration |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
11 |
|
public function getConfiguration() |
45
|
|
|
{ |
46
|
11 |
|
return $this->config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets the configuration |
51
|
|
|
* |
52
|
|
|
* @param array $config |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
16 |
|
public function setConfiguration(array $config = []) |
56
|
|
|
{ |
57
|
16 |
|
$this->config = $config; |
58
|
16 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets a single configuration value |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* @param string $value |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
5 |
|
public function setConfigurationValue($key, $value) |
68
|
|
|
{ |
69
|
5 |
|
$completeConfiguration = $this->getConfiguration(); |
70
|
5 |
|
$configuration = &$completeConfiguration; |
71
|
|
|
|
72
|
5 |
|
$pathParts = explode(self::DELIMITER, $key); |
73
|
5 |
|
foreach ($pathParts as $pathPart) { |
74
|
|
|
|
75
|
|
|
// If the current configuration is not an array, |
76
|
|
|
// but the client want to set a value, just overwrite the current value |
77
|
5 |
|
if (is_array($configuration) === false) { |
78
|
5 |
|
$configuration = []; |
79
|
5 |
|
} |
80
|
5 |
|
$configuration = &$configuration[ucfirst($pathPart)]; |
81
|
5 |
|
} |
82
|
|
|
|
83
|
5 |
|
$configuration = $value; |
84
|
5 |
|
$this->setConfiguration($completeConfiguration); |
85
|
5 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a single configuration value |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
8 |
|
public function getConfigurationValue($key) |
94
|
|
|
{ |
95
|
8 |
|
$configuration = $this->getConfiguration(); |
96
|
|
|
|
97
|
8 |
|
$pathParts = explode(self::DELIMITER, $key); |
98
|
8 |
|
foreach ($pathParts as $pathPart) { |
99
|
8 |
|
if (isset($configuration[ucfirst($pathPart)]) === true) { |
100
|
8 |
|
$configuration = $configuration[ucfirst($pathPart)]; |
101
|
8 |
|
} else { |
102
|
5 |
|
$configuration = null; |
103
|
|
|
} |
104
|
8 |
|
} |
105
|
|
|
|
106
|
8 |
|
return $configuration; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks if the configuration got the incoming key |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
1 |
|
public function hasConfigurationKey($key) |
116
|
|
|
{ |
117
|
1 |
|
$result = true; |
118
|
1 |
|
$configuration = $this->getConfiguration(); |
119
|
|
|
|
120
|
1 |
|
$pathParts = explode(self::DELIMITER, $key); |
121
|
1 |
|
foreach ($pathParts as $pathPart) { |
122
|
1 |
|
if (array_key_exists(ucfirst($pathPart), $configuration) === false) { |
123
|
1 |
|
$result = false; |
124
|
1 |
|
break; |
125
|
|
|
} else { |
126
|
1 |
|
$configuration = $configuration[ucfirst($pathPart)]; |
127
|
|
|
} |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
return $result; |
131
|
|
|
} |
132
|
|
|
} |