|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2010-2016 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are |
|
9
|
|
|
* permitted provided that the following conditions are met: |
|
10
|
|
|
* |
|
11
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of |
|
12
|
|
|
* conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
15
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials |
|
16
|
|
|
* provided with the distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21
|
|
|
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
|
22
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
27
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
namespace Cervo\Libraries; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Configuration manager for Cervo. |
|
37
|
|
|
* |
|
38
|
|
|
* @author Marc André Audet <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
class Config |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* The currently set default values in a multi-dimensional array. |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $defaultValues = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The currently set values in a multi-dimensional array. |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $values = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add a new array element to the specified configuration path. |
|
56
|
|
|
* Warning: If the current value is not an array, it is overwritten. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string|array $name The configuration path |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function add($name, $value) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!is_array($name)) { |
|
66
|
|
|
$name = explode('/', trim($name, "/\t\n\r\0\x0B")); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$current = &$this->values; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($name as $key) { |
|
72
|
|
|
$current = &$current[$key]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!is_array($current)) { |
|
76
|
|
|
$current = []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$current[] = $value; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set the value at the specified configuration path. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string|array $name The configuration path |
|
88
|
|
|
* @param mixed $value |
|
89
|
|
|
* |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
public function set($name, $value) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->_set($name, $value, false); |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set the default fallback value for the specified configuration path. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string|array $name The configuration path |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setDefault($name, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->_set($name, $value, true); |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return the value for the specified configuration path. |
|
114
|
|
|
* If this value is not set, return the default value. |
|
115
|
|
|
* Return null if not set. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name The configuration path |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public function get($name) |
|
122
|
|
|
{ |
|
123
|
|
|
$ex_name = explode('/', $name); |
|
124
|
|
|
|
|
125
|
|
|
$current = &$this->values; |
|
126
|
|
|
|
|
127
|
|
|
foreach ($ex_name as $key) { |
|
128
|
|
|
if ($current[$key]) { |
|
129
|
|
|
$current = &$current[$key]; |
|
130
|
|
|
} else { |
|
131
|
|
|
return $this->getDefault($name); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $current; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Return the default value for the specified configuration path. |
|
140
|
|
|
* Return null if not set. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $name The configuration path |
|
143
|
|
|
* |
|
144
|
|
|
* @return mixed |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getDefault($name) |
|
147
|
|
|
{ |
|
148
|
|
|
$current = &$this->defaultValues; |
|
149
|
|
|
|
|
150
|
|
|
foreach (explode('/', $name) as $key) { |
|
151
|
|
|
if ($current[$key]) { |
|
152
|
|
|
$current = &$current[$key]; |
|
153
|
|
|
} else { |
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ($current) { |
|
159
|
|
|
return $current; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Import a JSON file and parse it. |
|
167
|
|
|
* Return true on success, false if the file does not exists. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $file The path to the file. |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
|
|
public function importJSON($file) |
|
174
|
|
|
{ |
|
175
|
|
|
if ($file === null || !file_exists($file)) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$this->setFromArrayRecursive(json_decode(file_get_contents($file), true)); |
|
180
|
|
|
|
|
181
|
|
|
return true; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Recursively set all the values from an array. |
|
186
|
|
|
* Usually used when importing. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $array |
|
189
|
|
|
* @param array $current_path |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function setFromArrayRecursive($array, $current_path = []) |
|
192
|
|
|
{ |
|
193
|
|
|
foreach ($array as $key => $el) { |
|
194
|
|
|
if (is_array($el)) { |
|
195
|
|
|
$this->setFromArrayRecursive($el, array_merge($current_path, [$key])); |
|
196
|
|
|
} else { |
|
197
|
|
|
$this->set(array_merge($current_path, [$key]), $el); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
protected function _set($name, $value, $is_default = false) |
|
203
|
|
|
{ |
|
204
|
|
|
if (!is_array($name)) { |
|
205
|
|
|
$name = explode('/', $name); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if ($is_default) { |
|
209
|
|
|
$current = &$this->defaultValues; |
|
210
|
|
|
} else { |
|
211
|
|
|
$current = &$this->values; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
foreach ($name as $key) { |
|
215
|
|
|
$current = &$current[$key]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$current = $value; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|