PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Config/Xml.php

https://github.com/lanmediaservice/lms-tplib
PHP | 315 lines | 173 code | 41 blank | 101 comment | 53 complexity | 5709d2f7be35d8cfc650851e455e2366 MD5 | raw file
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Xml.php 17267 2009-07-29 02:13:18Z yoshida@zend.co.jp $
  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-2009 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. * XML namespace for ZF-related tags and attributes
  37. */
  38. const XML_NAMESPACE = 'http://framework.zend.com/xml/zend-config-xml/1.0/';
  39. /**
  40. * Wether to skip extends or not
  41. *
  42. * @var boolean
  43. */
  44. protected $_skipExtends = false;
  45. /**
  46. * Loads the section $section from the config file (or string $xml for
  47. * access facilitated by nested object properties.
  48. *
  49. * Sections are defined in the XML as children of the root element.
  50. *
  51. * In order to extend another section, a section defines the "extends"
  52. * attribute having a value of the section name from which the extending
  53. * section inherits values.
  54. *
  55. * Note that the keys in $section will override any keys of the same
  56. * name in the sections that have been included via "extends".
  57. *
  58. * @param string $xml XML file or string to process
  59. * @param mixed $section Section to process
  60. * @param boolean $options Whether modifiacations are allowed at runtime
  61. * @throws Zend_Config_Exception When xml is not set or cannot be loaded
  62. * @throws Zend_Config_Exception When section $sectionName cannot be found in $xml
  63. */
  64. public function __construct($xml, $section = null, $options = false)
  65. {
  66. if (empty($xml)) {
  67. //*** require_once 'Zend/Config/Exception.php';
  68. throw new Zend_Config_Exception('Filename is not set');
  69. }
  70. $allowModifications = false;
  71. if (is_bool($options)) {
  72. $allowModifications = $options;
  73. } elseif (is_array($options)) {
  74. if (isset($options['allowModifications'])) {
  75. $allowModifications = (bool) $options['allowModifications'];
  76. }
  77. if (isset($options['skipExtends'])) {
  78. $this->_skipExtends = (bool) $options['skipExtends'];
  79. }
  80. }
  81. set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
  82. if (strstr($xml, '<?xml')) {
  83. $config = simplexml_load_string($xml);
  84. } else {
  85. $config = simplexml_load_file($xml);
  86. }
  87. restore_error_handler();
  88. // Check if there was a error while loading file
  89. if ($this->_loadFileErrorStr !== null) {
  90. //*** require_once 'Zend/Config/Exception.php';
  91. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  92. }
  93. if ($section === null) {
  94. $dataArray = array();
  95. foreach ($config as $sectionName => $sectionData) {
  96. $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
  97. }
  98. parent::__construct($dataArray, $allowModifications);
  99. } else if (is_array($section)) {
  100. $dataArray = array();
  101. foreach ($section as $sectionName) {
  102. if (!isset($config->$sectionName)) {
  103. //*** require_once 'Zend/Config/Exception.php';
  104. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $xml");
  105. }
  106. $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
  107. }
  108. parent::__construct($dataArray, $allowModifications);
  109. } else {
  110. if (!isset($config->$section)) {
  111. //*** require_once 'Zend/Config/Exception.php';
  112. throw new Zend_Config_Exception("Section '$section' cannot be found in $xml");
  113. }
  114. $dataArray = $this->_processExtends($config, $section);
  115. if (!is_array($dataArray)) {
  116. // Section in the XML file contains just one top level string
  117. $dataArray = array($section => $dataArray);
  118. }
  119. parent::__construct($dataArray, $allowModifications);
  120. }
  121. $this->_loadedSection = $section;
  122. }
  123. /**
  124. * Helper function to process each element in the section and handle
  125. * the "extends" inheritance attribute.
  126. *
  127. * @param SimpleXMLElement $element XML Element to process
  128. * @param string $section Section to process
  129. * @param array $config Configuration which was parsed yet
  130. * @throws Zend_Config_Exception When $section cannot be found
  131. * @return array
  132. */
  133. protected function _processExtends(SimpleXMLElement $element, $section, array $config = array())
  134. {
  135. if (!isset($element->$section)) {
  136. //*** require_once 'Zend/Config/Exception.php';
  137. throw new Zend_Config_Exception("Section '$section' cannot be found");
  138. }
  139. $thisSection = $element->$section;
  140. $nsAttributes = $thisSection->attributes(self::XML_NAMESPACE);
  141. if (isset($thisSection['extends']) || isset($nsAttributes['extends'])) {
  142. $extendedSection = (string) (isset($nsAttributes['extends']) ? $nsAttributes['extends'] : $thisSection['extends']);
  143. $this->_assertValidExtend($section, $extendedSection);
  144. if (!$this->_skipExtends) {
  145. $config = $this->_processExtends($element, $extendedSection, $config);
  146. }
  147. }
  148. $config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection));
  149. return $config;
  150. }
  151. /**
  152. * Returns a string or an associative and possibly multidimensional array from
  153. * a SimpleXMLElement.
  154. *
  155. * @param SimpleXMLElement $xmlObject Convert a SimpleXMLElement into an array
  156. * @return array|string
  157. */
  158. protected function _toArray(SimpleXMLElement $xmlObject)
  159. {
  160. $config = array();
  161. $nsAttributes = $xmlObject->attributes(self::XML_NAMESPACE);
  162. // Search for parent node values
  163. if (count($xmlObject->attributes()) > 0) {
  164. foreach ($xmlObject->attributes() as $key => $value) {
  165. if ($key === 'extends') {
  166. continue;
  167. }
  168. $value = (string) $value;
  169. if (array_key_exists($key, $config)) {
  170. if (!is_array($config[$key])) {
  171. $config[$key] = array($config[$key]);
  172. }
  173. $config[$key][] = $value;
  174. } else {
  175. $config[$key] = $value;
  176. }
  177. }
  178. }
  179. // Search for local 'const' nodes and replace them
  180. if (count($xmlObject->children(self::XML_NAMESPACE)) > 0) {
  181. if (count($xmlObject->children()) > 0) {
  182. //*** require_once 'Zend/Config/Exception.php';
  183. throw new Zend_Config_Exception("A node with a 'const' childnode may not have any other children");
  184. }
  185. $dom = dom_import_simplexml($xmlObject);
  186. $namespaceChildNodes = array();
  187. // We have to store them in an array, as replacing nodes will
  188. // confuse the DOMNodeList later
  189. foreach ($dom->childNodes as $node) {
  190. if ($node instanceof DOMElement && $node->namespaceURI === self::XML_NAMESPACE) {
  191. $namespaceChildNodes[] = $node;
  192. }
  193. }
  194. foreach ($namespaceChildNodes as $node) {
  195. switch ($node->localName) {
  196. case 'const':
  197. if (!$node->hasAttributeNS(self::XML_NAMESPACE, 'name')) {
  198. //*** require_once 'Zend/Config/Exception.php';
  199. throw new Zend_Config_Exception("Misssing 'name' attribute in 'const' node");
  200. }
  201. $constantName = $node->getAttributeNS(self::XML_NAMESPACE, 'name');
  202. if (!defined($constantName)) {
  203. //*** require_once 'Zend/Config/Exception.php';
  204. throw new Zend_Config_Exception("Constant with name '$constantName' was not defined");
  205. }
  206. $constantValue = constant($constantName);
  207. $dom->replaceChild($dom->ownerDocument->createTextNode($constantValue), $node);
  208. break;
  209. default:
  210. //*** require_once 'Zend/Config/Exception.php';
  211. throw new Zend_Config_Exception("Unknown node with name '$node->localName' found");
  212. }
  213. }
  214. return (string) simplexml_import_dom($dom);
  215. }
  216. // Search for children
  217. if (count($xmlObject->children()) > 0) {
  218. foreach ($xmlObject->children() as $key => $value) {
  219. if (count($value->children()) > 0 || count($value->children(self::XML_NAMESPACE)) > 0) {
  220. $value = $this->_toArray($value);
  221. } else if (count($value->attributes()) > 0) {
  222. $attributes = $value->attributes();
  223. if (isset($attributes['value'])) {
  224. $value = (string) $attributes['value'];
  225. } else {
  226. $value = $this->_toArray($value);
  227. }
  228. } else {
  229. $value = (string) $value;
  230. }
  231. if (array_key_exists($key, $config)) {
  232. if (!is_array($config[$key]) || !array_key_exists(0, $config[$key])) {
  233. $config[$key] = array($config[$key]);
  234. }
  235. $config[$key][] = $value;
  236. } else {
  237. $config[$key] = $value;
  238. }
  239. }
  240. } else if (!isset($xmlObject['extends']) && !isset($nsAttributes['extends']) && (count($config) === 0)) {
  241. // Object has no children nor attributes and doesn't use the extends
  242. // attribute: it's a string
  243. $config = (string) $xmlObject;
  244. }
  245. return $config;
  246. }
  247. /**
  248. * Merge two arrays recursively, overwriting keys of the same name
  249. * in $firstArray with the value in $secondArray.
  250. *
  251. * @param mixed $firstArray First array
  252. * @param mixed $secondArray Second array to merge into first array
  253. * @return array
  254. */
  255. protected function _arrayMergeRecursive($firstArray, $secondArray)
  256. {
  257. if (is_array($firstArray) && is_array($secondArray)) {
  258. foreach ($secondArray as $key => $value) {
  259. if (isset($firstArray[$key])) {
  260. $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
  261. } else {
  262. if($key === 0) {
  263. $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
  264. } else {
  265. $firstArray[$key] = $value;
  266. }
  267. }
  268. }
  269. } else {
  270. $firstArray = $secondArray;
  271. }
  272. return $firstArray;
  273. }
  274. }