import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -16,7 +16,7 @@
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Ini.php 11206 2008-09-03 14:36:32Z ralph $
|
||||
* @version $Id: Ini.php 14667 2009-04-05 09:18:21Z rob $
|
||||
*/
|
||||
|
||||
|
||||
@ -41,6 +41,20 @@ class Zend_Config_Ini extends Zend_Config
|
||||
*/
|
||||
protected $_nestSeparator = '.';
|
||||
|
||||
/**
|
||||
* String that separates the parent section name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_sectionSeparator = ':';
|
||||
|
||||
/**
|
||||
* Wether to skip extends or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_skipExtends = false;
|
||||
|
||||
/**
|
||||
* Loads the section $section from the config file $filename for
|
||||
* access facilitated by nested object properties.
|
||||
@ -103,10 +117,63 @@ class Zend_Config_Ini extends Zend_Config
|
||||
if (isset($options['nestSeparator'])) {
|
||||
$this->_nestSeparator = (string) $options['nestSeparator'];
|
||||
}
|
||||
if (isset($options['skipExtends'])) {
|
||||
$this->_skipExtends = (bool) $options['skipExtends'];
|
||||
}
|
||||
}
|
||||
|
||||
$iniArray = $this->_loadIniFile($filename);
|
||||
|
||||
if (null === $section) {
|
||||
// Load entire file
|
||||
$dataArray = array();
|
||||
foreach ($iniArray as $sectionName => $sectionData) {
|
||||
if(!is_array($sectionData)) {
|
||||
$dataArray = array_merge_recursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
|
||||
} else {
|
||||
$dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
|
||||
}
|
||||
}
|
||||
parent::__construct($dataArray, $allowModifications);
|
||||
} else {
|
||||
// Load one or more sections
|
||||
if (!is_array($section)) {
|
||||
$section = array($section);
|
||||
}
|
||||
$dataArray = array();
|
||||
foreach ($section as $sectionName) {
|
||||
if (!isset($iniArray[$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->_processSection($iniArray, $sectionName), $dataArray);
|
||||
|
||||
}
|
||||
parent::__construct($dataArray, $allowModifications);
|
||||
}
|
||||
|
||||
$this->_loadedSection = $section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the ini file and preprocess the section separator (':' in the
|
||||
* section name (that is used for section extension) so that the resultant
|
||||
* array has the correct section names and the extension information is
|
||||
* stored in a sub-key called ';extends'. We use ';extends' as this can
|
||||
* never be a valid key name in an INI file that has been loaded using
|
||||
* parse_ini_file().
|
||||
*
|
||||
* @param string $filename
|
||||
* @throws Zend_Config_Exception
|
||||
* @return array
|
||||
*/
|
||||
protected function _loadIniFile($filename)
|
||||
{
|
||||
set_error_handler(array($this, '_loadFileErrorHandler'));
|
||||
$iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed
|
||||
$loaded = 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) {
|
||||
@ -116,20 +183,20 @@ class Zend_Config_Ini extends Zend_Config
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception($this->_loadFileErrorStr);
|
||||
}
|
||||
|
||||
$preProcessedArray = array();
|
||||
foreach ($iniArray as $key => $data)
|
||||
|
||||
$iniArray = array();
|
||||
foreach ($loaded as $key => $data)
|
||||
{
|
||||
$bits = explode(':', $key);
|
||||
$thisSection = trim($bits[0]);
|
||||
switch (count($bits)) {
|
||||
$pieces = explode($this->_sectionSeparator, $key);
|
||||
$thisSection = trim($pieces[0]);
|
||||
switch (count($pieces)) {
|
||||
case 1:
|
||||
$preProcessedArray[$thisSection] = $data;
|
||||
$iniArray[$thisSection] = $data;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$extendedSection = trim($bits[1]);
|
||||
$preProcessedArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
|
||||
$extendedSection = trim($pieces[1]);
|
||||
$iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -141,48 +208,13 @@ class Zend_Config_Ini extends Zend_Config
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return $iniArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Process each element in the section and handle the ";extends" inheritance
|
||||
* key. Passes control to _processKey() to handle the nest separator
|
||||
* sub-property syntax that may be used within the key name.
|
||||
*
|
||||
* @param array $iniArray
|
||||
* @param string $section
|
||||
@ -190,7 +222,7 @@ class Zend_Config_Ini extends Zend_Config
|
||||
* @throws Zend_Config_Exception
|
||||
* @return array
|
||||
*/
|
||||
protected function _processExtends($iniArray, $section, $config = array())
|
||||
protected function _processSection($iniArray, $section, $config = array())
|
||||
{
|
||||
$thisSection = $iniArray[$section];
|
||||
|
||||
@ -198,13 +230,16 @@ class Zend_Config_Ini extends Zend_Config
|
||||
if (strtolower($key) == ';extends') {
|
||||
if (isset($iniArray[$value])) {
|
||||
$this->_assertValidExtend($section, $value);
|
||||
$config = $this->_processExtends($iniArray, $value, $config);
|
||||
|
||||
if (!$this->_skipExtends) {
|
||||
$config = $this->_processSection($iniArray, $value, $config);
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @see Zend_Config_Exception
|
||||
*/
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception("Section '$section' cannot be found");
|
||||
throw new Zend_Config_Exception("Parent section '$section' cannot be found");
|
||||
}
|
||||
} else {
|
||||
$config = $this->_processKey($config, $key, $value);
|
||||
@ -214,9 +249,8 @@ class Zend_Config_Ini extends Zend_Config
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign the key's value to the property list. Handle the "dot"
|
||||
* notation for sub-properties by passing control to
|
||||
* processLevelsInKey().
|
||||
* Assign the key's value to the property list. Handles the
|
||||
* nest separator for sub-properties.
|
||||
*
|
||||
* @param array $config
|
||||
* @param string $key
|
||||
@ -230,7 +264,12 @@ class Zend_Config_Ini extends Zend_Config
|
||||
$pieces = explode($this->_nestSeparator, $key, 2);
|
||||
if (strlen($pieces[0]) && strlen($pieces[1])) {
|
||||
if (!isset($config[$pieces[0]])) {
|
||||
$config[$pieces[0]] = array();
|
||||
if ($pieces[0] === '0' && !empty($config)) {
|
||||
// convert the current values in $config into an array
|
||||
$config = array($pieces[0] => $config);
|
||||
} else {
|
||||
$config[$pieces[0]] = array();
|
||||
}
|
||||
} elseif (!is_array($config[$pieces[0]])) {
|
||||
/**
|
||||
* @see Zend_Config_Exception
|
||||
|
@ -16,7 +16,7 @@
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Array.php 12221 2008-10-31 20:32:43Z dasprid $
|
||||
* @version $Id: Array.php 13708 2009-01-20 12:21:41Z dasprid $
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -39,6 +39,13 @@ class Zend_Config_Writer_Array extends Zend_Config_Writer
|
||||
*/
|
||||
protected $_filename = null;
|
||||
|
||||
/**
|
||||
* Wether to exclusively lock the file or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_exclusiveLock = false;
|
||||
|
||||
/**
|
||||
* Set the target filename
|
||||
*
|
||||
@ -52,16 +59,30 @@ class Zend_Config_Writer_Array extends Zend_Config_Writer
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set wether to exclusively lock the file or not
|
||||
*
|
||||
* @param boolean $exclusiveLock
|
||||
* @return Zend_Config_Writer_Array
|
||||
*/
|
||||
public function setExclusiveLock($exclusiveLock)
|
||||
{
|
||||
$this->_exclusiveLock = $exclusiveLock;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Config_Writer
|
||||
*
|
||||
* @param string $filename
|
||||
* @param Zend_Config $config
|
||||
* @param boolean $exclusiveLock
|
||||
* @throws Zend_Config_Exception When filename was not set
|
||||
* @throws Zend_Config_Exception When filename is not writable
|
||||
* @return void
|
||||
*/
|
||||
public function write($filename = null, Zend_Config $config = null)
|
||||
public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
|
||||
{
|
||||
if ($filename !== null) {
|
||||
$this->setFilename($filename);
|
||||
@ -71,6 +92,10 @@ class Zend_Config_Writer_Array extends Zend_Config_Writer
|
||||
$this->setConfig($config);
|
||||
}
|
||||
|
||||
if ($exclusiveLock !== null) {
|
||||
$this->setExclusiveLock($exclusiveLock);
|
||||
}
|
||||
|
||||
if ($this->_filename === null) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('No filename was set');
|
||||
@ -91,7 +116,13 @@ class Zend_Config_Writer_Array extends Zend_Config_Writer
|
||||
$arrayString = "<?php\n"
|
||||
. "return " . var_export($data, true) . ";\n";
|
||||
|
||||
$result = @file_put_contents($this->_filename, $arrayString);
|
||||
$flags = 0;
|
||||
|
||||
if ($this->_exclusiveLock) {
|
||||
$flags |= LOCK_EX;
|
||||
}
|
||||
|
||||
$result = @file_put_contents($this->_filename, $arrayString, $flags);
|
||||
|
||||
if ($result === false) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
|
@ -16,7 +16,7 @@
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Ini.php 12221 2008-10-31 20:32:43Z dasprid $
|
||||
* @version $Id: Ini.php 14175 2009-02-26 22:14:58Z dasprid $
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -39,6 +39,13 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
|
||||
*/
|
||||
protected $_filename = null;
|
||||
|
||||
/**
|
||||
* Wether to exclusively lock the file or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_exclusiveLock = false;
|
||||
|
||||
/**
|
||||
* String that separates nesting levels of configuration data identifiers
|
||||
*
|
||||
@ -59,6 +66,19 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set wether to exclusively lock the file or not
|
||||
*
|
||||
* @param boolean $exclusiveLock
|
||||
* @return Zend_Config_Writer_Array
|
||||
*/
|
||||
public function setExclusiveLock($exclusiveLock)
|
||||
{
|
||||
$this->_exclusiveLock = $exclusiveLock;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the nest separator
|
||||
*
|
||||
@ -77,11 +97,12 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
|
||||
*
|
||||
* @param string $filename
|
||||
* @param Zend_Config $config
|
||||
* @param boolean $exclusiveLock
|
||||
* @throws Zend_Config_Exception When filename was not set
|
||||
* @throws Zend_Config_Exception When filename is not writable
|
||||
* @return void
|
||||
*/
|
||||
public function write($filename = null, Zend_Config $config = null)
|
||||
public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
|
||||
{
|
||||
if ($filename !== null) {
|
||||
$this->setFilename($filename);
|
||||
@ -91,6 +112,10 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
|
||||
$this->setConfig($config);
|
||||
}
|
||||
|
||||
if ($exclusiveLock !== null) {
|
||||
$this->setExclusiveLock($exclusiveLock);
|
||||
}
|
||||
|
||||
if ($this->_filename === null) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('No filename was set');
|
||||
@ -109,19 +134,32 @@ class Zend_Config_Writer_Ini extends Zend_Config_Writer
|
||||
$iniString .= '[' . $sectionName . ']' . "\n"
|
||||
. $this->_addBranch($this->_config)
|
||||
. "\n";
|
||||
} else {
|
||||
} else {
|
||||
foreach ($this->_config as $sectionName => $data) {
|
||||
if (isset($extends[$sectionName])) {
|
||||
$sectionName .= ' : ' . $extends[$sectionName];
|
||||
if (!($data instanceof Zend_Config)) {
|
||||
$iniString .= $sectionName
|
||||
. ' = '
|
||||
. $this->_prepareValue($data)
|
||||
. "\n";
|
||||
} else {
|
||||
if (isset($extends[$sectionName])) {
|
||||
$sectionName .= ' : ' . $extends[$sectionName];
|
||||
}
|
||||
|
||||
$iniString .= '[' . $sectionName . ']' . "\n"
|
||||
. $this->_addBranch($data)
|
||||
. "\n";
|
||||
}
|
||||
|
||||
$iniString .= '[' . $sectionName . ']' . "\n"
|
||||
. $this->_addBranch($data)
|
||||
. "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$result = @file_put_contents($this->_filename, $iniString);
|
||||
$flags = 0;
|
||||
|
||||
if ($this->_exclusiveLock) {
|
||||
$flags |= LOCK_EX;
|
||||
}
|
||||
|
||||
$result = @file_put_contents($this->_filename, $iniString, $flags);
|
||||
|
||||
if ($result === false) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
|
@ -16,7 +16,7 @@
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Xml.php 12221 2008-10-31 20:32:43Z dasprid $
|
||||
* @version $Id: Xml.php 14267 2009-03-10 15:49:03Z dasprid $
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -39,6 +39,13 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
*/
|
||||
protected $_filename = null;
|
||||
|
||||
/**
|
||||
* Wether to exclusively lock the file or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_exclusiveLock = false;
|
||||
|
||||
/**
|
||||
* Set the target filename
|
||||
*
|
||||
@ -52,16 +59,30 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set wether to exclusively lock the file or not
|
||||
*
|
||||
* @param boolean $exclusiveLock
|
||||
* @return Zend_Config_Writer_Array
|
||||
*/
|
||||
public function setExclusiveLock($exclusiveLock)
|
||||
{
|
||||
$this->_exclusiveLock = $exclusiveLock;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defined by Zend_Config_Writer
|
||||
*
|
||||
* @param string $filename
|
||||
* @param Zend_Config $config
|
||||
* @param boolean $exclusiveLock
|
||||
* @throws Zend_Config_Exception When filename was not set
|
||||
* @throws Zend_Config_Exception When filename is not writable
|
||||
* @return void
|
||||
*/
|
||||
public function write($filename = null, Zend_Config $config = null)
|
||||
public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
|
||||
{
|
||||
if ($filename !== null) {
|
||||
$this->setFilename($filename);
|
||||
@ -71,6 +92,10 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
$this->setConfig($config);
|
||||
}
|
||||
|
||||
if ($exclusiveLock !== null) {
|
||||
$this->setExclusiveLock($exclusiveLock);
|
||||
}
|
||||
|
||||
if ($this->_filename === null) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('No filename was set');
|
||||
@ -88,20 +113,20 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
if (is_string($sectionName)) {
|
||||
$child = $xml->addChild($sectionName);
|
||||
|
||||
$this->_addBranch($this->_config, $child);
|
||||
$this->_addBranch($this->_config, $child, $xml);
|
||||
} else {
|
||||
foreach ($this->_config as $sectionName => $data) {
|
||||
if (!($data instanceof Zend_Config)) {
|
||||
continue;
|
||||
$xml->addChild($sectionName, (string) $data);
|
||||
} else {
|
||||
$child = $xml->addChild($sectionName);
|
||||
|
||||
if (isset($extends[$sectionName])) {
|
||||
$child->addAttribute('extends', $extends[$sectionName]);
|
||||
}
|
||||
|
||||
$this->_addBranch($data, $child, $xml);
|
||||
}
|
||||
|
||||
$child = $xml->addChild($sectionName);
|
||||
|
||||
if (isset($extends[$sectionName])) {
|
||||
$child->addAttribute('extends', $extends[$sectionName]);
|
||||
}
|
||||
|
||||
$this->_addBranch($data, $child);
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,7 +135,13 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
|
||||
$xmlString = $dom->saveXML();
|
||||
|
||||
$result = @file_put_contents($this->_filename, $xmlString);
|
||||
$flags = 0;
|
||||
|
||||
if ($this->_exclusiveLock) {
|
||||
$flags |= LOCK_EX;
|
||||
}
|
||||
|
||||
$result = @file_put_contents($this->_filename, $xmlString, $flags);
|
||||
|
||||
if ($result === false) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
@ -123,17 +154,45 @@ class Zend_Config_Writer_Xml extends Zend_Config_Writer
|
||||
*
|
||||
* @param Zend_Config $config
|
||||
* @param SimpleXMLElement $xml
|
||||
* @param SimpleXMLElement $parent
|
||||
* @return void
|
||||
*/
|
||||
protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml)
|
||||
protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
|
||||
{
|
||||
$branchType = null;
|
||||
|
||||
foreach ($config as $key => $value) {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$child = $xml->addChild($key);
|
||||
|
||||
$this->_addBranch($value, $child);
|
||||
} else {
|
||||
$xml->addChild($key, (string) $value);
|
||||
if ($branchType === null) {
|
||||
if (is_numeric($key)) {
|
||||
$branchType = 'numeric';
|
||||
$branchName = $xml->getName();
|
||||
$xml = $parent;
|
||||
|
||||
unset($parent->{$branchName});
|
||||
} else {
|
||||
$branchType = 'string';
|
||||
}
|
||||
} else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
|
||||
}
|
||||
|
||||
if ($branchType === 'numeric') {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$child = $parent->addChild($branchName, (string) $value);
|
||||
|
||||
$this->_addBranch($value, $child, $parent);
|
||||
} else {
|
||||
$parent->addChild($branchName, (string) $value);
|
||||
}
|
||||
} else {
|
||||
if ($value instanceof Zend_Config) {
|
||||
$child = $xml->addChild($key);
|
||||
|
||||
$this->_addBranch($value, $child, $xml);
|
||||
} else {
|
||||
$xml->addChild($key, (string) $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
* @package Zend_Config
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Xml.php 11427 2008-09-18 16:11:43Z doctorrock83 $
|
||||
* @version $Id: Xml.php 14665 2009-04-05 08:46:51Z rob $
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -35,7 +35,14 @@ require_once 'Zend/Config.php';
|
||||
class Zend_Config_Xml extends Zend_Config
|
||||
{
|
||||
/**
|
||||
* Loads the section $section from the config file $filename for
|
||||
* Wether to skip extends or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_skipExtends = false;
|
||||
|
||||
/**
|
||||
* Loads the section $section from the config file (or string $xml for
|
||||
* access facilitated by nested object properties.
|
||||
*
|
||||
* Sections are defined in the XML as children of the root element.
|
||||
@ -47,21 +54,38 @@ class Zend_Config_Xml extends Zend_Config
|
||||
* Note that the keys in $section will override any keys of the same
|
||||
* name in the sections that have been included via "extends".
|
||||
*
|
||||
* @param string $filename File to process
|
||||
* @param string $xml XML file or string to process
|
||||
* @param mixed $section Section to process
|
||||
* @param boolean $allowModifications Wether modifiacations are allowed at runtime
|
||||
* @throws Zend_Config_Exception When filename is not set
|
||||
* @throws Zend_Config_Exception When section $sectionName cannot be found in $filename
|
||||
* @throws Zend_Config_Exception When xml is not set or cannot be loaded
|
||||
* @throws Zend_Config_Exception When section $sectionName cannot be found in $xml
|
||||
*/
|
||||
public function __construct($filename, $section = null, $allowModifications = false)
|
||||
public function __construct($xml, $section = null, $options = false)
|
||||
{
|
||||
if (empty($filename)) {
|
||||
if (empty($xml)) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception('Filename is not set');
|
||||
}
|
||||
|
||||
set_error_handler(array($this, '_loadFileErrorHandler'));
|
||||
$config = simplexml_load_file($filename); // Warnings and errors are suppressed
|
||||
$allowModifications = false;
|
||||
if (is_bool($options)) {
|
||||
$allowModifications = $options;
|
||||
} elseif (is_array($options)) {
|
||||
if (isset($options['allowModifications'])) {
|
||||
$allowModifications = (bool) $options['allowModifications'];
|
||||
}
|
||||
if (isset($options['skipExtends'])) {
|
||||
$this->_skipExtends = (bool) $options['skipExtends'];
|
||||
}
|
||||
}
|
||||
|
||||
set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
|
||||
if (strstr($xml, '<?xml')) {
|
||||
$config = simplexml_load_string($xml);
|
||||
} else {
|
||||
$config = simplexml_load_file($xml);
|
||||
}
|
||||
|
||||
restore_error_handler();
|
||||
// Check if there was a error while loading file
|
||||
if ($this->_loadFileErrorStr !== null) {
|
||||
@ -81,7 +105,7 @@ class Zend_Config_Xml extends Zend_Config
|
||||
foreach ($section as $sectionName) {
|
||||
if (!isset($config->$sectionName)) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
|
||||
throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $xml");
|
||||
}
|
||||
|
||||
$dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
|
||||
@ -91,7 +115,7 @@ class Zend_Config_Xml extends Zend_Config
|
||||
} else {
|
||||
if (!isset($config->$section)) {
|
||||
require_once 'Zend/Config/Exception.php';
|
||||
throw new Zend_Config_Exception("Section '$section' cannot be found in $filename");
|
||||
throw new Zend_Config_Exception("Section '$section' cannot be found in $xml");
|
||||
}
|
||||
|
||||
$dataArray = $this->_processExtends($config, $section);
|
||||
@ -128,7 +152,10 @@ class Zend_Config_Xml extends Zend_Config
|
||||
if (isset($thisSection['extends'])) {
|
||||
$extendedSection = (string) $thisSection['extends'];
|
||||
$this->_assertValidExtend($section, $extendedSection);
|
||||
$config = $this->_processExtends($element, $extendedSection, $config);
|
||||
|
||||
if (!$this->_skipExtends) {
|
||||
$config = $this->_processExtends($element, $extendedSection, $config);
|
||||
}
|
||||
}
|
||||
|
||||
$config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection));
|
||||
@ -205,7 +232,7 @@ class Zend_Config_Xml extends Zend_Config
|
||||
|
||||
/**
|
||||
* Merge two arrays recursively, overwriting keys of the same name
|
||||
* in $array1 with the value in $array2.
|
||||
* in $firstArray with the value in $secondArray.
|
||||
*
|
||||
* @param mixed $firstArray First array
|
||||
* @param mixed $secondArray Second array to merge into first array
|
||||
@ -218,7 +245,11 @@ class Zend_Config_Xml extends Zend_Config
|
||||
if (isset($firstArray[$key])) {
|
||||
$firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
|
||||
} else {
|
||||
$firstArray[$key] = $value;
|
||||
if($key === 0) {
|
||||
$firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
|
||||
} else {
|
||||
$firstArray[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user