Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[DependencyInjection] Using class from Config component to loading XM…

…L files
  • Loading branch information...
commit 719158d92c48ea6d53f59318a590d7c82152b8b7 1 parent 1d69bbe
@hason hason authored
Showing with 15 additions and 139 deletions.
  1. +12 −117 Loader/XmlFileLoader.php
  2. +3 −22 SimpleXMLElement.php
View
129 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 @@ private function parseDefinition($id, $service, $file)
*
* @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');
}
@@ -286,27 +271,13 @@ private function processAnonymousServices(SimpleXMLElement $xml, $file)
}
/**
- * 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 @@ private function validateSchema(\DOMDocument $dom, $file)
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;
}
/**
@@ -404,33 +370,6 @@ private function validateExtensions(\DOMDocument $dom, $file)
}
/**
- * 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.
*
* @param SimpleXMLElement $xml
@@ -472,50 +411,6 @@ private function loadFromExtensions(SimpleXMLElement $xml)
*/
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);
}
}
View
25 SimpleXMLElement.php
@@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection;
+use Symfony\Component\Config\Util\XmlUtils;
+
/**
* SimpleXMLElement class.
*
@@ -101,27 +103,6 @@ public function getArgumentsAsPhp($name, $lowercase = true)
*/
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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.