[DependencyInjection] Using class from Config component to loading XML files

This commit is contained in:
Martin Hasoň 2012-10-30 12:08:35 +01:00
parent d6f08809dd
commit 7f5a7fd432
2 changed files with 15 additions and 139 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Loader; namespace Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\Alias;
@ -202,33 +203,17 @@ class XmlFileLoader extends FileLoader
* *
* @return SimpleXMLElement * @return SimpleXMLElement
* *
* @throws \InvalidArgumentException When loading of XML file returns error * @throws InvalidArgumentException When loading of XML file returns error
*/ */
protected function parseFile($file) protected function parseFile($file)
{ {
$internalErrors = libxml_use_internal_errors(true); try {
$disableEntities = libxml_disable_entity_loader(true); $dom = XmlUtils::loadFile($file, array($this, 'validateSchema'));
libxml_clear_errors(); } catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
$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.');
}
} }
$this->validate($dom, $file); $this->validateExtensions($dom, $file);
return simplexml_import_dom($dom, 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement'); 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. * Validates a documents XML schema.
* *
* @param \DOMDocument $dom * @param \DOMDocument $dom
* @param string $file
* *
* @throws RuntimeException When extension references a non-existent XSD file * @throws RuntimeException When extension references a non-existent XSD file
* @throws InvalidArgumentException When xml doesn't validate its xsd schema
*/ */
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')); $schemaLocations = array('http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd'));
@ -360,18 +331,13 @@ $imports
EOF EOF
; ;
$current = libxml_use_internal_errors(true);
libxml_clear_errors();
$valid = @$dom->schemaValidateSource($source); $valid = @$dom->schemaValidateSource($source);
foreach ($tmpfiles as $tmpfile) { foreach ($tmpfiles as $tmpfile) {
@unlink($tmpfile); @unlink($tmpfile);
} }
if (!$valid) {
throw new InvalidArgumentException(implode("\n", $this->getXmlErrors($current))); return $valid;
}
libxml_use_internal_errors($current);
} }
/** /**
@ -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. * Loads from an extension.
* *
@ -472,50 +411,6 @@ EOF
*/ */
public static function convertDomElementToArray(\DomElement $element) public static function convertDomElementToArray(\DomElement $element)
{ {
$empty = true; return XmlUtils::convertDomElementToArray($element);
$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;
} }
} }

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection; namespace Symfony\Component\DependencyInjection;
use Symfony\Component\Config\Util\XmlUtils;
/** /**
* SimpleXMLElement class. * SimpleXMLElement class.
* *
@ -101,27 +103,6 @@ class SimpleXMLElement extends \SimpleXMLElement
*/ */
public static function phpize($value) public static function phpize($value)
{ {
$value = (string) $value; return XmlUtils::phpize($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;
}
} }
} }