8000 [Config] extracted the xml parsing from XmlUtils::loadFile into XmlUtils::parse by Basster · Pull Request #23485 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] extracted the xml parsing from XmlUtils::loadFile into XmlUtils::parse #23485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
Next Next commit
introduced dedicated exception classes related to XML parsing.
  • Loading branch information
Basster committed Sep 26, 2017
commit 9fcb50fa96e39f9fb04890c2b9d34ca45141a45f
4 changes: 2 additions & 2 deletions src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function testLoadFile()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \Symfony\Component\Config\Util\Exception\InvalidXmlException
* @expectedExceptionMessage The XML is not valid
*/
public function testParse()
public function testParseWithInvalidValidatorCallable()
{
$fixtures = __DIR__.'/../Fixtures/Util/';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Util\Exception;

/**
* Exception class for when XML parsing with an XSD schema file path or a callable validator produces errors unrelated
* to the actual XML parsing.
*
* @author Ole Rößner <ole@roessner.it>
*/
class InvalidXmlException extends XmlParsingException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Util\Exception;

/**
* Exception class for when XML cannot be parsed properly.
*
* @author Ole Rößner <ole@roessner.it>
*/
class XmlParsingException extends \InvalidArgumentException
{
}
24 changes: 13 additions & 11 deletions src/Symfony/Component/Config/Util/XmlUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Config\Util;

use Symfony\Component\Config\Util\Exception\InvalidXmlException;
use Symfony\Component\Config\Util\Exception\XmlParsingException;

/**
* XMLUtils is a bunch of utility methods to XML operations.
*
Expand All @@ -37,7 +40,8 @@ private function __construct()
*
* @return \DOMDocument
*
* @throws \InvalidArgumentException When loading of XML file returns error
* @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When parsing of XML file returns error
* @throws \Symfony\Component\Config\Util\Exception\InvalidXmlException When parsing of XML with schema or callable produces any errors unrelated to the XML parsing itself
* @throws \RuntimeException When DOM extension is missing
*/
public static function parse($content, $schemaOrCallable = null)
Expand All @@ -55,7 +59,7 @@ public static function parse($content, $schemaOrCallable = null)
if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);

throw new \InvalidArgumentException(implode("\n", static::getXmlErrors($internalErrors)));
throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
}

$dom->normalizeDocument();
Expand All @@ -65,7 +69,7 @@ public static function parse($content, $schemaOrCallable = null)

foreach ($dom->childNodes as $child) {
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
throw new \InvalidArgumentException('Document types are not allowed.');
throw new XmlParsingException('Document types are not allowed.');
}
}

Expand All @@ -86,15 +90,15 @@ public static function parse($content, $schemaOrCallable = null)
} else {
libxml_use_internal_errors($internalErrors);

throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
throw new XmlParsingException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
}

if (!$valid) {
$messages = static::getXmlErrors($internalErrors);
if (empty($messages)) {
$messages = array('The XML is not valid.');
throw new InvalidXmlException('The XML is not valid.', 0, $e);
}
throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
throw new XmlParsingException(implode("\n", $messages), 0, $e);
}
}

Expand All @@ -113,6 +117,7 @@ public static function parse($content, $schemaOrCallable = null)
* @return \DOMDocument
*
* @throws \InvalidArgumentException When loading of XML file returns error
* @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When XML parsing returns any errors.
* @throws \RuntimeException When DOM extension is missing
*/
public static function loadFile($file, $schemaOrCallable = null)
Expand All @@ -124,11 +129,8 @@ public static function loadFile($file, $schemaOrCallable = null)

try {
return static::parse($content, $schemaOrCallable);
} catch (\InvalidArgumentException $e) {
if ('The XML is not valid.' !== $e->getMessage()) {
throw $e;
}
throw new \InvalidArgumentException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious());
} catch (InvalidXmlException $e) {
throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious());
}
}

Expand Down
0