Xml.php 12 KB

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