PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Config.php

http://digitalus-site-manager.googlecode.com/
PHP | 389 lines | 159 code | 33 blank | 197 comment | 21 complexity | e0aae662ab19049d41e5dce666be7633 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Config.php 11181 2008-09-01 09:41:44Z alexander $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Config
  24. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Config implements Countable, Iterator
  28. {
  29. /**
  30. * Whether in-memory modifications to configuration data are allowed
  31. *
  32. * @var boolean
  33. */
  34. protected $_allowModifications;
  35. /**
  36. * Iteration index
  37. *
  38. * @var integer
  39. */
  40. protected $_index;
  41. /**
  42. * Number of elements in configuration data
  43. *
  44. * @var integer
  45. */
  46. protected $_count;
  47. /**
  48. * Contains array of configuration data
  49. *
  50. * @var array
  51. */
  52. protected $_data;
  53. /**
  54. * Contains which config file sections were loaded. This is null
  55. * if all sections were loaded, a string name if one section is loaded
  56. * and an array of string names if multiple sections were loaded.
  57. *
  58. * @var mixed
  59. */
  60. protected $_loadedSection;
  61. /**
  62. * This is used to track section inheritance. The keys are names of sections that
  63. * extend other sections, and the values are the extended sections.
  64. *
  65. * @var array
  66. */
  67. protected $_extends = array();
  68. /**
  69. * Load file error string.
  70. *
  71. * Is null if there was no error while file loading
  72. *
  73. * @var string
  74. */
  75. protected $_loadFileErrorStr = null;
  76. /**
  77. * Zend_Config provides a property based interface to
  78. * an array. The data are read-only unless $allowModifications
  79. * is set to true on construction.
  80. *
  81. * Zend_Config also implements Countable and Iterator to
  82. * facilitate easy access to the data.
  83. *
  84. * @param array $array
  85. * @param boolean $allowModifications
  86. * @return void
  87. */
  88. public function __construct(array $array, $allowModifications = false)
  89. {
  90. $this->_allowModifications = (boolean) $allowModifications;
  91. $this->_loadedSection = null;
  92. $this->_index = 0;
  93. $this->_data = array();
  94. foreach ($array as $key => $value) {
  95. if (is_array($value)) {
  96. $this->_data[$key] = new self($value, $this->_allowModifications);
  97. } else {
  98. $this->_data[$key] = $value;
  99. }
  100. }
  101. $this->_count = count($this->_data);
  102. }
  103. /**
  104. * Retrieve a value and return $default if there is no element set.
  105. *
  106. * @param string $name
  107. * @param mixed $default
  108. * @return mixed
  109. */
  110. public function get($name, $default = null)
  111. {
  112. $result = $default;
  113. if (array_key_exists($name, $this->_data)) {
  114. $result = $this->_data[$name];
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Magic function so that $obj->value will work.
  120. *
  121. * @param string $name
  122. * @return mixed
  123. */
  124. public function __get($name)
  125. {
  126. return $this->get($name);
  127. }
  128. /**
  129. * Only allow setting of a property if $allowModifications
  130. * was set to true on construction. Otherwise, throw an exception.
  131. *
  132. * @param string $name
  133. * @param mixed $value
  134. * @throws Zend_Config_Exception
  135. * @return void
  136. */
  137. public function __set($name, $value)
  138. {
  139. if ($this->_allowModifications) {
  140. if (is_array($value)) {
  141. $this->_data[$name] = new self($value, true);
  142. } else {
  143. $this->_data[$name] = $value;
  144. }
  145. $this->_count = count($this->_data);
  146. } else {
  147. /** @see Zend_Config_Exception */
  148. require_once 'Zend/Config/Exception.php';
  149. throw new Zend_Config_Exception('Zend_Config is read only');
  150. }
  151. }
  152. /**
  153. * Deep clone of this instance to ensure that nested Zend_Configs
  154. * are also cloned.
  155. *
  156. * @return void
  157. */
  158. public function __clone()
  159. {
  160. $array = array();
  161. foreach ($this->_data as $key => $value) {
  162. if ($value instanceof Zend_Config) {
  163. $array[$key] = clone $value;
  164. } else {
  165. $array[$key] = $value;
  166. }
  167. }
  168. $this->_data = $array;
  169. }
  170. /**
  171. * Return an associative array of the stored data.
  172. *
  173. * @return array
  174. */
  175. public function toArray()
  176. {
  177. $array = array();
  178. foreach ($this->_data as $key => $value) {
  179. if ($value instanceof Zend_Config) {
  180. $array[$key] = $value->toArray();
  181. } else {
  182. $array[$key] = $value;
  183. }
  184. }
  185. return $array;
  186. }
  187. /**
  188. * Support isset() overloading on PHP 5.1
  189. *
  190. * @param string $name
  191. * @return boolean
  192. */
  193. public function __isset($name)
  194. {
  195. return isset($this->_data[$name]);
  196. }
  197. /**
  198. * Support unset() overloading on PHP 5.1
  199. *
  200. * @param string $name
  201. * @throws Zend_Config_Exception
  202. * @return void
  203. */
  204. public function __unset($name)
  205. {
  206. if ($this->_allowModifications) {
  207. unset($this->_data[$name]);
  208. $this->_count = count($this->_data);
  209. } else {
  210. /** @see Zend_Config_Exception */
  211. require_once 'Zend/Config/Exception.php';
  212. throw new Zend_Config_Exception('Zend_Config is read only');
  213. }
  214. }
  215. /**
  216. * Defined by Countable interface
  217. *
  218. * @return int
  219. */
  220. public function count()
  221. {
  222. return $this->_count;
  223. }
  224. /**
  225. * Defined by Iterator interface
  226. *
  227. * @return mixed
  228. */
  229. public function current()
  230. {
  231. return current($this->_data);
  232. }
  233. /**
  234. * Defined by Iterator interface
  235. *
  236. * @return mixed
  237. */
  238. public function key()
  239. {
  240. return key($this->_data);
  241. }
  242. /**
  243. * Defined by Iterator interface
  244. *
  245. */
  246. public function next()
  247. {
  248. next($this->_data);
  249. $this->_index++;
  250. }
  251. /**
  252. * Defined by Iterator interface
  253. *
  254. */
  255. public function rewind()
  256. {
  257. reset($this->_data);
  258. $this->_index = 0;
  259. }
  260. /**
  261. * Defined by Iterator interface
  262. *
  263. * @return boolean
  264. */
  265. public function valid()
  266. {
  267. return $this->_index < $this->_count;
  268. }
  269. /**
  270. * Returns the section name(s) loaded.
  271. *
  272. * @return mixed
  273. */
  274. public function getSectionName()
  275. {
  276. return $this->_loadedSection;
  277. }
  278. /**
  279. * Returns true if all sections were loaded
  280. *
  281. * @return boolean
  282. */
  283. public function areAllSectionsLoaded()
  284. {
  285. return $this->_loadedSection === null;
  286. }
  287. /**
  288. * Merge another Zend_Config with this one. The items
  289. * in $merge will override the same named items in
  290. * the current config.
  291. *
  292. * @param Zend_Config $merge
  293. * @return Zend_Config
  294. */
  295. public function merge(Zend_Config $merge)
  296. {
  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);
  301. } else {
  302. $this->$key = $item;
  303. }
  304. } else {
  305. $this->$key = $item;
  306. }
  307. }
  308. return $this;
  309. }
  310. /**
  311. * Prevent any more modifications being made to this instance. Useful
  312. * after merge() has been used to merge multiple Zend_Config objects
  313. * into one object which should then not be modified again.
  314. *
  315. */
  316. public function setReadOnly()
  317. {
  318. $this->_allowModifications = false;
  319. }
  320. /**
  321. * Throws an exception if $extendingSection may not extend $extendedSection,
  322. * and tracks the section extension if it is valid.
  323. *
  324. * @param string $extendingSection
  325. * @param string $extendedSection
  326. * @throws Zend_Config_Exception
  327. * @return void
  328. */
  329. protected function _assertValidExtend($extendingSection, $extendedSection)
  330. {
  331. // detect circular section inheritance
  332. $extendedSectionCurrent = $extendedSection;
  333. while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
  334. if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
  335. /** @see Zend_Config_Exception */
  336. require_once 'Zend/Config/Exception.php';
  337. throw new Zend_Config_Exception('Illegal circular inheritance detected');
  338. }
  339. $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
  340. }
  341. // remember that this section extends another section
  342. $this->_extends[$extendingSection] = $extendedSection;
  343. }
  344. /**
  345. * Handle any errors from simplexml_load_file or parse_ini_file
  346. *
  347. * @param integer $errno
  348. * @param string $errstr
  349. * @param string $errfile
  350. * @param integer $errline
  351. */
  352. protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
  353. {
  354. $this->_loadFileErrorStr = $errstr;
  355. }
  356. }