1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Config; |
4
|
|
|
|
5
|
|
|
use Phalcon\DI; |
6
|
|
|
use Yarak\Exceptions\InvalidConfig; |
7
|
|
|
use Phalcon\Config as PhalconConfig; |
8
|
|
|
|
9
|
|
|
class Config |
10
|
|
|
{ |
11
|
|
|
use PathHelpers; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Instance of self. |
15
|
|
|
* |
16
|
|
|
* @var Config |
17
|
|
|
*/ |
18
|
|
|
private static $instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Phalcon config instance. |
22
|
|
|
* |
23
|
|
|
* @var PhalconConfig |
24
|
|
|
*/ |
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The original config array. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $original; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Default setting values. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
const DEFAULTS = [ |
40
|
|
|
'migratorType' => 'fileDate', |
41
|
|
|
'migrationRepository' => 'database', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Private constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $configArray |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
private function __construct() |
50
|
|
|
{ |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get config values by key from PhalconConfig. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function __get($key) |
61
|
|
|
{ |
62
|
|
|
if (isset($this->config[$key])) { |
63
|
|
|
return $this->config->$key; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->getDefault($key); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get instance of self with config array set. |
71
|
|
|
* |
72
|
|
|
* @param array $configArray |
|
|
|
|
73
|
|
|
* |
74
|
|
|
* @return Config |
75
|
|
|
*/ |
76
|
|
|
public static function getInstance() |
77
|
|
|
{ |
78
|
|
|
if (empty(self::$instance)) { |
79
|
|
|
self::$instance = new self(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return self::$instance; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set config using config data in di or passed data. |
87
|
|
|
* |
88
|
|
|
* @param array|string $userConfig If string, config path. If array, data. |
89
|
|
|
* @param bool $merge If true, merge given config array into current. |
90
|
|
|
*/ |
91
|
|
|
public function setConfig($userConfig = null, $merge = true) |
92
|
|
|
{ |
93
|
|
|
$this->config = \Phalcon\Di::getDefault()->get('config'); |
94
|
|
|
|
95
|
|
|
if (is_string($userConfig)) { |
96
|
|
|
$this->config = $this->getNested(explode('.', $userConfig)); |
97
|
|
|
} elseif (is_array($userConfig)) { |
98
|
|
|
$userConfig = new PhalconConfig($userConfig); |
99
|
|
|
|
100
|
|
|
if ($merge === true) { |
101
|
|
|
$this->merge($userConfig); |
102
|
|
|
} else { |
103
|
|
|
$this->config = $userConfig; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->original = $this->toArray(); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a value by key from config. |
114
|
|
|
* |
115
|
|
|
* @param string|array $key |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function get($key) |
120
|
|
|
{ |
121
|
|
|
return $this->$key; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get nested values. |
126
|
|
|
* |
127
|
|
|
* @param array $keyArray |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function getNested(array $keyArray) |
132
|
|
|
{ |
133
|
|
|
$current = $this->config; |
134
|
|
|
|
135
|
|
|
foreach ($keyArray as $key) { |
136
|
|
|
if ($current[$key] === null) { |
137
|
|
|
return $this->getDefault($key); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$current = $current->$key; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $current; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Return true if config array has given value. |
148
|
|
|
* |
149
|
|
|
* @param mixed $value |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function has($value) |
154
|
|
|
{ |
155
|
|
|
$current = $this->config; |
156
|
|
|
|
157
|
|
|
foreach ($this->makeArray($value) as $configItem) { |
158
|
|
|
if ($current[$configItem] === null) { |
159
|
|
|
$current = $this->getDefault($configItem); |
160
|
|
|
|
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$current = $current->$configItem; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $current !== null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get a setting's default value. |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
* |
175
|
|
|
* @return mixed|null |
176
|
|
|
*/ |
177
|
|
|
public function getDefault($key) |
178
|
|
|
{ |
179
|
|
|
if (array_key_exists($key, self::DEFAULTS)) { |
180
|
|
|
return self::DEFAULTS[$key]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set an item in the config. |
186
|
|
|
* |
187
|
|
|
* @param mixed $keys |
188
|
|
|
* @param mixed $value |
189
|
|
|
*/ |
190
|
|
|
public function set($keys, $value) |
191
|
|
|
{ |
192
|
|
|
$temp = &$this->config; |
193
|
|
|
|
194
|
|
|
$keys = $this->makeArray($keys); |
195
|
|
|
|
196
|
|
|
$count = count($keys); |
197
|
|
|
|
198
|
|
|
foreach ($keys as $index => $key) { |
199
|
|
|
if ($count === $index + 1) { |
200
|
|
|
return $temp[$key] = $value; |
201
|
|
|
} elseif ($temp[$key] === null) { |
202
|
|
|
$temp[$key] = new PhalconConfig(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$temp = &$temp[$key]; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Remove an item from the config. |
211
|
|
|
* |
212
|
|
|
* @param mixed $keys |
213
|
|
|
*/ |
214
|
|
|
public function remove($keys) |
215
|
|
|
{ |
216
|
|
|
$temp = &$this->config; |
217
|
|
|
|
218
|
|
|
$keys = $this->makeArray($keys); |
219
|
|
|
|
220
|
|
|
$count = count($keys); |
221
|
|
|
|
222
|
|
|
foreach ($keys as $key) { |
223
|
|
|
if ($key === $keys[$count - 1]) { |
224
|
|
|
unset($temp[$key]); |
225
|
|
|
} else { |
226
|
|
|
$temp = &$temp[$key]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the config array to its original values. |
233
|
|
|
*/ |
234
|
|
|
public function refresh() |
235
|
|
|
{ |
236
|
|
|
$this->config = $this->config->__set_state($this->original); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Return the config array. |
241
|
|
|
* |
242
|
|
|
* @return array |
243
|
|
|
*/ |
244
|
|
|
public function toArray() |
245
|
|
|
{ |
246
|
|
|
if (is_string($this->config)) { |
247
|
|
|
return $this->config; |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->config->toArray(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Make a variable an array if not one already. |
255
|
|
|
* |
256
|
|
|
* @param mixed $value |
257
|
|
|
* |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
protected function makeArray($value) |
261
|
|
|
{ |
262
|
|
|
if (!is_array($value)) { |
263
|
|
|
return [$value]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $value; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Validate that a setting exists. |
271
|
|
|
* |
272
|
|
|
* @param array $settings |
273
|
|
|
* |
274
|
|
|
* @throws InvalidConfig |
275
|
|
|
*/ |
276
|
|
|
public function validate(array $settings) |
277
|
|
|
{ |
278
|
|
|
if (!$this->has($settings)) { |
279
|
|
|
throw InvalidConfig::configValueNotFound(implode(' -> ', $settings)); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Merge the given config with the current one. |
285
|
|
|
* |
286
|
|
|
* @param PhalconConfig $config |
287
|
|
|
*/ |
288
|
|
|
public function merge(PhalconConfig $config) |
289
|
|
|
{ |
290
|
|
|
$this->config = $this->config->merge($config); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Return the config key count. |
295
|
|
|
* |
296
|
|
|
* @return int |
297
|
|
|
*/ |
298
|
|
|
public function count() |
299
|
|
|
{ |
300
|
|
|
return $this->config->count(); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.