Skip to content

Commit

Permalink
Merge branch 'ZF2-13' of https://github.com/marc-mabe/zf2 into hotfix…
Browse files Browse the repository at this point in the history
…/zf2-13
  • Loading branch information
weierophinney committed Jul 26, 2011
2 parents 3dee5cb + b9441a2 commit 71a4519
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 124 deletions.
64 changes: 41 additions & 23 deletions library/Zend/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ class Config implements \Countable, \Iterator
protected $_extends = array();

/**
* Load file error string.
* Internal error messages
*
* Is null if there was no error while file loading
*
* @var string
* @var null|array
*/
protected $_loadFileErrorStr = null;
protected $_errorMessages = array();

/**
* Zend_Config provides a property based interface to
Expand Down Expand Up @@ -329,7 +327,6 @@ public function areAllSectionsLoaded()
return $this->_loadedSection === null;
}


/**
* Merge another Zend_Config with this one. The items
* in $merge will override the same named items in
Expand Down Expand Up @@ -434,23 +431,6 @@ protected function _assertValidExtend($extendingSection, $extendedSection)
$this->_extends[$extendingSection] = $extendedSection;
}

/**
* Handle any errors from simplexml_load_file or parse_ini_file
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
*/
protected function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($this->_loadFileErrorStr === null) {
$this->_loadFileErrorStr = $errstr;
} else {
$this->_loadFileErrorStr .= (PHP_EOL . $errstr);
}
}

/**
* Merge two arrays recursively, overwriting keys of the same name
* in $firstArray with the value in $secondArray.
Expand Down Expand Up @@ -479,4 +459,42 @@ protected function _arrayMergeRecursive($firstArray, $secondArray)

return $firstArray;
}

/**
* Set internal error handler
*
* @return void
*/
protected function _setErrorHandler()
{
set_error_handler(array($this, '_handleError'));
}

/**
* Restore internal error handler
*
* @return array Handled error messages
*/
protected function _restoreErrorHandler()
{
restore_error_handler();
$errorMessages = $this->_errorMessages;
$this->_errorMessages = array();
return $errorMessages;
}

/**
* Handle internal errors
*
* @param integer $errno
* @param string $errstr
* @param string $errfile
* @param integer $errline
* @return void
*/
protected function _handleError($errno, $errstr, $errfile, $errline)
{
$this->_errorMessages[] = trim($errstr);
}

}
35 changes: 12 additions & 23 deletions library/Zend/Config/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,6 @@ public function __construct($filename, $section = null, $options = false)

$this->_loadedSection = $section;
}

/**
* Load the INI file from disk using parse_ini_file(). Use a private error
* handler to convert any loading errors into a Zend_Config_Exception
*
* @param string $filename
* @throws \Zend\Config\Exception
* @return array
*/
protected function _parseIniFile($filename)
{
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) {
throw new Exception\RuntimeException($this->_loadFileErrorStr);
}

return $iniArray;
}

/**
* Load the ini file and preprocess the section separator (':' in the
Expand All @@ -185,7 +163,18 @@ protected function _parseIniFile($filename)
*/
protected function _loadIniFile($filename)
{
$loaded = $this->_parseIniFile($filename);
$this->_setErrorHandler();
$loaded = parse_ini_file($filename, true);
$errorMessages = $this->_restoreErrorHandler();
if ($loaded === false) {
$e = null;
foreach ($errorMessages as $errMsg) {
$e = new Exception\RuntimeException($errMsg, 0, $e);
}
$e = new Exception\RuntimeException("Can't parse ini file '{$filename}'", 0, $e);
throw $e;
}

$iniArray = array();
foreach ($loaded as $key => $data)
{
Expand Down
25 changes: 15 additions & 10 deletions library/Zend/Config/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ public function __construct($json, $section = null, $options = false)
}
}

set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
if ($json[0] != '{') {
$json = file_get_contents($json);
}
restore_error_handler();

// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
throw new Exception\RuntimeException($this->_loadFileErrorStr);
// read json file
$this->_setErrorHandler();
$content = file_get_contents($json, true);
$errorMessages = $this->_restoreErrorHandler();
if ($content === false) {
$e = null;
foreach ($errorMessages as $errMsg) {
$e = new Exception\RuntimeException($errMsg, 0, $e);
}
$e = new Exception\RuntimeException("Can't read file '{$json}'", 0, $e);
throw $e;
}
$json = $content;
}

// Replace constants
Expand Down Expand Up @@ -229,8 +234,8 @@ protected function _getConstants()

/**
* Flatten JSON object structure to associative array
*
* @param object|array $config
*
* @param object|array $config
* @return array
*/
protected function flattenObjects($config)
Expand Down
29 changes: 23 additions & 6 deletions library/Zend/Config/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,34 @@ public function __construct($xml, $section = null, $options = false)
}
}

set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
// load XML and throw exception of each failure using previous exception
$oldUseInternalErrors = libxml_use_internal_errors(true);
if ($oldUseInternalErrors) {
libxml_clear_errors();
}
if (strstr($xml, '<' . '?xml')) { // string concat to fix syntax highlighting
$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) {
throw new Exception\InvalidArgumentException($this->_loadFileErrorStr);
$xmlErrors = libxml_get_errors();
if (!$oldUseInternalErrors) {
libxml_use_internal_errors(false);
}
if ( ($xmlErrorCnt = count($xmlErrors)) ) {
libxml_clear_errors();

// create and throw exception stack
$e = null;
foreach ($xmlErrors as $xmlError) {
$msg = trim($xmlError->message);
$line = $xmlError->line;
$col = $xmlError->column;
$e = new Exception\RuntimeException(
$msg . ' @ line/column ' . $line . '/' . $col, 0, $e
);
}
throw $e;
}

if ($section === null) {
Expand Down
20 changes: 12 additions & 8 deletions library/Zend/Config/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,19 @@ public function __construct($yaml, $section = null, $options = false)
}
}

// Suppress warnings and errors while loading file
set_error_handler(array($this, '_loadFileErrorHandler'));
$yaml = file_get_contents($yaml);
restore_error_handler();

// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
throw new Exception\RuntimeException($this->_loadFileErrorStr);
// read yaml file
$this->_setErrorHandler();
$content = file_get_contents($yaml, true);
$errorMessages = $this->_restoreErrorHandler();
if ($content === false) {
$e = null;
foreach ($errorMessages as $errMsg) {
$e = new Exception\RuntimeException($errMsg, 0, $e);
}
$e = new Exception\RuntimeException("Can't read file '{$yaml}'", 0, $e);
throw $e;
}
$yaml = $content;

// Override static value for ignore_constants if provided in $options
self::setIgnoreConstants($ignoreConstants);
Expand Down
Loading

0 comments on commit 71a4519

Please sign in to comment.