PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Config/Xml.php

https://bitbucket.org/hmancvs/farmis
PHP | 296 lines | 164 code | 39 blank | 93 comment | 46 complexity | 32f63371c1dec1926ef3129337b0c9fb 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-2011 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,v 1.1 2011/11/03 08:29:27 smusoke Exp $
  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-2011 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. * Whether 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. * The $options parameter may be provided as either a boolean or an array.
  59. * If provided as a boolean, this sets the $allowModifications option of
  60. * Zend_Config. If provided as an array, there are two configuration
  61. * directives that may be set. For example:
  62. *
  63. * $options = array(
  64. * 'allowModifications' => false,
  65. * 'skipExtends' => false
  66. * );
  67. *
  68. * @param string $xml XML file or string to process
  69. * @param mixed $section Section to process
  70. * @param array|boolean $options
  71. * @throws Zend_Config_Exception When xml is not set or cannot be loaded
  72. * @throws Zend_Config_Exception When section $sectionName cannot be found in $xml
  73. */
  74. public function __construct($xml, $section = null, $options = false)
  75. {
  76. if (empty($xml)) {
  77. require_once 'Zend/Config/Exception.php';
  78. throw new Zend_Config_Exception('Filename is not set');
  79. }
  80. $allowModifications = false;
  81. if (is_bool($options)) {
  82. $allowModifications = $options;
  83. } elseif (is_array($options)) {
  84. if (isset($options['allowModifications'])) {
  85. $allowModifications = (bool) $options['allowModifications'];
  86. }
  87. if (isset($options['skipExtends'])) {
  88. $this->_skipExtends = (bool) $options['skipExtends'];
  89. }
  90. }
  91. set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
  92. if (strstr($xml, '<?xml')) {
  93. $config = simplexml_load_string($xml);
  94. } else {
  95. $config = simplexml_load_file($xml);
  96. }
  97. restore_error_handler();
  98. // Check if there was a error while loading file
  99. if ($this->_loadFileErrorStr !== null) {
  100. require_once 'Zend/Config/Exception.php';
  101. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  102. }
  103. if ($section === null) {
  104. $dataArray = array();
  105. foreach ($config as $sectionName => $sectionData) {
  106. $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
  107. }
  108. parent::__construct($dataArray, $allowModifications);
  109. } else if (is_array($section)) {
  110. $dataArray = array();
  111. foreach ($section as $sectionName) {
  112. if (!isset($config->$sectionName)) {
  113. require_once 'Zend/Config/Exception.php';
  114. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $xml");
  115. }
  116. $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
  117. }
  118. parent::__construct($dataArray, $allowModifications);
  119. } else {
  120. if (!isset($config->$section)) {
  121. require_once 'Zend/Config/Exception.php';
  122. throw new Zend_Config_Exception("Section '$section' cannot be found in $xml");
  123. }
  124. $dataArray = $this->_processExtends($config, $section);
  125. if (!is_array($dataArray)) {
  126. // Section in the XML file contains just one top level string
  127. $dataArray = array($section => $dataArray);
  128. }
  129. parent::__construct($dataArray, $allowModifications);
  130. }
  131. $this->_loadedSection = $section;
  132. }
  133. /**
  134. * Helper function to process each element in the section and handle
  135. * the "extends" inheritance attribute.
  136. *
  137. * @param SimpleXMLElement $element XML Element to process
  138. * @param string $section Section to process
  139. * @param array $config Configuration which was parsed yet
  140. * @throws Zend_Config_Exception When $section cannot be found
  141. * @return array
  142. */
  143. protected function _processExtends(SimpleXMLElement $element, $section, array $config = array())
  144. {
  145. if (!isset($element->$section)) {
  146. require_once 'Zend/Config/Exception.php';
  147. throw new Zend_Config_Exception("Section '$section' cannot be found");
  148. }
  149. $thisSection = $element->$section;
  150. $nsAttributes = $thisSection->attributes(self::XML_NAMESPACE);
  151. if (isset($thisSection['extends']) || isset($nsAttributes['extends'])) {
  152. $extendedSection = (string) (isset($nsAttributes['extends']) ? $nsAttributes['extends'] : $thisSection['extends']);
  153. $this->_assertValidExtend($section, $extendedSection);
  154. if (!$this->_skipExtends) {
  155. $config = $this->_processExtends($element, $extendedSection, $config);
  156. }
  157. }
  158. $config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection));
  159. return $config;
  160. }
  161. /**
  162. * Returns a string or an associative and possibly multidimensional array from
  163. * a SimpleXMLElement.
  164. *
  165. * @param SimpleXMLElement $xmlObject Convert a SimpleXMLElement into an array
  166. * @return array|string
  167. */
  168. protected function _toArray(SimpleXMLElement $xmlObject)
  169. {
  170. $config = array();
  171. $nsAttributes = $xmlObject->attributes(self::XML_NAMESPACE);
  172. // Search for parent node values
  173. if (count($xmlObject->attributes()) > 0) {
  174. foreach ($xmlObject->attributes() as $key => $value) {
  175. if ($key === 'extends') {
  176. continue;
  177. }
  178. $value = (string) $value;
  179. if (array_key_exists($key, $config)) {
  180. if (!is_array($config[$key])) {
  181. $config[$key] = array($config[$key]);
  182. }
  183. $config[$key][] = $value;
  184. } else {
  185. $config[$key] = $value;
  186. }
  187. }
  188. }
  189. // Search for local 'const' nodes and replace them
  190. if (count($xmlObject->children(self::XML_NAMESPACE)) > 0) {
  191. if (count($xmlObject->children()) > 0) {
  192. require_once 'Zend/Config/Exception.php';
  193. throw new Zend_Config_Exception("A node with a 'const' childnode may not have any other children");
  194. }
  195. $dom = dom_import_simplexml($xmlObject);
  196. $namespaceChildNodes = array();
  197. // We have to store them in an array, as replacing nodes will
  198. // confuse the DOMNodeList later
  199. foreach ($dom->childNodes as $node) {
  200. if ($node instanceof DOMElement && $node->namespaceURI === self::XML_NAMESPACE) {
  201. $namespaceChildNodes[] = $node;
  202. }
  203. }
  204. foreach ($namespaceChildNodes as $node) {
  205. switch ($node->localName) {
  206. case 'const':
  207. if (!$node->hasAttributeNS(self::XML_NAMESPACE, 'name')) {
  208. require_once 'Zend/Config/Exception.php';
  209. throw new Zend_Config_Exception("Misssing 'name' attribute in 'const' node");
  210. }
  211. $constantName = $node->getAttributeNS(self::XML_NAMESPACE, 'name');
  212. if (!defined($constantName)) {
  213. require_once 'Zend/Config/Exception.php';
  214. throw new Zend_Config_Exception("Constant with name '$constantName' was not defined");
  215. }
  216. $constantValue = constant($constantName);
  217. $dom->replaceChild($dom->ownerDocument->createTextNode($constantValue), $node);
  218. break;
  219. default:
  220. require_once 'Zend/Config/Exception.php';
  221. throw new Zend_Config_Exception("Unknown node with name '$node->localName' found");
  222. }
  223. }
  224. return (string) simplexml_import_dom($dom);
  225. }
  226. // Search for children
  227. if (count($xmlObject->children()) > 0) {
  228. foreach ($xmlObject->children() as $key => $value) {
  229. if (count($value->children()) > 0 || count($value->children(self::XML_NAMESPACE)) > 0) {
  230. $value = $this->_toArray($value);
  231. } else if (count($value->attributes()) > 0) {
  232. $attributes = $value->attributes();
  233. if (isset($attributes['value'])) {
  234. $value = (string) $attributes['value'];
  235. } else {
  236. $value = $this->_toArray($value);
  237. }
  238. } else {
  239. $value = (string) $value;
  240. }
  241. if (array_key_exists($key, $config)) {
  242. if (!is_array($config[$key]) || !array_key_exists(0, $config[$key])) {
  243. $config[$key] = array($config[$key]);
  244. }
  245. $config[$key][] = $value;
  246. } else {
  247. $config[$key] = $value;
  248. }
  249. }
  250. } else if (!isset($xmlObject['extends']) && !isset($nsAttributes['extends']) && (count($config) === 0)) {
  251. // Object has no children nor attributes and doesn't use the extends
  252. // attribute: it's a string
  253. $config = (string) $xmlObject;
  254. }
  255. return $config;
  256. }
  257. }