From 7f5a7fd43239def28085ff85a470810018b7a4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 30 Oct 2012 12:08:35 +0100 Subject: [PATCH] [DependencyInjection] Using class from Config component to loading XML files --- .../Loader/XmlFileLoader.php | 129 ++---------------- .../DependencyInjection/SimpleXMLElement.php | 25 +--- 2 files changed, 15 insertions(+), 139 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index 6f2c8eba1a..f8f4336736 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Loader; use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Config\Util\XmlUtils; use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Alias; @@ -202,33 +203,17 @@ class XmlFileLoader extends FileLoader * * @return SimpleXMLElement * - * @throws \InvalidArgumentException When loading of XML file returns error + * @throws InvalidArgumentException When loading of XML file returns error */ protected function parseFile($file) { - $internalErrors = libxml_use_internal_errors(true); - $disableEntities = libxml_disable_entity_loader(true); - libxml_clear_errors(); - - $dom = new \DOMDocument(); - $dom->validateOnParse = true; - if (!$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { - libxml_disable_entity_loader($disableEntities); - - throw new InvalidArgumentException(implode("\n", $this->getXmlErrors($internalErrors))); - } - $dom->normalizeDocument(); - - libxml_use_internal_errors($internalErrors); - libxml_disable_entity_loader($disableEntities); - - foreach ($dom->childNodes as $child) { - if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { - throw new InvalidArgumentException('Document types are not allowed.'); - } + try { + $dom = XmlUtils::loadFile($file, array($this, 'validateSchema')); + } catch (\InvalidArgumentException $e) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } - $this->validate($dom, $file); + $this->validateExtensions($dom, $file); return simplexml_import_dom($dom, 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement'); } @@ -285,28 +270,14 @@ class XmlFileLoader extends FileLoader } } - /** - * Validates an XML document. - * - * @param \DOMDocument $dom - * @param string $file - */ - private function validate(\DOMDocument $dom, $file) - { - $this->validateSchema($dom, $file); - $this->validateExtensions($dom, $file); - } - /** * Validates a documents XML schema. * * @param \DOMDocument $dom - * @param string $file * - * @throws RuntimeException When extension references a non-existent XSD file - * @throws InvalidArgumentException When xml doesn't validate its xsd schema + * @throws RuntimeException When extension references a non-existent XSD file */ - private function validateSchema(\DOMDocument $dom, $file) + public function validateSchema(\DOMDocument $dom) { $schemaLocations = array('http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd')); @@ -360,18 +331,13 @@ $imports 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($current))); - } - libxml_use_internal_errors($current); + + return $valid; } /** @@ -403,33 +369,6 @@ EOF } } - /** - * Returns an array of XML errors. - * - * @param Boolean $internalErrors - * - * @return array - */ - private function getXmlErrors($internalErrors) - { - $errors = array(); - foreach (libxml_get_errors() as $error) { - $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)', - LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', - $error->code, - trim($error->message), - $error->file ? $error->file : 'n/a', - $error->line, - $error->column - ); - } - - libxml_clear_errors(); - libxml_use_internal_errors($internalErrors); - - return $errors; - } - /** * Loads from an extension. * @@ -472,50 +411,6 @@ EOF */ public static function convertDomElementToArray(\DomElement $element) { - $empty = true; - $config = array(); - foreach ($element->attributes as $name => $node) { - $config[$name] = SimpleXMLElement::phpize($node->value); - $empty = false; - } - - $nodeValue = false; - foreach ($element->childNodes as $node) { - if ($node instanceof \DOMText) { - if (trim($node->nodeValue)) { - $nodeValue = trim($node->nodeValue); - $empty = false; - } - } elseif (!$node instanceof \DOMComment) { - if ($node instanceof \DOMElement && '_services' === $node->nodeName) { - $value = new Reference($node->getAttribute('id')); - } else { - $value = static::convertDomElementToArray($node); - } - - $key = $node->localName; - if (isset($config[$key])) { - if (!is_array($config[$key]) || !is_int(key($config[$key]))) { - $config[$key] = array($config[$key]); - } - $config[$key][] = $value; - } else { - $config[$key] = $value; - } - - $empty = false; - } - } - - if (false !== $nodeValue) { - $value = SimpleXMLElement::phpize($nodeValue); - if (count($config)) { - $config['value'] = $value; - } else { - $config = $value; - } - } - - return !$empty ? $config : null; + return XmlUtils::convertDomElementToArray($element); } } diff --git a/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php b/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php index d154602fd3..cc5e311987 100644 --- a/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php +++ b/src/Symfony/Component/DependencyInjection/SimpleXMLElement.php @@ -11,6 +11,8 @@ namespace Symfony\Component\DependencyInjection; +use Symfony\Component\Config\Util\XmlUtils; + /** * SimpleXMLElement class. * @@ -101,27 +103,6 @@ class SimpleXMLElement extends \SimpleXMLElement */ public static function phpize($value) { - $value = (string) $value; - $lowercaseValue = strtolower($value); - - switch (true) { - case 'null' === $lowercaseValue: - return null; - case ctype_digit($value): - $raw = $value; - $cast = intval($value); - - return '0' == $value[0] ? octdec($value) : (((string) $raw == (string) $cast) ? $cast : $raw); - case 'true' === $lowercaseValue: - return true; - case 'false' === $lowercaseValue: - return false; - case is_numeric($value): - return '0x' == $value[0].$value[1] ? hexdec($value) : floatval($value); - case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $value): - return floatval(str_replace(',', '', $value)); - default: - return $value; - } + return XmlUtils::phpize($value); } }