Config.php
12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Config.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Config implements Countable, Iterator
{
/**
* Whether in-memory modifications to configuration data are allowed
*
* @var boolean
*/
protected $_allowModifications;
/**
* Iteration index
*
* @var integer
*/
protected $_index;
/**
* Number of elements in configuration data
*
* @var integer
*/
protected $_count;
/**
* Contains array of configuration data
*
* @var array
*/
protected $_data;
/**
* Used when unsetting values during iteration to ensure we do not skip
* the next element
*
* @var boolean
*/
protected $_skipNextIteration;
/**
* Contains which config file sections were loaded. This is null
* if all sections were loaded, a string name if one section is loaded
* and an array of string names if multiple sections were loaded.
*
* @var mixed
*/
protected $_loadedSection;
/**
* This is used to track section inheritance. The keys are names of sections that
* extend other sections, and the values are the extended sections.
*
* @var array
*/
protected $_extends = array();
/**
* Load file error string.
*
* Is null if there was no error while file loading
*
* @var string
*/
protected $_loadFileErrorStr = null;
/**
* Zend_Config provides a property based interface to
* an array. The data are read-only unless $allowModifications
* is set to true on construction.
*
* Zend_Config also implements Countable and Iterator to
* facilitate easy access to the data.
*
* @param array $array
* @param boolean $allowModifications
* @return void
*/
public function __construct(array $array, $allowModifications = false)
{
$this->_allowModifications = (boolean) $allowModifications;
$this->_loadedSection = null;
$this->_index = 0;
$this->_data = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->_data[$key] = new self($value, $this->_allowModifications);
} else {
$this->_data[$key] = $value;
}
}
$this->_count = count($this->_data);
}
/**
* Retrieve a value and return $default if there is no element set.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
public function get($name, $default = null)
{
$result = $default;
if (array_key_exists($name, $this->_data)) {
$result = $this->_data[$name];
}
return $result;
}
/**
* Magic function so that $obj->value will work.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}
/**
* Only allow setting of a property if $allowModifications
* was set to true on construction. Otherwise, throw an exception.
*
* @param string $name
* @param mixed $value
* @throws Zend_Config_Exception
* @return void
*/
public function __set($name, $value)
{
if ($this->_allowModifications) {
if (is_array($value)) {
$this->_data[$name] = new self($value, true);
} else {
$this->_data[$name] = $value;
}
$this->_count = count($this->_data);
} else {
/** @see Zend_Config_Exception */
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Zend_Config is read only');
}
}
/**
* Deep clone of this instance to ensure that nested Zend_Configs
* are also cloned.
*
* @return void
*/
public function __clone()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof Zend_Config) {
$array[$key] = clone $value;
} else {
$array[$key] = $value;
}
}
$this->_data = $array;
}
/**
* Return an associative array of the stored data.
*
* @return array
*/
public function toArray()
{
$array = array();
$data = $this->_data;
foreach ($data as $key => $value) {
if ($value instanceof Zend_Config) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
/**
* Support isset() overloading on PHP 5.1
*
* @param string $name
* @return boolean
*/
public function __isset($name)
{
return isset($this->_data[$name]);
}
/**
* Support unset() overloading on PHP 5.1
*
* @param string $name
* @throws Zend_Config_Exception
* @return void
*/
public function __unset($name)
{
if ($this->_allowModifications) {
unset($this->_data[$name]);
$this->_count = count($this->_data);
$this->_skipNextIteration = true;
} else {
/** @see Zend_Config_Exception */
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Zend_Config is read only');
}
}
/**
* Defined by Countable interface
*
* @return int
*/
public function count()
{
return $this->_count;
}
/**
* Defined by Iterator interface
*
* @return mixed
*/
public function current()
{
$this->_skipNextIteration = false;
return current($this->_data);
}
/**
* Defined by Iterator interface
*
* @return mixed
*/
public function key()
{
return key($this->_data);
}
/**
* Defined by Iterator interface
*
*/
public function next()
{
if ($this->_skipNextIteration) {
$this->_skipNextIteration = false;
return;
}
next($this->_data);
$this->_index++;
}
/**
* Defined by Iterator interface
*
*/
public function rewind()
{
$this->_skipNextIteration = false;
reset($this->_data);
$this->_index = 0;
}
/**
* Defined by Iterator interface
*
* @return boolean
*/
public function valid()
{
return $this->_index < $this->_count;
}
/**
* Returns the section name(s) loaded.
*
* @return mixed
*/
public function getSectionName()
{
if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
$this->_loadedSection = $this->_loadedSection[0];
}
return $this->_loadedSection;
}
/**
* Returns true if all sections were loaded
*
* @return boolean
*/
public function areAllSectionsLoaded()
{
return $this->_loadedSection === null;
}
/**
* Merge another Zend_Config with this one. The items
* in $merge will override the same named items in
* the current config.
*
* @param Zend_Config $merge
* @return Zend_Config
*/
public function merge(Zend_Config $merge)
{
foreach($merge as $key => $item) {
if(array_key_exists($key, $this->_data)) {
if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
$this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
} else {
$this->$key = $item;
}
} else {
if($item instanceof Zend_Config) {
$this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
} else {
$this->$key = $item;
}
}
}
return $this;
}
/**
* Prevent any more modifications being made to this instance. Useful
* after merge() has been used to merge multiple Zend_Config objects
* into one object which should then not be modified again.
*
*/
public function setReadOnly()
{
$this->_allowModifications = false;
foreach ($this->_data as $key => $value) {
if ($value instanceof Zend_Config) {
$value->setReadOnly();
}
}
}
/**
* Returns if this Zend_Config object is read only or not.
*
* @return boolean
*/
public function readOnly()
{
return !$this->_allowModifications;
}
/**
* Get the current extends
*
* @return array
*/
public function getExtends()
{
return $this->_extends;
}
/**
* Set an extend for Zend_Config_Writer
*
* @param string $extendingSection
* @param string $extendedSection
* @return void
*/
public function setExtend($extendingSection, $extendedSection = null)
{
if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
unset($this->_extends[$extendingSection]);
} else if ($extendedSection !== null) {
$this->_extends[$extendingSection] = $extendedSection;
}
}
/**
* Throws an exception if $extendingSection may not extend $extendedSection,
* and tracks the section extension if it is valid.
*
* @param string $extendingSection
* @param string $extendedSection
* @throws Zend_Config_Exception
* @return void
*/
protected function _assertValidExtend($extendingSection, $extendedSection)
{
// detect circular section inheritance
$extendedSectionCurrent = $extendedSection;
while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
/** @see Zend_Config_Exception */
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Illegal circular inheritance detected');
}
$extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
}
// remember that this section extends another section
$this->_extends[$extendingSection] = $extendedSection;
}
/**
* Handle any errors from simplexml_load_file or parse_ini_file
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
*/
protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($this->_loadFileErrorStr === null) {
$this->_loadFileErrorStr = $errstr;
} else {
$this->_loadFileErrorStr .= (PHP_EOL . $errstr);
}
}
/**
* Merge two arrays recursively, overwriting keys of the same name
* in $firstArray with the value in $secondArray.
*
* @param mixed $firstArray First array
* @param mixed $secondArray Second array to merge into first array
* @return array
*/
protected function _arrayMergeRecursive($firstArray, $secondArray)
{
if (is_array($firstArray) && is_array($secondArray)) {
foreach ($secondArray as $key => $value) {
if (isset($firstArray[$key])) {
$firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
} else {
if($key === 0) {
$firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
} else {
$firstArray[$key] = $value;
}
}
}
} else {
$firstArray = $secondArray;
}
return $firstArray;
}
}