Xml.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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$
  20. */
  21. /**
  22. * @see Zend_Config_Writer
  23. */
  24. require_once 'Zend/Config/Writer.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-2009 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
  36. {
  37. /**
  38. * Filename to write to
  39. *
  40. * @var string
  41. */
  42. protected $_filename = null;
  43. /**
  44. * Wether to exclusively lock the file or not
  45. *
  46. * @var boolean
  47. */
  48. protected $_exclusiveLock = false;
  49. /**
  50. * Set the target filename
  51. *
  52. * @param string $filename
  53. * @return Zend_Config_Writer_Xml
  54. */
  55. public function setFilename($filename)
  56. {
  57. $this->_filename = $filename;
  58. return $this;
  59. }
  60. /**
  61. * Set wether to exclusively lock the file or not
  62. *
  63. * @param boolean $exclusiveLock
  64. * @return Zend_Config_Writer_Array
  65. */
  66. public function setExclusiveLock($exclusiveLock)
  67. {
  68. $this->_exclusiveLock = $exclusiveLock;
  69. return $this;
  70. }
  71. /**
  72. * Defined by Zend_Config_Writer
  73. *
  74. * @param string $filename
  75. * @param Zend_Config $config
  76. * @param boolean $exclusiveLock
  77. * @throws Zend_Config_Exception When filename was not set
  78. * @throws Zend_Config_Exception When filename is not writable
  79. * @return void
  80. */
  81. public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
  82. {
  83. if ($filename !== null) {
  84. $this->setFilename($filename);
  85. }
  86. if ($config !== null) {
  87. $this->setConfig($config);
  88. }
  89. if ($exclusiveLock !== null) {
  90. $this->setExclusiveLock($exclusiveLock);
  91. }
  92. if ($this->_filename === null) {
  93. require_once 'Zend/Config/Exception.php';
  94. throw new Zend_Config_Exception('No filename was set');
  95. }
  96. if ($this->_config === null) {
  97. require_once 'Zend/Config/Exception.php';
  98. throw new Zend_Config_Exception('No config was set');
  99. }
  100. $xml = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>');
  101. $extends = $this->_config->getExtends();
  102. $sectionName = $this->_config->getSectionName();
  103. if (is_string($sectionName)) {
  104. $child = $xml->addChild($sectionName);
  105. $this->_addBranch($this->_config, $child, $xml);
  106. } else {
  107. foreach ($this->_config as $sectionName => $data) {
  108. if (!($data instanceof Zend_Config)) {
  109. $xml->addChild($sectionName, (string) $data);
  110. } else {
  111. $child = $xml->addChild($sectionName);
  112. if (isset($extends[$sectionName])) {
  113. $child->addAttribute('zf:extends', $extends[$sectionName], Zend_Config_Xml::XML_NAMESPACE);
  114. }
  115. $this->_addBranch($data, $child, $xml);
  116. }
  117. }
  118. }
  119. $dom = dom_import_simplexml($xml)->ownerDocument;
  120. $dom->formatOutput = true;
  121. $xmlString = $dom->saveXML();
  122. $flags = 0;
  123. if ($this->_exclusiveLock) {
  124. $flags |= LOCK_EX;
  125. }
  126. $result = @file_put_contents($this->_filename, $xmlString, $flags);
  127. if ($result === false) {
  128. require_once 'Zend/Config/Exception.php';
  129. throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
  130. }
  131. }
  132. /**
  133. * Add a branch to an XML object recursively
  134. *
  135. * @param Zend_Config $config
  136. * @param SimpleXMLElement $xml
  137. * @param SimpleXMLElement $parent
  138. * @return void
  139. */
  140. protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
  141. {
  142. $branchType = null;
  143. foreach ($config as $key => $value) {
  144. if ($branchType === null) {
  145. if (is_numeric($key)) {
  146. $branchType = 'numeric';
  147. $branchName = $xml->getName();
  148. $xml = $parent;
  149. unset($parent->{$branchName});
  150. } else {
  151. $branchType = 'string';
  152. }
  153. } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
  154. require_once 'Zend/Config/Exception.php';
  155. throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
  156. }
  157. if ($branchType === 'numeric') {
  158. if ($value instanceof Zend_Config) {
  159. $child = $parent->addChild($branchName);
  160. $this->_addBranch($value, $child, $parent);
  161. } else {
  162. $parent->addChild($branchName, (string) $value);
  163. }
  164. } else {
  165. if ($value instanceof Zend_Config) {
  166. $child = $xml->addChild($key);
  167. $this->_addBranch($value, $child, $xml);
  168. } else {
  169. $xml->addChild($key, (string) $value);
  170. }
  171. }
  172. }
  173. }
  174. }