Skip to content

Instantly share code, notes, and snippets.

@ArnaudLigny
Created January 27, 2012 09:38
Show Gist options
  • Save ArnaudLigny/1687990 to your computer and use it in GitHub Desktop.
Save ArnaudLigny/1687990 to your computer and use it in GitHub Desktop.
Check the XML validity of Magento configuration files
<?php
/**
* Varien_Simplexml_Config overlap to Check the XML validity
* of Magento configuration files
*/
class Varien_Simplexml_Config {
[...]
/**
* Imports XML file
*
* @param string $filePath
* @return boolean
*/
public function loadFile($filePath)
{
if (!is_readable($filePath)) {
//throw new Exception('Can not read xml file '.$filePath);
return false;
}
$fileData = file_get_contents($filePath);
$fileData = $this->processFileData($fileData);
/**
* Custom code: $filePath added
*/
// original
//return $this->loadString($fileData, $this->_elementClass);
// new
return $this->loadString($fileData, $this->_elementClass, $filePath);
}
/**
* Imports XML string
*
* @param string $string
* @return boolean
*/
public function loadString($string)
{
if (is_string($string)) {
/**
* Custom code: dealing with config file XML errors
*/
libxml_use_internal_errors(true);
$xmlcheck = simplexml_load_string($string);
if (!is_object($xmlcheck)) {
$argslist = func_get_args();
$filePath = $argslist[2];
foreach(libxml_get_errors() as $error) {
$error = array(
'message' => trim($error->message),
'code' => 'Code: ' . $error->code,
'file' => 'File: ' . ($error->file != '' ? $error->file : $filePath),
'line' => 'Line: ' . $error->line,
'column' => 'Column: ' . $error->column,
);
}
libxml_clear_errors();
// log
Mage::log("\n" . implode("\n", $error), Zend_Log::ERR, 'exception_config.log');
// throw in developer mode
if (Mage::getIsDeveloperMode()) {
Mage::printException(new Exception(implode("\n", $error)), 'CONFIG FILE ERROR');
}
}
// /Custom code
$xml = simplexml_load_string($string, $this->_elementClass);
if ($xml instanceof Varien_Simplexml_Element) {
$this->_xml = $xml;
return true;
}
} else {
Mage::logException(new Exception('"$string" parameter for simplexml_load_string is not a string'));
}
return false;
}
[...]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment