Config.php
6.69 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
<?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-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2007 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;
/**
* 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();
/**
* 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
* @throws Zend_Config_Exception
*/
public function __construct($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 Zend_Config($value, $this->_allowModifications);
} else {
$this->_data[$key] = $value;
}
}
$this->_count = count($this->_data);
}
/**
* Magic function so that $obj->value will work.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
$result = null;
if (isset($this->_data[$name])) {
$result = $this->_data[$name];
}
return $result;
}
/**
* 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
*/
public function __set($name, $value)
{
if ($this->_allowModifications) {
if (is_array($value)) {
$this->_data[$name] = new Zend_Config($value, true);
} else {
$this->_data[$name] = $value;
}
$this->_count = count($this->_data);
} else {
throw new Zend_Config_Exception('Zend_Config is read only');
}
}
/**
* Return an associative array of the stored data.
*
* @return array
*/
public function asArray()
{
$array = array();
foreach ($this->_data as $key => $value) {
if (is_object($value)) {
$array[$key] = $value->asArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
/**
* Support isset() overloading on PHP 5.1
*
* @param string $name
* @return boolean
*/
protected function __isset($name)
{
return isset($this->_data[$name]);
}
/**
* Defined by Countable interface
*
* @return int
*/
public function count()
{
return $this->_count;
}
/**
* Defined by Iterator interface
*
* @return mixed
*/
public function current()
{
return current($this->_data);
}
/**
* Defined by Iterator interface
*
* @return mixed
*/
public function key()
{
return key($this->_data);
}
/**
* Defined by Iterator interface
*
*/
public function next()
{
next($this->_data);
$this->_index++;
}
/**
* Defined by Iterator interface
*
*/
public function rewind()
{
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()
{
return $this->_loadedSection;
}
/**
* Returns true if all sections were loaded
*
* @return boolean
*/
public function areAllSectionsLoaded()
{
return $this->_loadedSection === null;
}
/**
* 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
*/
protected function _assertValidExtend($extendingSection, $extendedSection)
{
// detect circular section inheritance
$extendedSectionCurrent = $extendedSection;
while (isset($this->_extends[$extendedSectionCurrent])) {
if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
throw new Zend_Config_Exception('Illegal circular inheritance detected');
}
$extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
}
// remember that this section extends another section
$this->_extends[$extendingSection] = $extendedSection;
}
}