|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Config |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Config; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Shared\Tree\Tree; |
|
18
|
|
|
use Phossa2\Config\Message\Message; |
|
19
|
|
|
use Phossa2\Shared\Tree\TreeInterface; |
|
20
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
|
21
|
|
|
use Phossa2\Shared\Reference\ReferenceTrait; |
|
22
|
|
|
use Phossa2\Config\Exception\LogicException; |
|
23
|
|
|
use Phossa2\Config\Loader\ConfigLoaderInterface; |
|
24
|
|
|
use Phossa2\Shared\Reference\ReferenceInterface; |
|
25
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareTrait; |
|
26
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Config |
|
30
|
|
|
* |
|
31
|
|
|
* @package Phossa2\Config |
|
32
|
|
|
* @author Hong Zhang <[email protected]> |
|
33
|
|
|
* @see ObjectAbstract |
|
34
|
|
|
* @see ConfigInterface |
|
35
|
|
|
* @see ReferenceInterface |
|
36
|
|
|
* @version 2.0.0 |
|
37
|
|
|
* @since 2.0.0 added |
|
38
|
|
|
*/ |
|
39
|
|
|
class Config extends ObjectAbstract implements ConfigInterface, ReferenceInterface, DelegatorAwareInterface |
|
40
|
|
|
{ |
|
41
|
|
|
use ReferenceTrait, DelegatorAwareTrait; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* error type |
|
45
|
|
|
* |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
const ERROR_IGNORE = 0; |
|
49
|
|
|
const ERROR_WARNING = 1; |
|
50
|
|
|
const ERROR_EXCEPTION = 2; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* the config loader |
|
54
|
|
|
* |
|
55
|
|
|
* @var ConfigLoaderInterface |
|
56
|
|
|
* @access protected |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $loader; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* the config tree |
|
62
|
|
|
* |
|
63
|
|
|
* @var TreeInterface |
|
64
|
|
|
* @access protected |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $config; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* cache loaded group names |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
* @access protected |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $loaded = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* How to dealing with error, ignore/trigger_error/exception etc. |
|
78
|
|
|
* |
|
79
|
|
|
* @var int |
|
80
|
|
|
* @access protected |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $error_type; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Constructor |
|
86
|
|
|
* |
|
87
|
|
|
* @param ConfigLoaderInterface $loader |
|
88
|
|
|
* @param TreeInterface $configTree |
|
89
|
|
|
* @param int $errorType |
|
90
|
|
|
* @access public |
|
91
|
|
|
* @api |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __construct( |
|
94
|
|
|
ConfigLoaderInterface $loader, |
|
95
|
|
|
TreeInterface $configTree = null, |
|
96
|
|
|
/*# int */ $errorType = self::ERROR_WARNING |
|
97
|
|
|
) { |
|
98
|
|
|
// the config loader |
|
99
|
|
|
$this->loader = $loader; |
|
100
|
|
|
|
|
101
|
|
|
// the config tree |
|
102
|
|
|
if (null === $configTree) { |
|
103
|
|
|
$this->config = new Tree(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// set error type |
|
107
|
|
|
$this->error_type = $errorType; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function get(/*# string */ $key, $default = null) |
|
114
|
|
|
{ |
|
115
|
|
|
try { |
|
116
|
|
|
// lazy load |
|
117
|
|
|
$this->loadConfig((string) $key); |
|
118
|
|
|
|
|
119
|
|
|
// get value |
|
120
|
|
|
$val = $this->config->getNode($key); |
|
121
|
|
|
|
|
122
|
|
|
// dereference |
|
123
|
|
|
$this->deReferenceArray($val); |
|
124
|
|
|
|
|
125
|
|
|
return null === $val ? $default : $val; |
|
126
|
|
|
|
|
127
|
|
|
// if dereference exception catched |
|
128
|
|
|
} catch (\Exception $e) { |
|
129
|
|
|
$this->setError($e->getMessage(), $e->getCode()); |
|
130
|
|
|
return $default; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritDoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function has(/*# string */ $key)/*# : bool */ |
|
138
|
|
|
{ |
|
139
|
|
|
try { |
|
140
|
|
|
// update error type |
|
141
|
|
|
$err = $this->error_type; |
|
142
|
|
|
$this->error_type = self::ERROR_IGNORE; |
|
143
|
|
|
|
|
144
|
|
|
// lazy load |
|
145
|
|
|
$this->loadConfig((string) $key); |
|
146
|
|
|
|
|
147
|
|
|
// get value |
|
148
|
|
|
$result = null !== $this->config->getNode((string) $key); |
|
149
|
|
|
|
|
150
|
|
|
// restore error type |
|
151
|
|
|
$this->error_type = $err; |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
|
|
155
|
|
|
} catch (\Exception $e) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set configuration |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $key configuration key |
|
164
|
|
|
* @param mixed values |
|
165
|
|
|
* @return $this |
|
166
|
|
|
* @throws LogicException if error type is to throw exception |
|
167
|
|
|
* @access public |
|
168
|
|
|
* @api |
|
169
|
|
|
*/ |
|
170
|
|
|
public function set(/*# string */ $key, $value) |
|
171
|
|
|
{ |
|
172
|
|
|
// clear reference cache |
|
173
|
|
|
$this->clearReferenceCache(); |
|
174
|
|
|
|
|
175
|
|
|
// lazy load, no dereference |
|
176
|
|
|
$this->loadConfig((string) $key); |
|
177
|
|
|
|
|
178
|
|
|
// replace the node |
|
179
|
|
|
$this->config->addNode($key, $value); |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Load config |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $key |
|
188
|
|
|
* @return $this |
|
189
|
|
|
* @throws LogicException if current $error_type is to throw exception |
|
190
|
|
|
* @access protected |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function loadConfig(/*# string */ $key) |
|
193
|
|
|
{ |
|
194
|
|
|
// get group name |
|
195
|
|
|
$group = $this->getGroupName($key); |
|
196
|
|
|
|
|
197
|
|
|
// $group loaded ? |
|
198
|
|
|
if (isset($this->loaded[$group])) { |
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// mark as loaded |
|
203
|
|
|
$this->loaded[$group] = true; |
|
204
|
|
|
|
|
205
|
|
|
// loading the group |
|
206
|
|
|
return $this->loadByGroup($group); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Load one group config, force loading all groups if $group == '' |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $group |
|
213
|
|
|
* @return $this |
|
214
|
|
|
* @throws \Exception group loading issues |
|
215
|
|
|
* @access protected |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function loadByGroup(/*# string */ $group) |
|
218
|
|
|
{ |
|
219
|
|
|
// if super global |
|
220
|
|
|
if (substr($group, 0, 1) === '_') { |
|
221
|
|
|
return $this->loadGlobal($group); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// load from config file |
|
225
|
|
|
$conf = $this->loader->load($group); |
|
226
|
|
|
|
|
227
|
|
|
foreach ($conf as $grp => $data) { |
|
228
|
|
|
$this->config->addNode($grp, $data); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Load super globals |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $group |
|
238
|
|
|
* @return $this |
|
239
|
|
|
* @throws LogicException if global unknown |
|
240
|
|
|
* @access protected |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function loadGlobal(/*# string */ $group) |
|
243
|
|
|
{ |
|
244
|
|
|
if (!isset($GLOBALS[$group])) { |
|
245
|
|
|
throw new LogicException( |
|
246
|
|
|
Message::get(Message::CONFIG_GLOBAL_UNKNOWN, $group), |
|
247
|
|
|
Message::CONFIG_GLOBAL_UNKNOWN |
|
248
|
|
|
); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
// load super global |
|
252
|
|
|
$this->config->addNode($group, $GLOBALS[$group]); |
|
253
|
|
|
|
|
254
|
|
|
return $this; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get group name |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $key |
|
261
|
|
|
* @return string |
|
262
|
|
|
* @access protected |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function getGroupName(/*# string */ $key)/*# : string */ |
|
265
|
|
|
{ |
|
266
|
|
|
// first field of the $key |
|
267
|
|
|
return explode($this->config->getDelimiter(), $key)[0]; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* throw exception if current $error_type is to throw exception |
|
272
|
|
|
* |
|
273
|
|
|
* {@inheritDoc} |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function resolveUnknown(/*# string */ $name) |
|
276
|
|
|
{ |
|
277
|
|
|
// warn if reference unknown |
|
278
|
|
|
$this->setError( |
|
279
|
|
|
Message::get(Message::CONFIG_REFERENCE_UNKNOWN, $name), |
|
280
|
|
|
Message::CONFIG_REFERENCE_UNKNOWN |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
return null; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* {@inheritDoc} |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function getReference(/*# string */ $name) |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->get($name); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Dealing errors |
|
296
|
|
|
* |
|
297
|
|
|
* @param string $message |
|
298
|
|
|
* @param int $code |
|
299
|
|
|
* @return $this |
|
300
|
|
|
* @throws LogicException if current $error_type is to throw exception |
|
301
|
|
|
* @access protected |
|
302
|
|
|
*/ |
|
303
|
|
|
protected function setError(/*# string */ $message, /*# int */ $code) |
|
304
|
|
|
{ |
|
305
|
|
|
switch ($this->error_type) { |
|
306
|
|
|
case self::ERROR_WARNING: |
|
307
|
|
|
trigger_error($message, \E_USER_WARNING); |
|
308
|
|
|
break; |
|
309
|
|
|
case self::ERROR_EXCEPTION: |
|
310
|
|
|
throw new LogicException($message, $code); |
|
311
|
|
|
default: |
|
312
|
|
|
break; |
|
313
|
|
|
} |
|
314
|
|
|
return $this; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|