Config.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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$
  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. * Used when unsetting values during iteration to ensure we do not skip
  55. * the next element
  56. *
  57. * @var boolean
  58. */
  59. protected $_skipNextIteration;
  60. /**
  61. * Contains which config file sections were loaded. This is null
  62. * if all sections were loaded, a string name if one section is loaded
  63. * and an array of string names if multiple sections were loaded.
  64. *
  65. * @var mixed
  66. */
  67. protected $_loadedSection;
  68. /**
  69. * This is used to track section inheritance. The keys are names of sections that
  70. * extend other sections, and the values are the extended sections.
  71. *
  72. * @var array
  73. */
  74. protected $_extends = array();
  75. /**
  76. * Load file error string.
  77. *
  78. * Is null if there was no error while file loading
  79. *
  80. * @var string
  81. */
  82. protected $_loadFileErrorStr = null;
  83. /**
  84. * Zend_Config provides a property based interface to
  85. * an array. The data are read-only unless $allowModifications
  86. * is set to true on construction.
  87. *
  88. * Zend_Config also implements Countable and Iterator to
  89. * facilitate easy access to the data.
  90. *
  91. * @param array $array
  92. * @param boolean $allowModifications
  93. * @return void
  94. */
  95. public function __construct(array $array, $allowModifications = false)
  96. {
  97. $this->_allowModifications = (boolean) $allowModifications;
  98. $this->_loadedSection = null;
  99. $this->_index = 0;
  100. $this->_data = array();
  101. foreach ($array as $key => $value) {
  102. if (is_array($value)) {
  103. $this->_data[$key] = new self($value, $this->_allowModifications);
  104. } else {
  105. $this->_data[$key] = $value;
  106. }
  107. }
  108. $this->_count = count($this->_data);
  109. }
  110. /**
  111. * Retrieve a value and return $default if there is no element set.
  112. *
  113. * @param string $name
  114. * @param mixed $default
  115. * @return mixed
  116. */
  117. public function get($name, $default = null)
  118. {
  119. $result = $default;
  120. if (array_key_exists($name, $this->_data)) {
  121. $result = $this->_data[$name];
  122. }
  123. return $result;
  124. }
  125. /**
  126. * Magic function so that $obj->value will work.
  127. *
  128. * @param string $name
  129. * @return mixed
  130. */
  131. public function __get($name)
  132. {
  133. return $this->get($name);
  134. }
  135. /**
  136. * Only allow setting of a property if $allowModifications
  137. * was set to true on construction. Otherwise, throw an exception.
  138. *
  139. * @param string $name
  140. * @param mixed $value
  141. * @throws Zend_Config_Exception
  142. * @return void
  143. */
  144. public function __set($name, $value)
  145. {
  146. if ($this->_allowModifications) {
  147. if (is_array($value)) {
  148. $this->_data[$name] = new self($value, true);
  149. } else {
  150. $this->_data[$name] = $value;
  151. }
  152. $this->_count = count($this->_data);
  153. } else {
  154. /** @see Zend_Config_Exception */
  155. require_once 'Zend/Config/Exception.php';
  156. throw new Zend_Config_Exception('Zend_Config is read only');
  157. }
  158. }
  159. /**
  160. * Deep clone of this instance to ensure that nested Zend_Configs
  161. * are also cloned.
  162. *
  163. * @return void
  164. */
  165. public function __clone()
  166. {
  167. $array = array();
  168. foreach ($this->_data as $key => $value) {
  169. if ($value instanceof Zend_Config) {
  170. $array[$key] = clone $value;
  171. } else {
  172. $array[$key] = $value;
  173. }
  174. }
  175. $this->_data = $array;
  176. }
  177. /**
  178. * Return an associative array of the stored data.
  179. *
  180. * @return array
  181. */
  182. public function toArray()
  183. {
  184. $array = array();
  185. foreach ($this->_data as $key => $value) {
  186. if ($value instanceof Zend_Config) {
  187. $array[$key] = $value->toArray();
  188. } else {
  189. $array[$key] = $value;
  190. }
  191. }
  192. return $array;
  193. }
  194. /**
  195. * Support isset() overloading on PHP 5.1
  196. *
  197. * @param string $name
  198. * @return boolean
  199. */
  200. public function __isset($name)
  201. {
  202. return isset($this->_data[$name]);
  203. }
  204. /**
  205. * Support unset() overloading on PHP 5.1
  206. *
  207. * @param string $name
  208. * @throws Zend_Config_Exception
  209. * @return void
  210. */
  211. public function __unset($name)
  212. {
  213. if ($this->_allowModifications) {
  214. unset($this->_data[$name]);
  215. $this->_count = count($this->_data);
  216. $this->_skipNextIteration = true;
  217. } else {
  218. /** @see Zend_Config_Exception */
  219. require_once 'Zend/Config/Exception.php';
  220. throw new Zend_Config_Exception('Zend_Config is read only');
  221. }
  222. }
  223. /**
  224. * Defined by Countable interface
  225. *
  226. * @return int
  227. */
  228. public function count()
  229. {
  230. return $this->_count;
  231. }
  232. /**
  233. * Defined by Iterator interface
  234. *
  235. * @return mixed
  236. */
  237. public function current()
  238. {
  239. $this->_skipNextIteration = false;
  240. return current($this->_data);
  241. }
  242. /**
  243. * Defined by Iterator interface
  244. *
  245. * @return mixed
  246. */
  247. public function key()
  248. {
  249. return key($this->_data);
  250. }
  251. /**
  252. * Defined by Iterator interface
  253. *
  254. */
  255. public function next()
  256. {
  257. if ($this->_skipNextIteration) {
  258. $this->_skipNextIteration = false;
  259. return;
  260. }
  261. next($this->_data);
  262. $this->_index++;
  263. }
  264. /**
  265. * Defined by Iterator interface
  266. *
  267. */
  268. public function rewind()
  269. {
  270. $this->_skipNextIteration = false;
  271. reset($this->_data);
  272. $this->_index = 0;
  273. }
  274. /**
  275. * Defined by Iterator interface
  276. *
  277. * @return boolean
  278. */
  279. public function valid()
  280. {
  281. return $this->_index < $this->_count;
  282. }
  283. /**
  284. * Returns the section name(s) loaded.
  285. *
  286. * @return mixed
  287. */
  288. public function getSectionName()
  289. {
  290. if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
  291. $this->_loadedSection = $this->_loadedSection[0];
  292. }
  293. return $this->_loadedSection;
  294. }
  295. /**
  296. * Returns true if all sections were loaded
  297. *
  298. * @return boolean
  299. */
  300. public function areAllSectionsLoaded()
  301. {
  302. return $this->_loadedSection === null;
  303. }
  304. /**
  305. * Merge another Zend_Config with this one. The items
  306. * in $merge will override the same named items in
  307. * the current config.
  308. *
  309. * @param Zend_Config $merge
  310. * @return Zend_Config
  311. */
  312. public function merge(Zend_Config $merge)
  313. {
  314. foreach($merge as $key => $item) {
  315. if(array_key_exists($key, $this->_data)) {
  316. if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
  317. $this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
  318. } else {
  319. $this->$key = $item;
  320. }
  321. } else {
  322. if($item instanceof Zend_Config) {
  323. $this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
  324. } else {
  325. $this->$key = $item;
  326. }
  327. }
  328. }
  329. return $this;
  330. }
  331. /**
  332. * Prevent any more modifications being made to this instance. Useful
  333. * after merge() has been used to merge multiple Zend_Config objects
  334. * into one object which should then not be modified again.
  335. *
  336. */
  337. public function setReadOnly()
  338. {
  339. $this->_allowModifications = false;
  340. foreach ($this->_data as $key => $value) {
  341. if ($value instanceof Zend_Config) {
  342. $value->setReadOnly();
  343. }
  344. }
  345. }
  346. /**
  347. * Returns if this Zend_Config object is read only or not.
  348. *
  349. * @return boolean
  350. */
  351. public function readOnly()
  352. {
  353. return !$this->_allowModifications;
  354. }
  355. /**
  356. * Get the current extends
  357. *
  358. * @return array
  359. */
  360. public function getExtends()
  361. {
  362. return $this->_extends;
  363. }
  364. /**
  365. * Set an extend for Zend_Config_Writer
  366. *
  367. * @param string $extendingSection
  368. * @param string $extendedSection
  369. * @return void
  370. */
  371. public function setExtend($extendingSection, $extendedSection = null)
  372. {
  373. if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
  374. unset($this->_extends[$extendingSection]);
  375. } else if ($extendedSection !== null) {
  376. $this->_extends[$extendingSection] = $extendedSection;
  377. }
  378. }
  379. /**
  380. * Throws an exception if $extendingSection may not extend $extendedSection,
  381. * and tracks the section extension if it is valid.
  382. *
  383. * @param string $extendingSection
  384. * @param string $extendedSection
  385. * @throws Zend_Config_Exception
  386. * @return void
  387. */
  388. protected function _assertValidExtend($extendingSection, $extendedSection)
  389. {
  390. // detect circular section inheritance
  391. $extendedSectionCurrent = $extendedSection;
  392. while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
  393. if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
  394. /** @see Zend_Config_Exception */
  395. require_once 'Zend/Config/Exception.php';
  396. throw new Zend_Config_Exception('Illegal circular inheritance detected');
  397. }
  398. $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
  399. }
  400. // remember that this section extends another section
  401. $this->_extends[$extendingSection] = $extendedSection;
  402. }
  403. /**
  404. * Handle any errors from simplexml_load_file or parse_ini_file
  405. *
  406. * @param integer $errno
  407. * @param string $errstr
  408. * @param string $errfile
  409. * @param integer $errline
  410. */
  411. protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
  412. {
  413. if ($this->_loadFileErrorStr === null) {
  414. $this->_loadFileErrorStr = $errstr;
  415. } else {
  416. $this->_loadFileErrorStr .= (PHP_EOL . $errstr);
  417. }
  418. }
  419. }