File indexing completed on 2024-12-22 05:36:32
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 /** 0024 * @see Zend_Config 0025 */ 0026 // require_once 'Zend/Config.php'; 0027 0028 0029 /** 0030 * @category Zend 0031 * @package Zend_Config 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_Config_Ini extends Zend_Config 0036 { 0037 /** 0038 * String that separates nesting levels of configuration data identifiers 0039 * 0040 * @var string 0041 */ 0042 protected $_nestSeparator = '.'; 0043 0044 /** 0045 * String that separates the parent section name 0046 * 0047 * @var string 0048 */ 0049 protected $_sectionSeparator = ':'; 0050 0051 /** 0052 * Whether to skip extends or not 0053 * 0054 * @var boolean 0055 */ 0056 protected $_skipExtends = false; 0057 0058 /** 0059 * Loads the section $section from the config file $filename for 0060 * access facilitated by nested object properties. 0061 * 0062 * If the section name contains a ":" then the section name to the right 0063 * is loaded and included into the properties. Note that the keys in 0064 * this $section will override any keys of the same 0065 * name in the sections that have been included via ":". 0066 * 0067 * If the $section is null, then all sections in the ini file are loaded. 0068 * 0069 * If any key includes a ".", then this will act as a separator to 0070 * create a sub-property. 0071 * 0072 * example ini file: 0073 * [all] 0074 * db.connection = database 0075 * hostname = live 0076 * 0077 * [staging : all] 0078 * hostname = staging 0079 * 0080 * after calling $data = new Zend_Config_Ini($file, 'staging'); then 0081 * $data->hostname === "staging" 0082 * $data->db->connection === "database" 0083 * 0084 * The $options parameter may be provided as either a boolean or an array. 0085 * If provided as a boolean, this sets the $allowModifications option of 0086 * Zend_Config. If provided as an array, there are three configuration 0087 * directives that may be set. For example: 0088 * 0089 * $options = array( 0090 * 'allowModifications' => false, 0091 * 'nestSeparator' => ':', 0092 * 'skipExtends' => false, 0093 * ); 0094 * 0095 * @param string $filename 0096 * @param mixed $section 0097 * @param boolean|array $options 0098 * @throws Zend_Config_Exception 0099 * @return void 0100 */ 0101 public function __construct($filename, $section = null, $options = false) 0102 { 0103 if (empty($filename)) { 0104 /** 0105 * @see Zend_Config_Exception 0106 */ 0107 // require_once 'Zend/Config/Exception.php'; 0108 throw new Zend_Config_Exception('Filename is not set'); 0109 } 0110 0111 $allowModifications = false; 0112 if (is_bool($options)) { 0113 $allowModifications = $options; 0114 } elseif (is_array($options)) { 0115 if (isset($options['allowModifications'])) { 0116 $allowModifications = (bool) $options['allowModifications']; 0117 } 0118 if (isset($options['nestSeparator'])) { 0119 $this->_nestSeparator = (string) $options['nestSeparator']; 0120 } 0121 if (isset($options['skipExtends'])) { 0122 $this->_skipExtends = (bool) $options['skipExtends']; 0123 } 0124 } 0125 0126 $iniArray = $this->_loadIniFile($filename); 0127 0128 if (null === $section) { 0129 // Load entire file 0130 $dataArray = array(); 0131 foreach ($iniArray as $sectionName => $sectionData) { 0132 if(!is_array($sectionData)) { 0133 $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData)); 0134 } else { 0135 $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName); 0136 } 0137 } 0138 parent::__construct($dataArray, $allowModifications); 0139 } else { 0140 // Load one or more sections 0141 if (!is_array($section)) { 0142 $section = array($section); 0143 } 0144 $dataArray = array(); 0145 foreach ($section as $sectionName) { 0146 if (!isset($iniArray[$sectionName])) { 0147 /** 0148 * @see Zend_Config_Exception 0149 */ 0150 // require_once 'Zend/Config/Exception.php'; 0151 throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename"); 0152 } 0153 $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray); 0154 0155 } 0156 parent::__construct($dataArray, $allowModifications); 0157 } 0158 0159 $this->_loadedSection = $section; 0160 } 0161 0162 /** 0163 * Load the INI file from disk using parse_ini_file(). Use a private error 0164 * handler to convert any loading errors into a Zend_Config_Exception 0165 * 0166 * @param string $filename 0167 * @throws Zend_Config_Exception 0168 * @return array 0169 */ 0170 protected function _parseIniFile($filename) 0171 { 0172 set_error_handler(array($this, '_loadFileErrorHandler')); 0173 $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed 0174 restore_error_handler(); 0175 0176 // Check if there was a error while loading file 0177 if ($this->_loadFileErrorStr !== null) { 0178 /** 0179 * @see Zend_Config_Exception 0180 */ 0181 // require_once 'Zend/Config/Exception.php'; 0182 throw new Zend_Config_Exception($this->_loadFileErrorStr); 0183 } 0184 0185 return $iniArray; 0186 } 0187 0188 /** 0189 * Load the ini file and preprocess the section separator (':' in the 0190 * section name (that is used for section extension) so that the resultant 0191 * array has the correct section names and the extension information is 0192 * stored in a sub-key called ';extends'. We use ';extends' as this can 0193 * never be a valid key name in an INI file that has been loaded using 0194 * parse_ini_file(). 0195 * 0196 * @param string $filename 0197 * @throws Zend_Config_Exception 0198 * @return array 0199 */ 0200 protected function _loadIniFile($filename) 0201 { 0202 $loaded = $this->_parseIniFile($filename); 0203 $iniArray = array(); 0204 foreach ($loaded as $key => $data) 0205 { 0206 $pieces = explode($this->_sectionSeparator, $key); 0207 $thisSection = trim($pieces[0]); 0208 switch (count($pieces)) { 0209 case 1: 0210 $iniArray[$thisSection] = $data; 0211 break; 0212 0213 case 2: 0214 $extendedSection = trim($pieces[1]); 0215 $iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data); 0216 break; 0217 0218 default: 0219 /** 0220 * @see Zend_Config_Exception 0221 */ 0222 // require_once 'Zend/Config/Exception.php'; 0223 throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename"); 0224 } 0225 } 0226 0227 return $iniArray; 0228 } 0229 0230 /** 0231 * Process each element in the section and handle the ";extends" inheritance 0232 * key. Passes control to _processKey() to handle the nest separator 0233 * sub-property syntax that may be used within the key name. 0234 * 0235 * @param array $iniArray 0236 * @param string $section 0237 * @param array $config 0238 * @throws Zend_Config_Exception 0239 * @return array 0240 */ 0241 protected function _processSection($iniArray, $section, $config = array()) 0242 { 0243 $thisSection = $iniArray[$section]; 0244 0245 foreach ($thisSection as $key => $value) { 0246 if (strtolower($key) == ';extends') { 0247 if (isset($iniArray[$value])) { 0248 $this->_assertValidExtend($section, $value); 0249 0250 if (!$this->_skipExtends) { 0251 $config = $this->_processSection($iniArray, $value, $config); 0252 } 0253 } else { 0254 /** 0255 * @see Zend_Config_Exception 0256 */ 0257 // require_once 'Zend/Config/Exception.php'; 0258 throw new Zend_Config_Exception("Parent section '$section' cannot be found"); 0259 } 0260 } else { 0261 $config = $this->_processKey($config, $key, $value); 0262 } 0263 } 0264 return $config; 0265 } 0266 0267 /** 0268 * Assign the key's value to the property list. Handles the 0269 * nest separator for sub-properties. 0270 * 0271 * @param array $config 0272 * @param string $key 0273 * @param string $value 0274 * @throws Zend_Config_Exception 0275 * @return array 0276 */ 0277 protected function _processKey($config, $key, $value) 0278 { 0279 if (strpos($key, $this->_nestSeparator) !== false) { 0280 $pieces = explode($this->_nestSeparator, $key, 2); 0281 if (strlen($pieces[0]) && strlen($pieces[1])) { 0282 if (!isset($config[$pieces[0]])) { 0283 if ($pieces[0] === '0' && !empty($config)) { 0284 // convert the current values in $config into an array 0285 $config = array($pieces[0] => $config); 0286 } else { 0287 $config[$pieces[0]] = array(); 0288 } 0289 } elseif (!is_array($config[$pieces[0]])) { 0290 /** 0291 * @see Zend_Config_Exception 0292 */ 0293 // require_once 'Zend/Config/Exception.php'; 0294 throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists"); 0295 } 0296 $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value); 0297 } else { 0298 /** 0299 * @see Zend_Config_Exception 0300 */ 0301 // require_once 'Zend/Config/Exception.php'; 0302 throw new Zend_Config_Exception("Invalid key '$key'"); 0303 } 0304 } else { 0305 $config[$key] = $value; 0306 } 0307 return $config; 0308 } 0309 }