浏览代码

standardized the way we handle XML errors

Fabien Potencier 12 年之前
父节点
当前提交
865461d204

+ 13 - 6
src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

@@ -211,14 +211,18 @@ class XmlFileLoader extends FileLoader
      */
     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 @@ EOF
         ;
 
         $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 @@ EOF
      *
      * @return array
      */
-    private function getXmlErrors()
+    private function getXmlErrors($internalErrors)
     {
         $errors = array();
         foreach (libxml_get_errors() as $error) {
@@ -421,6 +427,7 @@ EOF
         }
 
         libxml_clear_errors();
+        libxml_use_internal_errors($internalErrors);
 
         return $errors;
     }

+ 13 - 6
src/Symfony/Component/Routing/Loader/XmlFileLoader.php

@@ -150,14 +150,18 @@ class XmlFileLoader extends FileLoader
      */
     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 @@ class XmlFileLoader extends FileLoader
         $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 @@ class XmlFileLoader extends FileLoader
      *
      * @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 @@ class XmlFileLoader extends FileLoader
         }
 
         libxml_clear_errors();
+        libxml_use_internal_errors($internalErrors);
 
         return $errors;
     }

+ 10 - 7
src/Symfony/Component/Translation/Loader/XliffFileLoader.php

@@ -55,10 +55,13 @@ class XliffFileLoader implements LoaderInterface
      */
     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 @@ class XliffFileLoader implements LoaderInterface
         $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 @@ class XliffFileLoader implements LoaderInterface
      *
      * @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 @@ class XliffFileLoader implements LoaderInterface
         }
 
         libxml_clear_errors();
-        libxml_use_internal_errors(false);
+        libxml_use_internal_errors($internalErrors);
 
         return $errors;
     }

+ 10 - 7
src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

@@ -180,22 +180,25 @@ class XmlFileLoader extends FileLoader
      */
     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 @@ class XmlFileLoader extends FileLoader
         }
 
         libxml_clear_errors();
-        libxml_use_internal_errors(false);
+        libxml_use_internal_errors($internalErrors);
 
         return $errors;
     }