1 | <?php |
||
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( |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | public function get(/*# string */ $key, $default = null) |
||
132 | |||
133 | /** |
||
134 | * {@inheritDoc} |
||
135 | */ |
||
136 | public function has(/*# string */ $key)/*# : bool */ |
||
158 | |||
159 | /** |
||
160 | * {@inheritDoc} |
||
161 | */ |
||
162 | public function set(/*# string */ $key, $value) |
||
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) |
||
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) |
||
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) |
||
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 */ |
||
278 | |||
279 | /** |
||
280 | * throw exception if current $error_type is to throw exception |
||
281 | * |
||
282 | * {@inheritDoc} |
||
283 | */ |
||
284 | protected function resolveUnknown(/*# string */ $name) |
||
294 | |||
295 | /** |
||
296 | * {@inheritDoc} |
||
297 | */ |
||
298 | protected function getReference(/*# string */ $name) |
||
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) |
||
325 | } |
||
326 |