File indexing completed on 2025-03-02 05:29:14
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Config 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Config_Writer 0024 */ 0025 // require_once 'Zend/Config/Writer/FileAbstract.php'; 0026 0027 /** 0028 * @see Zend_Config_Xml 0029 */ 0030 // require_once 'Zend/Config/Xml.php'; 0031 0032 /** 0033 * @category Zend 0034 * @package Zend_Config 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 */ 0038 class Zend_Config_Writer_Xml extends Zend_Config_Writer_FileAbstract 0039 { 0040 /** 0041 * Render a Zend_Config into a XML config string. 0042 * 0043 * @since 1.10 0044 * @return string 0045 */ 0046 public function render() 0047 { 0048 $xml = new SimpleXMLElement('<zend-config xmlns:zf="' . Zend_Config_Xml::XML_NAMESPACE . '"/>'); 0049 $extends = $this->_config->getExtends(); 0050 $sectionName = $this->_config->getSectionName(); 0051 0052 if (is_string($sectionName)) { 0053 $child = $xml->addChild($sectionName); 0054 0055 $this->_addBranch($this->_config, $child, $xml); 0056 } else { 0057 foreach ($this->_config as $sectionName => $data) { 0058 if (!($data instanceof Zend_Config)) { 0059 $xml->addChild($sectionName, (string) $data); 0060 } else { 0061 $child = $xml->addChild($sectionName); 0062 0063 if (isset($extends[$sectionName])) { 0064 $child->addAttribute('zf:extends', $extends[$sectionName], Zend_Config_Xml::XML_NAMESPACE); 0065 } 0066 0067 $this->_addBranch($data, $child, $xml); 0068 } 0069 } 0070 } 0071 0072 $dom = dom_import_simplexml($xml)->ownerDocument; 0073 $dom->formatOutput = true; 0074 0075 $xmlString = $dom->saveXML(); 0076 0077 return $xmlString; 0078 } 0079 0080 /** 0081 * Add a branch to an XML object recursively 0082 * 0083 * @param Zend_Config $config 0084 * @param SimpleXMLElement $xml 0085 * @param SimpleXMLElement $parent 0086 * @return void 0087 */ 0088 protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent) 0089 { 0090 $branchType = null; 0091 0092 foreach ($config as $key => $value) { 0093 if ($branchType === null) { 0094 if (is_numeric($key)) { 0095 $branchType = 'numeric'; 0096 $branchName = $xml->getName(); 0097 $xml = $parent; 0098 0099 unset($parent->{$branchName}); 0100 } else { 0101 $branchType = 'string'; 0102 } 0103 } else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) { 0104 // require_once 'Zend/Config/Exception.php'; 0105 throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed'); 0106 } 0107 0108 if ($branchType === 'numeric') { 0109 if ($value instanceof Zend_Config) { 0110 $child = $parent->addChild($branchName); 0111 0112 $this->_addBranch($value, $child, $parent); 0113 } else { 0114 $parent->addChild($branchName, (string) $value); 0115 } 0116 } else { 0117 if ($value instanceof Zend_Config) { 0118 $child = $xml->addChild($key); 0119 0120 $this->_addBranch($value, $child, $xml); 0121 } else { 0122 $xml->addChild($key, (string) $value); 0123 } 0124 } 0125 } 0126 } 0127 }