diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index 12b59e5cbebc..9521097fe0b0 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -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'); @@ -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); } @@ -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) { @@ -421,6 +427,7 @@ private function getXmlErrors() } libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 1203067d23ca..d7f1677b9f8c 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -150,14 +150,18 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $definiti */ protected function loadFile($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); return $dom; @@ -175,8 +179,10 @@ protected function validate(\DOMDocument $dom) $location = __DIR__.'/schema/routing/routing-1.0.xsd'; $current = libxml_use_internal_errors(true); + libxml_clear_errors(); + if (!$dom->schemaValidate($location)) { - throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); + throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($current))); } libxml_use_internal_errors($current); } @@ -186,7 +192,7 @@ protected function validate(\DOMDocument $dom) * * @return array An array of libxml error strings */ - private function getXmlErrors() + private function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -201,6 +207,7 @@ private function getXmlErrors() } libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index 4bd6b69a4adf..86885eaf5a94 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -55,10 +55,13 @@ public function load($resource, $locale, $domain = 'messages') */ private function parseFile($file) { + $internalErrors = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument(); - $current = libxml_use_internal_errors(true); + $dom->validateOnParse = true; if (!@$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors())); + throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); } $location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd'; @@ -77,11 +80,11 @@ private function parseFile($file) $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source); if (!@$dom->schemaValidateSource($source)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors())); + throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); } - $dom->validateOnParse = true; $dom->normalizeDocument(); - libxml_use_internal_errors($current); + + libxml_use_internal_errors($internalErrors); return simplexml_import_dom($dom); } @@ -91,7 +94,7 @@ private function parseFile($file) * * @return array An array of errors */ - private function getXmlErrors() + private function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -106,7 +109,7 @@ private function getXmlErrors() } libxml_clear_errors(); - libxml_use_internal_errors(false); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php index d95d97537774..d85e857681a2 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php @@ -180,22 +180,25 @@ protected function parseOptions(\SimpleXMLElement $nodes) */ protected 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 MappingException(implode("\n", $this->getXmlErrors())); + throw new MappingException(implode("\n", $this->getXmlErrors($internalErrors))); } if (!$dom->schemaValidate(__DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd')) { - throw new MappingException(implode("\n", $this->getXmlErrors())); + throw new MappingException(implode("\n", $this->getXmlErrors($internalErrors))); } - $dom->validateOnParse = true; $dom->normalizeDocument(); - libxml_use_internal_errors(false); + + libxml_use_internal_errors($internalErrors); return simplexml_import_dom($dom); } - protected function getXmlErrors() + protected function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -210,7 +213,7 @@ protected function getXmlErrors() } libxml_clear_errors(); - libxml_use_internal_errors(false); + libxml_use_internal_errors($internalErrors); return $errors; }