00001 <?php
00029 class Zend_Config implements Countable, Iterator
00030 {
00036 protected $_allowModifications;
00037
00043 protected $_index;
00044
00050 protected $_count;
00051
00057 protected $_data;
00058
00065 protected $_skipNextIteration;
00066
00074 protected $_loadedSection;
00075
00082 protected $_extends = array();
00083
00091 protected $_loadFileErrorStr = null;
00092
00105 public function __construct(array $array, $allowModifications = false)
00106 {
00107 $this->_allowModifications = (boolean) $allowModifications;
00108 $this->_loadedSection = null;
00109 $this->_index = 0;
00110 $this->_data = array();
00111 foreach ($array as $key => $value) {
00112 if (is_array($value)) {
00113 $this->_data[$key] = new self($value, $this->_allowModifications);
00114 } else {
00115 $this->_data[$key] = $value;
00116 }
00117 }
00118 $this->_count = count($this->_data);
00119 }
00120
00128 public function get($name, $default = null)
00129 {
00130 $result = $default;
00131 if (array_key_exists($name, $this->_data)) {
00132 $result = $this->_data[$name];
00133 }
00134 return $result;
00135 }
00136
00143 public function __get($name)
00144 {
00145 return $this->get($name);
00146 }
00147
00157 public function __set($name, $value)
00158 {
00159 if ($this->_allowModifications) {
00160 if (is_array($value)) {
00161 $this->_data[$name] = new self($value, true);
00162 } else {
00163 $this->_data[$name] = $value;
00164 }
00165 $this->_count = count($this->_data);
00166 } else {
00168 require_once 'Zend/Config/Exception.php';
00169 throw new Zend_Config_Exception('Zend_Config is read only');
00170 }
00171 }
00172
00179 public function __clone()
00180 {
00181 $array = array();
00182 foreach ($this->_data as $key => $value) {
00183 if ($value instanceof Zend_Config) {
00184 $array[$key] = clone $value;
00185 } else {
00186 $array[$key] = $value;
00187 }
00188 }
00189 $this->_data = $array;
00190 }
00191
00197 public function toArray()
00198 {
00199 $array = array();
00200 $data = $this->_data;
00201 foreach ($data as $key => $value) {
00202 if ($value instanceof Zend_Config) {
00203 $array[$key] = $value->toArray();
00204 } else {
00205 $array[$key] = $value;
00206 }
00207 }
00208 return $array;
00209 }
00210
00217 public function __isset($name)
00218 {
00219 return isset($this->_data[$name]);
00220 }
00221
00229 public function __unset($name)
00230 {
00231 if ($this->_allowModifications) {
00232 unset($this->_data[$name]);
00233 $this->_count = count($this->_data);
00234 $this->_skipNextIteration = true;
00235 } else {
00237 require_once 'Zend/Config/Exception.php';
00238 throw new Zend_Config_Exception('Zend_Config is read only');
00239 }
00240
00241 }
00242
00248 public function count()
00249 {
00250 return $this->_count;
00251 }
00252
00258 public function current()
00259 {
00260 $this->_skipNextIteration = false;
00261 return current($this->_data);
00262 }
00263
00269 public function key()
00270 {
00271 return key($this->_data);
00272 }
00273
00278 public function next()
00279 {
00280 if ($this->_skipNextIteration) {
00281 $this->_skipNextIteration = false;
00282 return;
00283 }
00284 next($this->_data);
00285 $this->_index++;
00286 }
00287
00292 public function rewind()
00293 {
00294 $this->_skipNextIteration = false;
00295 reset($this->_data);
00296 $this->_index = 0;
00297 }
00298
00304 public function valid()
00305 {
00306 return $this->_index < $this->_count;
00307 }
00308
00314 public function getSectionName()
00315 {
00316 if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
00317 $this->_loadedSection = $this->_loadedSection[0];
00318 }
00319 return $this->_loadedSection;
00320 }
00321
00327 public function areAllSectionsLoaded()
00328 {
00329 return $this->_loadedSection === null;
00330 }
00331
00332
00341 public function merge(Zend_Config $merge)
00342 {
00343 foreach($merge as $key => $item) {
00344 if(array_key_exists($key, $this->_data)) {
00345 if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
00346 $this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
00347 } else {
00348 $this->$key = $item;
00349 }
00350 } else {
00351 if($item instanceof Zend_Config) {
00352 $this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
00353 } else {
00354 $this->$key = $item;
00355 }
00356 }
00357 }
00358
00359 return $this;
00360 }
00361
00368 public function setReadOnly()
00369 {
00370 $this->_allowModifications = false;
00371 foreach ($this->_data as $key => $value) {
00372 if ($value instanceof Zend_Config) {
00373 $value->setReadOnly();
00374 }
00375 }
00376 }
00377
00383 public function readOnly()
00384 {
00385 return !$this->_allowModifications;
00386 }
00387
00393 public function getExtends()
00394 {
00395 return $this->_extends;
00396 }
00397
00405 public function setExtend($extendingSection, $extendedSection = null)
00406 {
00407 if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
00408 unset($this->_extends[$extendingSection]);
00409 } else if ($extendedSection !== null) {
00410 $this->_extends[$extendingSection] = $extendedSection;
00411 }
00412 }
00413
00423 protected function _assertValidExtend($extendingSection, $extendedSection)
00424 {
00425
00426 $extendedSectionCurrent = $extendedSection;
00427 while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
00428 if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
00430 require_once 'Zend/Config/Exception.php';
00431 throw new Zend_Config_Exception('Illegal circular inheritance detected');
00432 }
00433 $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
00434 }
00435
00436 $this->_extends[$extendingSection] = $extendedSection;
00437 }
00438
00447 protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
00448 {
00449 if ($this->_loadFileErrorStr === null) {
00450 $this->_loadFileErrorStr = $errstr;
00451 } else {
00452 $this->_loadFileErrorStr .= (PHP_EOL . $errstr);
00453 }
00454 }
00455
00456 }