$sectionData) { $dataArray[$sectionName] = $this->_processExtends($config, $sectionName); } parent::__construct($dataArray, $allowModifications); } elseif (is_array($section)) { $dataArray = array(); foreach ($section as $sectionName) { if (!isset($config->$sectionName)) { throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename"); } $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray); } parent::__construct($dataArray, $allowModifications); } else { if (!isset($config->$section)) { throw new Zend_Config_Exception("Section '$section' cannot be found in $filename"); } parent::__construct($this->_processExtends($config, $section), $allowModifications); } $this->_loadedSection = $section; } /** * Helper function to process each element in the section and handle * the "extends" inheritance attribute. * * @param SimpleXMLElement $element * @param string $section * @param array $config * @throws Zend_Config_Exception * @return array */ protected function _processExtends($element, $section, $config = array()) { if (!$element->$section) { throw new Zend_Config_Exception("Section '$section' cannot be found"); } $thisSection = $element->$section; if (isset($thisSection['extends'])) { $extendedSection = (string) $thisSection['extends']; $this->_assertValidExtend($section, $extendedSection); $config = $this->_processExtends($element, $extendedSection, $config); } $config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection)); return $config; } /** * Returns an associative and possibly multidimensional array from a SimpleXMLElement. * * @param SimpleXMLElement $xmlObject * @return array */ protected function _toArray($xmlObject) { $config = array(); foreach ($xmlObject->children() as $key => $value) { if ($value->children()) { $config[$key] = $this->_toArray($value); } else { $config[$key] = (string) $value; } } return $config; } /** * Merge two arrays recursively, overwriting keys of the same name name * in $array1 with the value in $array2. * * @param array $array1 * @param array $array2 * @return array */ protected function _arrayMergeRecursive($array1, $array2) { if (is_array($array1) && is_array($array2)) { foreach ($array2 as $key => $value) { if (isset($array1[$key])) { $array1[$key] = $this->_arrayMergeRecursive($array1[$key], $value); } else { $array1[$key] = $value; } } } else { $array1 = $array2; } return $array1; } }