4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Util;
15 * XMLUtils is a bunch of utility methods to XML operations.
17 * This class contains static methods only and is not meant to be instantiated.
19 * @author Fabien Potencier <fabien@symfony.com>
20 * @author Martin HasoĊ <martin.hason@gmail.com>
25 * This class should not be instantiated.
27 private function __construct()
34 * @param string $file An XML file path
35 * @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
37 * @return \DOMDocument
39 * @throws \InvalidArgumentException When loading of XML file returns error
41 public static function loadFile($file, $schemaOrCallable = null)
43 $content = @file_get_contents($file);
44 if ('' === trim($content)) {
45 throw new \InvalidArgumentException(sprintf('File %s does not contain valid XML, it is empty.', $file));
48 $internalErrors = libxml_use_internal_errors(true);
49 $disableEntities = libxml_disable_entity_loader(true);
50 libxml_clear_errors();
52 $dom = new \DOMDocument();
53 $dom->validateOnParse = true;
54 if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
55 libxml_disable_entity_loader($disableEntities);
57 throw new \InvalidArgumentException(implode("\n", static::getXmlErrors($internalErrors)));
60 $dom->normalizeDocument();
62 libxml_use_internal_errors($internalErrors);
63 libxml_disable_entity_loader($disableEntities);
65 foreach ($dom->childNodes as $child) {
66 if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
67 throw new \InvalidArgumentException('Document types are not allowed.');
71 if (null !== $schemaOrCallable) {
72 $internalErrors = libxml_use_internal_errors(true);
73 libxml_clear_errors();
76 if (is_callable($schemaOrCallable)) {
78 $valid = call_user_func($schemaOrCallable, $dom, $internalErrors);
79 } catch (\Exception $e) {
82 } elseif (!is_array($schemaOrCallable) && is_file((string) $schemaOrCallable)) {
83 $schemaSource = file_get_contents((string) $schemaOrCallable);
84 $valid = @$dom->schemaValidateSource($schemaSource);
86 libxml_use_internal_errors($internalErrors);
88 throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
92 $messages = static::getXmlErrors($internalErrors);
93 if (empty($messages)) {
94 $messages = array(sprintf('The XML file "%s" is not valid.', $file));
96 throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
100 libxml_clear_errors();
101 libxml_use_internal_errors($internalErrors);
107 * Converts a \DomElement object to a PHP array.
109 * The following rules applies during the conversion:
111 * * Each tag is converted to a key value or an array
112 * if there is more than one "value"
114 * * The content of a tag is set under a "value" key (<foo>bar</foo>)
115 * if the tag also has some nested tags
117 * * The attributes are converted to keys (<foo foo="bar"/>)
119 * * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
121 * @param \DomElement $element A \DomElement instance
122 * @param bool $checkPrefix Check prefix in an element or an attribute name
124 * @return array A PHP array
126 public static function convertDomElementToArray(\DOMElement $element, $checkPrefix = true)
128 $prefix = (string) $element->prefix;
131 foreach ($element->attributes as $name => $node) {
132 if ($checkPrefix && !in_array((string) $node->prefix, array('', $prefix), true)) {
135 $config[$name] = static::phpize($node->value);
140 foreach ($element->childNodes as $node) {
141 if ($node instanceof \DOMText) {
142 if ('' !== trim($node->nodeValue)) {
143 $nodeValue = trim($node->nodeValue);
146 } elseif ($checkPrefix && $prefix != (string) $node->prefix) {
148 } elseif (!$node instanceof \DOMComment) {
149 $value = static::convertDomElementToArray($node, $checkPrefix);
151 $key = $node->localName;
152 if (isset($config[$key])) {
153 if (!is_array($config[$key]) || !is_int(key($config[$key]))) {
154 $config[$key] = array($config[$key]);
156 $config[$key][] = $value;
158 $config[$key] = $value;
165 if (false !== $nodeValue) {
166 $value = static::phpize($nodeValue);
167 if (count($config)) {
168 $config['value'] = $value;
174 return !$empty ? $config : null;
178 * Converts an xml value to a PHP type.
180 * @param mixed $value
184 public static function phpize($value)
186 $value = (string) $value;
187 $lowercaseValue = strtolower($value);
190 case 'null' === $lowercaseValue:
192 case ctype_digit($value):
194 $cast = (int) $value;
196 return '0' == $value[0] ? octdec($value) : (((string) $raw === (string) $cast) ? $cast : $raw);
197 case isset($value[1]) && '-' === $value[0] && ctype_digit(substr($value, 1)):
199 $cast = (int) $value;
201 return '0' == $value[1] ? octdec($value) : (((string) $raw === (string) $cast) ? $cast : $raw);
202 case 'true' === $lowercaseValue:
204 case 'false' === $lowercaseValue:
206 case isset($value[1]) && '0b' == $value[0].$value[1]:
207 return bindec($value);
208 case is_numeric($value):
209 return '0x' === $value[0].$value[1] ? hexdec($value) : (float) $value;
210 case preg_match('/^0x[0-9a-f]++$/i', $value):
211 return hexdec($value);
212 case preg_match('/^(-|\+)?[0-9]+(\.[0-9]+)?$/', $value):
213 return (float) $value;
219 protected static function getXmlErrors($internalErrors)
222 foreach (libxml_get_errors() as $error) {
223 $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
224 LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
226 trim($error->message),
227 $error->file ?: 'n/a',
233 libxml_clear_errors();
234 libxml_use_internal_errors($internalErrors);