Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -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
{
}

View File

@@ -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
{
}

View File

@@ -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.
*
@@ -18,6 +21,7 @@ namespace Symfony\Component\Config\Util;
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Martin Hasoň <martin.hason@gmail.com>
* @author Ole Rößner <ole@roessner.it>
*/
class XmlUtils
{
@@ -29,27 +33,23 @@ class XmlUtils
}
/**
* Loads an XML file.
* Parses an XML string.
*
* @param string $file An XML file path
* @param string $content An XML string
* @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
*
* @return \DOMDocument
*
* @throws \InvalidArgumentException When loading of XML file returns error
* @throws \RuntimeException When DOM extension is missing
* @throws XmlParsingException When parsing of XML file returns error
* @throws 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 loadFile($file, $schemaOrCallable = null)
public static function parse($content, $schemaOrCallable = null)
{
if (!\extension_loaded('dom')) {
throw new \RuntimeException('Extension DOM is required.');
}
$content = @file_get_contents($file);
if ('' === trim($content)) {
throw new \InvalidArgumentException(sprintf('File %s does not contain valid XML, it is empty.', $file));
}
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
@@ -59,7 +59,7 @@ class XmlUtils
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();
@@ -69,7 +69,7 @@ class XmlUtils
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.');
}
}
@@ -90,15 +90,15 @@ class XmlUtils
} 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(sprintf('The XML file "%s" is not valid.', $file));
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);
}
}
@@ -108,6 +108,32 @@ class XmlUtils
return $dom;
}
/**
* Loads an XML file.
*
* @param string $file An XML file path
* @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
*
* @return \DOMDocument
*
* @throws \InvalidArgumentException When loading of XML file returns error
* @throws XmlParsingException When XML parsing returns any errors
* @throws \RuntimeException When DOM extension is missing
*/
public static function loadFile($file, $schemaOrCallable = null)
{
$content = @file_get_contents($file);
if ('' === trim($content)) {
throw new \InvalidArgumentException(sprintf('File %s does not contain valid XML, it is empty.', $file));
}
try {
return static::parse($content, $schemaOrCallable);
} catch (InvalidXmlException $e) {
throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious());
}
}
/**
* Converts a \DOMElement object to a PHP array.
*
@@ -126,15 +152,15 @@ class XmlUtils
* @param \DOMElement $element A \DOMElement instance
* @param bool $checkPrefix Check prefix in an element or an attribute name
*
* @return array A PHP array
* @return mixed
*/
public static function convertDomElementToArray(\DOMElement $element, $checkPrefix = true)
{
$prefix = (string) $element->prefix;
$empty = true;
$config = array();
$config = [];
foreach ($element->attributes as $name => $node) {
if ($checkPrefix && !\in_array((string) $node->prefix, array('', $prefix), true)) {
if ($checkPrefix && !\in_array((string) $node->prefix, ['', $prefix], true)) {
continue;
}
$config[$name] = static::phpize($node->value);
@@ -156,7 +182,7 @@ class XmlUtils
$key = $node->localName;
if (isset($config[$key])) {
if (!\is_array($config[$key]) || !\is_int(key($config[$key]))) {
$config[$key] = array($config[$key]);
$config[$key] = [$config[$key]];
}
$config[$key][] = $value;
} else {
@@ -193,7 +219,7 @@ class XmlUtils
switch (true) {
case 'null' === $lowercaseValue:
return;
return null;
case ctype_digit($value):
$raw = $value;
$cast = (int) $value;
@@ -208,13 +234,13 @@ class XmlUtils
return true;
case 'false' === $lowercaseValue:
return false;
case isset($value[1]) && '0b' == $value[0].$value[1]:
case isset($value[1]) && '0b' == $value[0].$value[1] && preg_match('/^0b[01]*$/', $value):
return bindec($value);
case is_numeric($value):
return '0x' === $value[0].$value[1] ? hexdec($value) : (float) $value;
case preg_match('/^0x[0-9a-f]++$/i', $value):
return hexdec($value);
case preg_match('/^(-|\+)?[0-9]+(\.[0-9]+)?$/', $value):
case preg_match('/^[+-]?[0-9]+(\.[0-9]+)?$/', $value):
return (float) $value;
default:
return $value;
@@ -223,7 +249,7 @@ class XmlUtils
protected static function getXmlErrors($internalErrors)
{
$errors = array();
$errors = [];
foreach (libxml_get_errors() as $error) {
$errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',