Skip to content

Commit

Permalink
standardized the way we handle XML errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 28, 2012
1 parent e24f270 commit 4e902ab
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Loader/XmlFileLoader.php
Expand Up @@ -211,14 +211,18 @@ private function parseDefinition($id, $service, $file)
*/
private function parseFile($file)
{
$internalErrors = libxml_use_internal_errors(true);
libxml_clear_errors();

$dom = new \DOMDocument();
libxml_use_internal_errors(true);
$dom->validateOnParse = true;
if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($internalErrors)));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors(false);

libxml_use_internal_errors($internalErrors);

$this->validate($dom, $file);

return simplexml_import_dom($dom, 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement');
Expand Down Expand Up @@ -360,12 +364,14 @@ private function validateSchema(\DOMDocument $dom, $file)
;

$current = libxml_use_internal_errors(true);
libxml_clear_errors();

$valid = $dom->schemaValidateSource($source);
foreach ($tmpfiles as $tmpfile) {
@unlink($tmpfile);
}
if (!$valid) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($current)));
}
libxml_use_internal_errors($current);
}
Expand Down Expand Up @@ -406,7 +412,7 @@ private function validateExtensions(\DOMDocument $dom, $file)
*
* @return array
*/
private function getXmlErrors()
private function getXmlErrors($internalErrors)
{
$errors = array();
foreach (libxml_get_errors() as $error) {
Expand All @@ -421,6 +427,7 @@ private function getXmlErrors()
}

libxml_clear_errors();
libxml_use_internal_errors($internalErrors);

return $errors;
}
Expand Down

0 comments on commit 4e902ab

Please sign in to comment.