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