Config.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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-2015 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-2015 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. $data = $this->_data;
  186. foreach ($data as $key => $value) {
  187. if ($value instanceof Zend_Config) {
  188. $array[$key] = $value->toArray();
  189. } else {
  190. $array[$key] = $value;
  191. }
  192. }
  193. return $array;
  194. }
  195. /**
  196. * Support isset() overloading on PHP 5.1
  197. *
  198. * @param string $name
  199. * @return boolean
  200. */
  201. public function __isset($name)
  202. {
  203. return isset($this->_data[$name]);
  204. }
  205. /**
  206. * Support unset() overloading on PHP 5.1
  207. *
  208. * @param string $name
  209. * @throws Zend_Config_Exception
  210. * @return void
  211. */
  212. public function __unset($name)
  213. {
  214. if ($this->_allowModifications) {
  215. unset($this->_data[$name]);
  216. $this->_count = count($this->_data);
  217. $this->_skipNextIteration = true;
  218. } else {
  219. /** @see Zend_Config_Exception */
  220. #require_once 'Zend/Config/Exception.php';
  221. throw new Zend_Config_Exception('Zend_Config is read only');
  222. }
  223. }
  224. /**
  225. * Defined by Countable interface
  226. *
  227. * @return int
  228. */
  229. public function count()
  230. {
  231. return $this->_count;
  232. }
  233. /**
  234. * Defined by Iterator interface
  235. *
  236. * @return mixed
  237. */
  238. public function current()
  239. {
  240. $this->_skipNextIteration = false;
  241. return current($this->_data);
  242. }
  243. /**
  244. * Defined by Iterator interface
  245. *
  246. * @return mixed
  247. */
  248. public function key()
  249. {
  250. return key($this->_data);
  251. }
  252. /**
  253. * Defined by Iterator interface
  254. *
  255. */
  256. public function next()
  257. {
  258. if ($this->_skipNextIteration) {
  259. $this->_skipNextIteration = false;
  260. return;
  261. }
  262. next($this->_data);
  263. $this->_index++;
  264. }
  265. /**
  266. * Defined by Iterator interface
  267. *
  268. */
  269. public function rewind()
  270. {
  271. $this->_skipNextIteration = false;
  272. reset($this->_data);
  273. $this->_index = 0;
  274. }
  275. /**
  276. * Defined by Iterator interface
  277. *
  278. * @return boolean
  279. */
  280. public function valid()
  281. {
  282. return $this->_index < $this->_count;
  283. }
  284. /**
  285. * Returns the section name(s) loaded.
  286. *
  287. * @return mixed
  288. */
  289. public function getSectionName()
  290. {
  291. if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
  292. $this->_loadedSection = $this->_loadedSection[0];
  293. }
  294. return $this->_loadedSection;
  295. }
  296. /**
  297. * Returns true if all sections were loaded
  298. *
  299. * @return boolean
  300. */
  301. public function areAllSectionsLoaded()
  302. {
  303. return $this->_loadedSection === null;
  304. }
  305. /**
  306. * Merge another Zend_Config with this one. The items
  307. * in $merge will override the same named items in
  308. * the current config.
  309. *
  310. * @param Zend_Config $merge
  311. * @return Zend_Config
  312. */
  313. public function merge(Zend_Config $merge)
  314. {
  315. foreach($merge as $key => $item) {
  316. if(array_key_exists($key, $this->_data)) {
  317. if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
  318. $this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
  319. } else {
  320. $this->$key = $item;
  321. }
  322. } else {
  323. if($item instanceof Zend_Config) {
  324. $this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
  325. } else {
  326. $this->$key = $item;
  327. }
  328. }
  329. }
  330. return $this;
  331. }
  332. /**
  333. * Prevent any more modifications being made to this instance. Useful
  334. * after merge() has been used to merge multiple Zend_Config objects
  335. * into one object which should then not be modified again.
  336. *
  337. */
  338. public function setReadOnly()
  339. {
  340. $this->_allowModifications = false;
  341. foreach ($this->_data as $key => $value) {
  342. if ($value instanceof Zend_Config) {
  343. $value->setReadOnly();
  344. }
  345. }
  346. }
  347. /**
  348. * Returns if this Zend_Config object is read only or not.
  349. *
  350. * @return boolean
  351. */
  352. public function readOnly()
  353. {
  354. return !$this->_allowModifications;
  355. }
  356. /**
  357. * Get the current extends
  358. *
  359. * @return array
  360. */
  361. public function getExtends()
  362. {
  363. return $this->_extends;
  364. }
  365. /**
  366. * Set an extend for Zend_Config_Writer
  367. *
  368. * @param string $extendingSection
  369. * @param string $extendedSection
  370. * @return void
  371. */
  372. public function setExtend($extendingSection, $extendedSection = null)
  373. {
  374. if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
  375. unset($this->_extends[$extendingSection]);
  376. } else if ($extendedSection !== null) {
  377. $this->_extends[$extendingSection] = $extendedSection;
  378. }
  379. }
  380. /**
  381. * Throws an exception if $extendingSection may not extend $extendedSection,
  382. * and tracks the section extension if it is valid.
  383. *
  384. * @param string $extendingSection
  385. * @param string $extendedSection
  386. * @throws Zend_Config_Exception
  387. * @return void
  388. */
  389. protected function _assertValidExtend($extendingSection, $extendedSection)
  390. {
  391. // detect circular section inheritance
  392. $extendedSectionCurrent = $extendedSection;
  393. while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
  394. if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
  395. /** @see Zend_Config_Exception */
  396. #require_once 'Zend/Config/Exception.php';
  397. throw new Zend_Config_Exception('Illegal circular inheritance detected');
  398. }
  399. $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
  400. }
  401. // remember that this section extends another section
  402. $this->_extends[$extendingSection] = $extendedSection;
  403. }
  404. /**
  405. * Handle any errors from simplexml_load_file or parse_ini_file
  406. *
  407. * @param integer $errno
  408. * @param string $errstr
  409. * @param string $errfile
  410. * @param integer $errline
  411. */
  412. public function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
  413. {
  414. if ($this->_loadFileErrorStr === null) {
  415. $this->_loadFileErrorStr = $errstr;
  416. } else {
  417. $this->_loadFileErrorStr .= (PHP_EOL . $errstr);
  418. }
  419. }
  420. /**
  421. * Merge two arrays recursively, overwriting keys of the same name
  422. * in $firstArray with the value in $secondArray.
  423. *
  424. * @param mixed $firstArray First array
  425. * @param mixed $secondArray Second array to merge into first array
  426. * @return array
  427. */
  428. protected function _arrayMergeRecursive($firstArray, $secondArray)
  429. {
  430. if (is_array($firstArray) && is_array($secondArray)) {
  431. foreach ($secondArray as $key => $value) {
  432. if (isset($firstArray[$key])) {
  433. $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
  434. } else {
  435. if($key === 0) {
  436. $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
  437. } else {
  438. $firstArray[$key] = $value;
  439. }
  440. }
  441. }
  442. } else {
  443. $firstArray = $secondArray;
  444. }
  445. return $firstArray;
  446. }
  447. }