Xml.php 6.0 KB

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