1 | <?php |
||
50 | class Config extends ObjectAbstract implements ConfigInterface, WritableInterface, \ArrayAccess, ReferenceInterface, DelegatorAwareInterface |
||
51 | { |
||
52 | use ReferenceTrait, DelegatorAwareTrait, ArrayAccessTrait, WritableTrait; |
||
53 | |||
54 | /** |
||
55 | * error type |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | const ERROR_IGNORE = 0; |
||
60 | const ERROR_WARNING = 1; |
||
61 | const ERROR_EXCEPTION = 2; |
||
62 | |||
63 | /** |
||
64 | * the config loader |
||
65 | * |
||
66 | * @var ConfigLoaderInterface |
||
67 | * @access protected |
||
68 | */ |
||
69 | protected $loader; |
||
70 | |||
71 | /** |
||
72 | * the config tree |
||
73 | * |
||
74 | * @var TreeInterface |
||
75 | * @access protected |
||
76 | */ |
||
77 | protected $config; |
||
78 | |||
79 | /** |
||
80 | * cache loaded group names |
||
81 | * |
||
82 | * @var array |
||
83 | * @access protected |
||
84 | */ |
||
85 | protected $loaded = []; |
||
86 | |||
87 | /** |
||
88 | * How to dealing with error, ignore/trigger_error/exception etc. |
||
89 | * |
||
90 | * @var int |
||
91 | * @access protected |
||
92 | */ |
||
93 | protected $error_type = self::ERROR_WARNING; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | * @access private |
||
98 | */ |
||
99 | private $cached_id; |
||
100 | |||
101 | /** |
||
102 | * @var mixed |
||
103 | * @access private |
||
104 | */ |
||
105 | private $cached_value; |
||
106 | |||
107 | /** |
||
108 | * Constructor |
||
109 | * |
||
110 | * @param ConfigLoaderInterface $loader config loader if any |
||
111 | * @param TreeInterface $configTree config tree if any |
||
112 | * @param array $configData config data for the tree |
||
113 | * @access public |
||
114 | * @api |
||
115 | */ |
||
116 | public function __construct( |
||
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | public function get(/*# string */ $id, $default = null) |
||
141 | |||
142 | /** |
||
143 | * {@inheritDoc} |
||
144 | */ |
||
145 | public function has(/*# string */ $id)/*# : bool */ |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function set(/*# string */ $id, $value)/*# : bool */ |
||
188 | |||
189 | /** |
||
190 | * Set error type |
||
191 | * |
||
192 | * @param int $type |
||
193 | * @return $this |
||
194 | * @access public |
||
195 | * @api |
||
196 | */ |
||
197 | public function setErrorType(/*# int */ $type) |
||
202 | |||
203 | /** |
||
204 | * Load config |
||
205 | * |
||
206 | * @param string $id |
||
207 | * @return $this |
||
208 | * @throws LogicException if current $error_type is to throw exception |
||
209 | * @access protected |
||
210 | */ |
||
211 | protected function loadConfig(/*# string */ $id) |
||
227 | |||
228 | /** |
||
229 | * Load one group config, force loading all groups if $group == '' |
||
230 | * |
||
231 | * @param string $group |
||
232 | * @return $this |
||
233 | * @throws \Exception group loading issues |
||
234 | * @access protected |
||
235 | */ |
||
236 | protected function loadByGroup(/*# string */ $group) |
||
252 | |||
253 | /** |
||
254 | * Load super globals |
||
255 | * |
||
256 | * @param string $group |
||
257 | * @return $this |
||
258 | * @throws LogicException if super global unknown |
||
259 | * @access protected |
||
260 | */ |
||
261 | protected function loadGlobal(/*# string */ $group) |
||
275 | |||
276 | /** |
||
277 | * Get group name |
||
278 | * |
||
279 | * - returns 'system' from $id 'system.dir.tmp' |
||
280 | * - '.system.tmpdir' is invalid |
||
281 | * |
||
282 | * @param string $id |
||
283 | * @return string |
||
284 | * @access protected |
||
285 | */ |
||
286 | protected function getGroupName(/*# string */ $id)/*# : string */ |
||
293 | |||
294 | /** |
||
295 | * Override 'referenceLookup()' in ReferenceTrait. |
||
296 | * |
||
297 | * Delegator support goes here |
||
298 | * |
||
299 | * @since 2.0.10 using recursive getDelegator |
||
300 | * {@inheritDoc} |
||
301 | */ |
||
302 | protected function referenceLookup(/*# string */ $name) |
||
313 | |||
314 | /** |
||
315 | * throw exception if current $error_type is to throw exception |
||
316 | * |
||
317 | * {@inheritDoc} |
||
318 | */ |
||
319 | protected function resolveUnknown(/*# string */ $name) |
||
329 | |||
330 | /** |
||
331 | * {@inheritDoc} |
||
332 | */ |
||
333 | protected function getReference(/*# string */ $name) |
||
337 | |||
338 | /** |
||
339 | * Dealing errors |
||
340 | * |
||
341 | * @param string $message |
||
342 | * @param int $code |
||
343 | * @return $this |
||
344 | * @throws LogicException if current $error_type is to throw exception |
||
345 | * @access protected |
||
346 | */ |
||
347 | protected function throwError(/*# string */ $message, /*# int */ $code) |
||
360 | } |
||
361 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: