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 * @category Zend 0029 * @package Zend_Config 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Config_Writer_Ini extends Zend_Config_Writer_FileAbstract 0034 { 0035 /** 0036 * String that separates nesting levels of configuration data identifiers 0037 * 0038 * @var string 0039 */ 0040 protected $_nestSeparator = '.'; 0041 0042 /** 0043 * If true the ini string is rendered in the global namespace without sections. 0044 * 0045 * @var bool 0046 */ 0047 protected $_renderWithoutSections = false; 0048 0049 /** 0050 * Set the nest separator 0051 * 0052 * @param string $filename 0053 * @return Zend_Config_Writer_Ini 0054 */ 0055 public function setNestSeparator($separator) 0056 { 0057 $this->_nestSeparator = $separator; 0058 0059 return $this; 0060 } 0061 0062 /** 0063 * Set if rendering should occour without sections or not. 0064 * 0065 * If set to true, the INI file is rendered without sections completely 0066 * into the global namespace of the INI file. 0067 * 0068 * @param bool $withoutSections 0069 * @return Zend_Config_Writer_Ini 0070 */ 0071 public function setRenderWithoutSections($withoutSections=true) 0072 { 0073 $this->_renderWithoutSections = (bool)$withoutSections; 0074 return $this; 0075 } 0076 0077 /** 0078 * Render a Zend_Config into a INI config string. 0079 * 0080 * @since 1.10 0081 * @return string 0082 */ 0083 public function render() 0084 { 0085 $iniString = ''; 0086 $extends = $this->_config->getExtends(); 0087 $sectionName = $this->_config->getSectionName(); 0088 0089 if($this->_renderWithoutSections == true) { 0090 $iniString .= $this->_addBranch($this->_config); 0091 } else if (is_string($sectionName)) { 0092 $iniString .= '[' . $sectionName . ']' . "\n" 0093 . $this->_addBranch($this->_config) 0094 . "\n"; 0095 } else { 0096 $config = $this->_sortRootElements($this->_config); 0097 foreach ($config as $sectionName => $data) { 0098 if (!($data instanceof Zend_Config)) { 0099 $iniString .= $sectionName 0100 . ' = ' 0101 . $this->_prepareValue($data) 0102 . "\n"; 0103 } else { 0104 if (isset($extends[$sectionName])) { 0105 $sectionName .= ' : ' . $extends[$sectionName]; 0106 } 0107 0108 $iniString .= '[' . $sectionName . ']' . "\n" 0109 . $this->_addBranch($data) 0110 . "\n"; 0111 } 0112 } 0113 } 0114 0115 return $iniString; 0116 } 0117 0118 /** 0119 * Add a branch to an INI string recursively 0120 * 0121 * @param Zend_Config $config 0122 * @return void 0123 */ 0124 protected function _addBranch(Zend_Config $config, $parents = array()) 0125 { 0126 $iniString = ''; 0127 0128 foreach ($config as $key => $value) { 0129 $group = array_merge($parents, array($key)); 0130 0131 if ($value instanceof Zend_Config) { 0132 $iniString .= $this->_addBranch($value, $group); 0133 } else { 0134 $iniString .= implode($this->_nestSeparator, $group) 0135 . ' = ' 0136 . $this->_prepareValue($value) 0137 . "\n"; 0138 } 0139 } 0140 0141 return $iniString; 0142 } 0143 0144 /** 0145 * Prepare a value for INI 0146 * 0147 * @param mixed $value 0148 * @return string 0149 */ 0150 protected function _prepareValue($value) 0151 { 0152 if (is_integer($value) || is_float($value)) { 0153 return $value; 0154 } elseif (is_bool($value)) { 0155 return ($value ? 'true' : 'false'); 0156 } elseif (strpos($value, '"') === false) { 0157 return '"' . $value . '"'; 0158 } else { 0159 /** @see Zend_Config_Exception */ 0160 // require_once 'Zend/Config/Exception.php'; 0161 throw new Zend_Config_Exception('Value can not contain double quotes "'); 0162 } 0163 } 0164 0165 /** 0166 * Root elements that are not assigned to any section needs to be 0167 * on the top of config. 0168 * 0169 * @see http://framework.zend.com/issues/browse/ZF-6289 0170 * @param Zend_Config 0171 * @return Zend_Config 0172 */ 0173 protected function _sortRootElements(Zend_Config $config) 0174 { 0175 $configArray = $config->toArray(); 0176 $sections = array(); 0177 0178 // remove sections from config array 0179 foreach ($configArray as $key => $value) { 0180 if (is_array($value)) { 0181 $sections[$key] = $value; 0182 unset($configArray[$key]); 0183 } 0184 } 0185 0186 // readd sections to the end 0187 foreach ($sections as $key => $value) { 0188 $configArray[$key] = $value; 0189 } 0190 0191 return new Zend_Config($configArray); 0192 } 0193 }