8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
17
* @package Zend_Config
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: Config.php 8064 2008-02-16 10:58:39Z thomas $
26
* @package Zend_Config
27
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
28
* @license http://framework.zend.com/license/new-bsd New BSD License
30
class Zend_Config implements Countable, Iterator
33
* Whether in-memory modifications to configuration data are allowed
37
protected $_allowModifications;
47
* Number of elements in configuration data
54
* Contains array of configuration data
62
* Contains which config file sections were loaded. This is null
63
* if all sections were loaded, a string name if one section is loaded
64
* and an array of string names if multiple sections were loaded.
68
protected $_loadedSection;
71
* This is used to track section inheritance. The keys are names of sections that
72
* extend other sections, and the values are the extended sections.
76
protected $_extends = array();
79
* Zend_Config provides a property based interface to
80
* an array. The data are read-only unless $allowModifications
81
* is set to true on construction.
83
* Zend_Config also implements Countable and Iterator to
84
* facilitate easy access to the data.
87
* @param boolean $allowModifications
90
public function __construct($array, $allowModifications = false)
92
$this->_allowModifications = (boolean) $allowModifications;
93
$this->_loadedSection = null;
95
$this->_data = array();
96
foreach ($array as $key => $value) {
97
if (is_array($value)) {
98
$this->_data[$key] = new self($value, $this->_allowModifications);
100
$this->_data[$key] = $value;
103
$this->_count = count($this->_data);
107
* Retrieve a value and return $default if there is no element set.
109
* @param string $name
110
* @param mixed $default
113
public function get($name, $default = null)
116
if (array_key_exists($name, $this->_data)) {
117
$result = $this->_data[$name];
123
* Magic function so that $obj->value will work.
125
* @param string $name
128
public function __get($name)
130
return $this->get($name);
134
* Only allow setting of a property if $allowModifications
135
* was set to true on construction. Otherwise, throw an exception.
137
* @param string $name
138
* @param mixed $value
139
* @throws Zend_Config_Exception
142
public function __set($name, $value)
144
if ($this->_allowModifications) {
145
if (is_array($value)) {
146
$this->_data[$name] = new self($value, true);
148
$this->_data[$name] = $value;
150
$this->_count = count($this->_data);
152
/** @see Zend_Config_Exception */
153
require_once 'Zend/Config/Exception.php';
154
throw new Zend_Config_Exception('Zend_Config is read only');
159
* Return an associative array of the stored data.
163
public function toArray()
166
foreach ($this->_data as $key => $value) {
167
if ($value instanceof Zend_Config) {
168
$array[$key] = $value->toArray();
170
$array[$key] = $value;
177
* Support isset() overloading on PHP 5.1
179
* @param string $name
182
protected function __isset($name)
184
return isset($this->_data[$name]);
188
* Support unset() overloading on PHP 5.1
190
* @param string $name
191
* @throws Zend_Config_Exception
194
protected function __unset($name)
196
if ($this->_allowModifications) {
197
unset($this->_data[$name]);
199
/** @see Zend_Config_Exception */
200
require_once 'Zend/Config/Exception.php';
201
throw new Zend_Config_Exception('Zend_Config is read only');
207
* Defined by Countable interface
211
public function count()
213
return $this->_count;
217
* Defined by Iterator interface
221
public function current()
223
return current($this->_data);
227
* Defined by Iterator interface
231
public function key()
233
return key($this->_data);
237
* Defined by Iterator interface
240
public function next()
247
* Defined by Iterator interface
250
public function rewind()
257
* Defined by Iterator interface
261
public function valid()
263
return $this->_index < $this->_count;
267
* Returns the section name(s) loaded.
271
public function getSectionName()
273
return $this->_loadedSection;
277
* Returns true if all sections were loaded
281
public function areAllSectionsLoaded()
283
return $this->_loadedSection === null;
288
* Merge another Zend_Config with this one. The items
289
* in $merge will override the same named items in
290
* the current config.
292
* @param Zend_Config $merge
293
* @return Zend_Config
295
public function merge(Zend_Config $merge)
297
foreach($merge as $key => $item) {
298
if(array_key_exists($key, $this->_data)) {
299
if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
300
$this->$key = $this->$key->merge($item);
313
* Prevent any more modifications being made to this instance. Useful
314
* after merge() has been used to merge multiple Zend_Config objects
315
* into one object which should then not be modified again.
318
public function setReadOnly()
320
$this->_allowModifications = false;
324
* Throws an exception if $extendingSection may not extend $extendedSection,
325
* and tracks the section extension if it is valid.
327
* @param string $extendingSection
328
* @param string $extendedSection
329
* @throws Zend_Config_Exception
332
protected function _assertValidExtend($extendingSection, $extendedSection)
334
// detect circular section inheritance
335
$extendedSectionCurrent = $extendedSection;
336
while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
337
if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
338
/** @see Zend_Config_Exception */
339
require_once 'Zend/Config/Exception.php';
340
throw new Zend_Config_Exception('Illegal circular inheritance detected');
342
$extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
344
// remember that this section extends another section
345
$this->_extends[$extendingSection] = $extendedSection;