hostname === "staging" * $data->db->connection === "database" * * The $options parameter may be provided as either a boolean or an array. * If provided as a boolean, this sets the $allowModifications option of * Zend_Config. If provided as an array, there are two configuration * directives that may be set. For example: * * $options = array( * 'allowModifications' => false, * 'nestSeparator' => '->' * ); * * @param string $filename * @param string|null $section * @param boolean|array $options * @throws Zend_Config_Exception * @return void */ public function __construct($filename, $section = null, $options = false) { if (empty($filename)) { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception('Filename is not set'); } $allowModifications = false; if (is_bool($options)) { $allowModifications = $options; } elseif (is_array($options)) { if (isset($options['allowModifications'])) { $allowModifications = (bool) $options['allowModifications']; } if (isset($options['nestSeparator'])) { $this->_nestSeparator = (string) $options['nestSeparator']; } } set_error_handler(array($this, '_loadFileErrorHandler')); $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed restore_error_handler(); // Check if there was a error while loading file if ($this->_loadFileErrorStr !== null) { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception($this->_loadFileErrorStr); } $preProcessedArray = array(); foreach ($iniArray as $key => $data) { $bits = explode(':', $key); $thisSection = trim($bits[0]); switch (count($bits)) { case 1: $preProcessedArray[$thisSection] = $data; break; case 2: $extendedSection = trim($bits[1]); $preProcessedArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data); break; default: /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename"); } } if (null === $section) { $dataArray = array(); foreach ($preProcessedArray as $sectionName => $sectionData) { if(!is_array($sectionData)) { $dataArray = array_merge_recursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData)); } else { $dataArray[$sectionName] = $this->_processExtends($preProcessedArray, $sectionName); } } parent::__construct($dataArray, $allowModifications); } elseif (is_array($section)) { $dataArray = array(); foreach ($section as $sectionName) { if (!isset($preProcessedArray[$sectionName])) { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename"); } $dataArray = array_merge($this->_processExtends($preProcessedArray, $sectionName), $dataArray); } parent::__construct($dataArray, $allowModifications); } else { if (!isset($preProcessedArray[$section])) { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Section '$section' cannot be found in $filename"); } parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications); } $this->_loadedSection = $section; } /** * Helper function to process each element in the section and handle * the "extends" inheritance keyword. Passes control to _processKey() * to handle the "dot" sub-property syntax in each key. * * @param array $iniArray * @param string $section * @param array $config * @throws Zend_Config_Exception * @return array */ protected function _processExtends($iniArray, $section, $config = array()) { $thisSection = $iniArray[$section]; foreach ($thisSection as $key => $value) { if (strtolower($key) == ';extends') { if (isset($iniArray[$value])) { $this->_assertValidExtend($section, $value); $config = $this->_processExtends($iniArray, $value, $config); } else { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Section '$section' cannot be found"); } } else { $config = $this->_processKey($config, $key, $value); } } return $config; } /** * Assign the key's value to the property list. Handle the "dot" * notation for sub-properties by passing control to * processLevelsInKey(). * * @param array $config * @param string $key * @param string $value * @throws Zend_Config_Exception * @return array */ protected function _processKey($config, $key, $value) { if (strpos($key, $this->_nestSeparator) !== false) { $pieces = explode($this->_nestSeparator, $key, 2); if (strlen($pieces[0]) && strlen($pieces[1])) { if (!isset($config[$pieces[0]])) { $config[$pieces[0]] = array(); } elseif (!is_array($config[$pieces[0]])) { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists"); } $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value); } else { /** * @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Invalid key '$key'"); } } else { $config[$key] = $value; } return $config; } }