Xml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_Writer
  23. */
  24. #require_once 'Zend/Config/Writer/FileAbstract.php';
  25. /**
  26. * @see Zend_Config_Xml
  27. */
  28. #require_once 'Zend/Config/Xml.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Config
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Config_Writer_Xml extends Zend_Config_Writer_FileAbstract
  36. {
  37. /**
  38. * Render a Zend_Config into a XML config string.
  39. *
  40. * @since 1.10
  41. * @return string
  42. */
  43. public function render()
  44. {
  45. $xml = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>');
  46. $extends = $this->_config->getExtends();
  47. $sectionName = $this->_config->getSectionName();
  48. if (is_string($sectionName)) {
  49. $child = $xml->addChild($sectionName);
  50. $this->_addBranch($this->_config, $child, $xml);
  51. } else {
  52. foreach ($this->_config as $sectionName => $data) {
  53. if (!($data instanceof Zend_Config)) {
  54. $xml->addChild($sectionName, (string) $data);
  55. } else {
  56. $child = $xml->addChild($sectionName);
  57. if (isset($extends[$sectionName])) {
  58. $child->addAttribute('zf:extends', $extends[$sectionName], Zend_Config_Xml::XML_NAMESPACE);
  59. }
  60. $this->_addBranch($data, $child, $xml);
  61. }
  62. }
  63. }
  64. $dom = dom_import_simplexml($xml)->ownerDocument;
  65. $dom->formatOutput = true;
  66. $xmlString = $dom->saveXML();
  67. return $xmlString;
  68. }
  69. /**
  70. * Add a branch to an XML object recursively
  71. *
  72. * @param Zend_Config $config
  73. * @param SimpleXMLElement $xml
  74. * @param SimpleXMLElement $parent
  75. * @return void
  76. */
  77. protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
  78. {
  79. $branchType = null;
  80. foreach ($config as $key => $value) {
  81. if ($branchType === null) {
  82. if (is_numeric($key)) {
  83. $branchType = 'numeric';
  84. $branchName = $xml->getName();
  85. $xml = $parent;
  86. unset($parent->{$branchName});
  87. } else {
  88. $branchType = 'string';
  89. }
  90. } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
  91. #require_once 'Zend/Config/Exception.php';
  92. throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
  93. }
  94. if ($branchType === 'numeric') {
  95. if ($value instanceof Zend_Config) {
  96. $child = $parent->addChild($branchName);
  97. $this->_addBranch($value, $child, $parent);
  98. } else {
  99. $parent->addChild($branchName, (string) $value);
  100. }
  101. } else {
  102. if ($value instanceof Zend_Config) {
  103. $child = $xml->addChild($key);
  104. $this->_addBranch($value, $child, $xml);
  105. } else {
  106. $xml->addChild($key, (string) $value);
  107. }
  108. }
  109. }
  110. }
  111. }