Xml.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * @see Zend_Config
  23. */
  24. require_once 'Zend/Config.php';
  25. /**
  26. * XML Adapter for Zend_Config
  27. *
  28. * @category Zend
  29. * @package Zend_Config
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Config_Xml extends Zend_Config
  34. {
  35. /**
  36. * Wether to skip extends or not
  37. *
  38. * @var boolean
  39. */
  40. protected $_skipExtends = false;
  41. /**
  42. * Loads the section $section from the config file (or string $xml for
  43. * access facilitated by nested object properties.
  44. *
  45. * Sections are defined in the XML as children of the root element.
  46. *
  47. * In order to extend another section, a section defines the "extends"
  48. * attribute having a value of the section name from which the extending
  49. * section inherits values.
  50. *
  51. * Note that the keys in $section will override any keys of the same
  52. * name in the sections that have been included via "extends".
  53. *
  54. * @param string $xml XML file or string to process
  55. * @param mixed $section Section to process
  56. * @param boolean $allowModifications Wether modifiacations are allowed at runtime
  57. * @throws Zend_Config_Exception When xml is not set or cannot be loaded
  58. * @throws Zend_Config_Exception When section $sectionName cannot be found in $xml
  59. */
  60. public function __construct($xml, $section = null, $options = false)
  61. {
  62. if (empty($xml)) {
  63. require_once 'Zend/Config/Exception.php';
  64. throw new Zend_Config_Exception('Filename is not set');
  65. }
  66. $allowModifications = false;
  67. if (is_bool($options)) {
  68. $allowModifications = $options;
  69. } elseif (is_array($options)) {
  70. if (isset($options['allowModifications'])) {
  71. $allowModifications = (bool) $options['allowModifications'];
  72. }
  73. if (isset($options['skipExtends'])) {
  74. $this->_skipExtends = (bool) $options['skipExtends'];
  75. }
  76. }
  77. set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
  78. if (strstr($xml, '<?xml')) {
  79. $config = simplexml_load_string($xml);
  80. } else {
  81. $config = simplexml_load_file($xml);
  82. }
  83. restore_error_handler();
  84. // Check if there was a error while loading file
  85. if ($this->_loadFileErrorStr !== null) {
  86. require_once 'Zend/Config/Exception.php';
  87. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  88. }
  89. if ($section === null) {
  90. $dataArray = array();
  91. foreach ($config as $sectionName => $sectionData) {
  92. $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
  93. }
  94. parent::__construct($dataArray, $allowModifications);
  95. } else if (is_array($section)) {
  96. $dataArray = array();
  97. foreach ($section as $sectionName) {
  98. if (!isset($config->$sectionName)) {
  99. require_once 'Zend/Config/Exception.php';
  100. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $xml");
  101. }
  102. $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
  103. }
  104. parent::__construct($dataArray, $allowModifications);
  105. } else {
  106. if (!isset($config->$section)) {
  107. require_once 'Zend/Config/Exception.php';
  108. throw new Zend_Config_Exception("Section '$section' cannot be found in $xml");
  109. }
  110. $dataArray = $this->_processExtends($config, $section);
  111. if (!is_array($dataArray)) {
  112. // Section in the XML file contains just one top level string
  113. $dataArray = array($section => $dataArray);
  114. }
  115. parent::__construct($dataArray, $allowModifications);
  116. }
  117. $this->_loadedSection = $section;
  118. }
  119. /**
  120. * Helper function to process each element in the section and handle
  121. * the "extends" inheritance attribute.
  122. *
  123. * @param SimpleXMLElement $element XML Element to process
  124. * @param string $section Section to process
  125. * @param array $config Configuration which was parsed yet
  126. * @throws Zend_Config_Exception When $section cannot be found
  127. * @return array
  128. */
  129. protected function _processExtends(SimpleXMLElement $element, $section, array $config = array())
  130. {
  131. if (!isset($element->$section)) {
  132. require_once 'Zend/Config/Exception.php';
  133. throw new Zend_Config_Exception("Section '$section' cannot be found");
  134. }
  135. $thisSection = $element->$section;
  136. if (isset($thisSection['extends'])) {
  137. $extendedSection = (string) $thisSection['extends'];
  138. $this->_assertValidExtend($section, $extendedSection);
  139. if (!$this->_skipExtends) {
  140. $config = $this->_processExtends($element, $extendedSection, $config);
  141. }
  142. }
  143. $config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection));
  144. return $config;
  145. }
  146. /**
  147. * Returns a string or an associative and possibly multidimensional array from
  148. * a SimpleXMLElement.
  149. *
  150. * @param SimpleXMLElement $xmlObject Convert a SimpleXMLElement into an array
  151. * @return array|string
  152. */
  153. protected function _toArray(SimpleXMLElement $xmlObject)
  154. {
  155. $config = array();
  156. // Search for parent node values
  157. if (count($xmlObject->attributes()) > 0) {
  158. foreach ($xmlObject->attributes() as $key => $value) {
  159. if ($key === 'extends') {
  160. continue;
  161. }
  162. $value = (string) $value;
  163. if (array_key_exists($key, $config)) {
  164. if (!is_array($config[$key])) {
  165. $config[$key] = array($config[$key]);
  166. }
  167. $config[$key][] = $value;
  168. } else {
  169. $config[$key] = $value;
  170. }
  171. }
  172. }
  173. // Search for children
  174. if (count($xmlObject->children()) > 0) {
  175. foreach ($xmlObject->children() as $key => $value) {
  176. if (count($value->children()) > 0) {
  177. $value = $this->_toArray($value);
  178. } else if (count($value->attributes()) > 0) {
  179. $attributes = $value->attributes();
  180. if (isset($attributes['value'])) {
  181. $value = (string) $attributes['value'];
  182. } else {
  183. $value = $this->_toArray($value);
  184. }
  185. } else {
  186. $value = (string) $value;
  187. }
  188. if (array_key_exists($key, $config)) {
  189. if (!is_array($config[$key]) || !array_key_exists(0, $config[$key])) {
  190. $config[$key] = array($config[$key]);
  191. }
  192. $config[$key][] = $value;
  193. } else {
  194. $config[$key] = $value;
  195. }
  196. }
  197. } else if (!isset($xmlObject['extends']) && (count($config) === 0)) {
  198. // Object has no children nor attributes and doesn't use the extends
  199. // attribute: it's a string
  200. $config = (string) $xmlObject;
  201. }
  202. return $config;
  203. }
  204. /**
  205. * Merge two arrays recursively, overwriting keys of the same name
  206. * in $firstArray with the value in $secondArray.
  207. *
  208. * @param mixed $firstArray First array
  209. * @param mixed $secondArray Second array to merge into first array
  210. * @return array
  211. */
  212. protected function _arrayMergeRecursive($firstArray, $secondArray)
  213. {
  214. if (is_array($firstArray) && is_array($secondArray)) {
  215. foreach ($secondArray as $key => $value) {
  216. if (isset($firstArray[$key])) {
  217. $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
  218. } else {
  219. if($key === 0) {
  220. $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
  221. } else {
  222. $firstArray[$key] = $value;
  223. }
  224. }
  225. }
  226. } else {
  227. $firstArray = $secondArray;
  228. }
  229. return $firstArray;
  230. }
  231. }