PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Config/Xml.php

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