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\Config\Loader\DummyLoader; |
20
|
|
|
use Phossa2\Shared\Tree\TreeInterface; |
21
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
22
|
|
|
use Phossa2\Config\Traits\WritableTrait; |
23
|
|
|
use Phossa2\Config\Traits\ArrayAccessTrait; |
24
|
|
|
use Phossa2\Shared\Reference\ReferenceTrait; |
25
|
|
|
use Phossa2\Config\Exception\LogicException; |
26
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
27
|
|
|
use Phossa2\Config\Loader\ConfigLoaderInterface; |
28
|
|
|
use Phossa2\Shared\Reference\ReferenceInterface; |
29
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
30
|
|
|
use Phossa2\Shared\Reference\DelegatorInterface; |
31
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareTrait; |
32
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Config |
36
|
|
|
* |
37
|
|
|
* @package Phossa2\Config |
38
|
|
|
* @author Hong Zhang <[email protected]> |
39
|
|
|
* @see ObjectAbstract |
40
|
|
|
* @see \ArrayAccess |
41
|
|
|
* @see ConfigInterface |
42
|
|
|
* @see ReferenceInterface |
43
|
|
|
* @see DelegatorAwareInterface |
44
|
|
|
* @version 2.0.0 |
45
|
|
|
* @since 2.0.0 added |
46
|
|
|
*/ |
47
|
|
|
class Config extends ObjectAbstract implements \ArrayAccess, ConfigInterface, WritableInterface, ReferenceInterface, DelegatorAwareInterface |
48
|
|
|
{ |
49
|
|
|
use ReferenceTrait, DelegatorAwareTrait, ArrayAccessTrait, WritableTrait; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* error type |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
const ERROR_IGNORE = 0; |
57
|
|
|
const ERROR_WARNING = 1; |
58
|
|
|
const ERROR_EXCEPTION = 2; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* the config loader |
62
|
|
|
* |
63
|
|
|
* @var ConfigLoaderInterface |
64
|
|
|
* @access protected |
65
|
|
|
*/ |
66
|
|
|
protected $loader; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* the config tree |
70
|
|
|
* |
71
|
|
|
* @var TreeInterface |
72
|
|
|
* @access protected |
73
|
|
|
*/ |
74
|
|
|
protected $config; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* cache loaded group names |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
* @access protected |
81
|
|
|
*/ |
82
|
|
|
protected $loaded = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* How to dealing with error, ignore/trigger_error/exception etc. |
86
|
|
|
* |
87
|
|
|
* @var int |
88
|
|
|
* @access protected |
89
|
|
|
*/ |
90
|
|
|
protected $error_type = self::ERROR_WARNING; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Constructor |
94
|
|
|
* |
95
|
|
|
* @param ConfigLoaderInterface $loader |
96
|
|
|
* @param TreeInterface $configTree |
97
|
|
|
* @param array $configData using this data if provided |
98
|
|
|
* @access public |
99
|
|
|
* @api |
100
|
|
|
*/ |
101
|
|
|
public function __construct( |
102
|
|
|
ConfigLoaderInterface $loader = null, |
103
|
|
|
TreeInterface $configTree = null, |
104
|
|
|
array $configData = [] |
105
|
|
|
) { |
106
|
|
|
$this->loader = $loader ?: new DummyLoader(); |
107
|
|
|
$this->config = $configTree ?: new Tree($configData); |
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((string) $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->throwError($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->setErrorType(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->setErrorType($err); |
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
|
155
|
|
|
} catch (\Exception $e) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
|
|
*/ |
163
|
|
|
public function set(/*# string */ $key, $value) |
164
|
|
|
{ |
165
|
|
|
if ($this->isWritable()) { |
166
|
|
|
// lazy load, no dereference |
167
|
|
|
$this->loadConfig((string) $key); |
168
|
|
|
|
169
|
|
|
// replace the node |
170
|
|
|
$this->config->addNode($key, $value); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
$this->throwError( |
175
|
|
|
Message::get(Message::CONFIG_NOT_WRITABLE), |
176
|
|
|
Message::CONFIG_NOT_WRITABLE |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set error type |
182
|
|
|
* |
183
|
|
|
* @param int $type |
184
|
|
|
* @return $this |
185
|
|
|
* @access public |
186
|
|
|
* @api |
187
|
|
|
*/ |
188
|
|
|
public function setErrorType(/*# int */ $type) |
189
|
|
|
{ |
190
|
|
|
$this->error_type = $type; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Overwrite setDelegator() in DelegatorAwareTrait |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
|
|
public function setDelegator(DelegatorInterface $delegator) |
199
|
|
|
{ |
200
|
|
|
$this->delegator = $delegator->addRegistry($this); |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Load config |
206
|
|
|
* |
207
|
|
|
* @param string $key |
208
|
|
|
* @return $this |
209
|
|
|
* @throws LogicException if current $error_type is to throw exception |
210
|
|
|
* @access protected |
211
|
|
|
*/ |
212
|
|
|
protected function loadConfig(/*# string */ $key) |
213
|
|
|
{ |
214
|
|
|
// get group name |
215
|
|
|
$group = $this->getGroupName($key); |
216
|
|
|
|
217
|
|
|
// $group loaded ? |
218
|
|
|
if (isset($this->loaded[$group])) { |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// mark as loaded |
223
|
|
|
$this->loaded[$group] = true; |
224
|
|
|
|
225
|
|
|
// loading the group |
226
|
|
|
return $this->loadByGroup($group); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Load one group config, force loading all groups if $group == '' |
231
|
|
|
* |
232
|
|
|
* @param string $group |
233
|
|
|
* @return $this |
234
|
|
|
* @throws \Exception group loading issues |
235
|
|
|
* @access protected |
236
|
|
|
*/ |
237
|
|
|
protected function loadByGroup(/*# string */ $group) |
238
|
|
|
{ |
239
|
|
|
// if super global |
240
|
|
|
if (substr($group, 0, 1) === '_') { |
241
|
|
|
return $this->loadGlobal($group); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// load from config file |
245
|
|
|
$conf = $this->loader->load($group); |
246
|
|
|
|
247
|
|
|
foreach ($conf as $grp => $data) { |
248
|
|
|
$this->config->addNode($grp, $data); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Load super globals |
256
|
|
|
* |
257
|
|
|
* @param string $group |
258
|
|
|
* @return $this |
259
|
|
|
* @throws LogicException if global unknown |
260
|
|
|
* @access protected |
261
|
|
|
*/ |
262
|
|
|
protected function loadGlobal(/*# string */ $group) |
263
|
|
|
{ |
264
|
|
|
if (!isset($GLOBALS[$group])) { |
265
|
|
|
throw new LogicException( |
266
|
|
|
Message::get(Message::CONFIG_GLOBAL_UNKNOWN, $group), |
267
|
|
|
Message::CONFIG_GLOBAL_UNKNOWN |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// load super global |
272
|
|
|
$this->config->addNode($group, $GLOBALS[$group]); |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get group name |
279
|
|
|
* |
280
|
|
|
* @param string $key |
281
|
|
|
* @return string |
282
|
|
|
* @access protected |
283
|
|
|
*/ |
284
|
|
|
protected function getGroupName(/*# string */ $key)/*# : string */ |
285
|
|
|
{ |
286
|
|
|
// first field of the $key |
287
|
|
|
return explode($this->config->getDelimiter(), $key)[0]; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* throw exception if current $error_type is to throw exception |
292
|
|
|
* |
293
|
|
|
* {@inheritDoc} |
294
|
|
|
*/ |
295
|
|
|
protected function resolveUnknown(/*# string */ $name) |
296
|
|
|
{ |
297
|
|
|
// warn if reference unknown |
298
|
|
|
$this->throwError( |
299
|
|
|
Message::get(Message::CONFIG_REFERENCE_UNKNOWN, $name), |
300
|
|
|
Message::CONFIG_REFERENCE_UNKNOWN |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
return null; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* {@inheritDoc} |
308
|
|
|
*/ |
309
|
|
|
protected function getReference(/*# string */ $name) |
310
|
|
|
{ |
311
|
|
|
return $this->get($name); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Dealing errors |
316
|
|
|
* |
317
|
|
|
* @param string $message |
318
|
|
|
* @param int $code |
319
|
|
|
* @return $this |
320
|
|
|
* @throws LogicException if current $error_type is to throw exception |
321
|
|
|
* @access protected |
322
|
|
|
*/ |
323
|
|
|
protected function throwError(/*# string */ $message, /*# int */ $code) |
324
|
|
|
{ |
325
|
|
|
switch ($this->error_type) { |
326
|
|
|
case self::ERROR_WARNING: |
327
|
|
|
trigger_error($message, \E_USER_WARNING); |
328
|
|
|
break; |
329
|
|
|
case self::ERROR_EXCEPTION: |
330
|
|
|
throw new LogicException($message, $code); |
331
|
|
|
default: |
332
|
|
|
break; |
333
|
|
|
} |
334
|
|
|
return $this; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|