refactor the XMlFileLoader

This commit is contained in:
Tobias Schultze 2012-12-03 12:51:35 +01:00
parent 83fc5ff72f
commit 8361b5a58b

View File

@ -20,6 +20,7 @@ use Symfony\Component\Config\Loader\FileLoader;
* XmlFileLoader loads XML routing files. * XmlFileLoader loads XML routing files.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
* *
* @api * @api
*/ */
@ -36,7 +37,8 @@ class XmlFileLoader extends FileLoader
* *
* @return RouteCollection A RouteCollection instance * @return RouteCollection A RouteCollection instance
* *
* @throws \InvalidArgumentException When a tag can't be parsed * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
* parsed because it does not validate against the scheme.
* *
* @api * @api
*/ */
@ -64,12 +66,12 @@ class XmlFileLoader extends FileLoader
/** /**
* Parses a node from a loaded XML file. * Parses a node from a loaded XML file.
* *
* @param RouteCollection $collection the collection to associate with the node * @param RouteCollection $collection Collection to associate with the node
* @param \DOMElement $node the node to parse * @param \DOMElement $node Element to parse
* @param string $path the path of the XML file being processed * @param string $path Full path of the XML file being processed
* @param string $file * @param string $file Loaded file name
* *
* @throws \InvalidArgumentException When a tag can't be parsed * @throws \InvalidArgumentException When the XML is invalid
*/ */
protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
{ {
@ -82,47 +84,10 @@ class XmlFileLoader extends FileLoader
$this->parseRoute($collection, $node, $path); $this->parseRoute($collection, $node, $path);
break; break;
case 'import': case 'import':
$resource = $node->getAttribute('resource'); $this->parseImport($collection, $node, $path, $file);
$type = $node->getAttribute('type');
$prefix = $node->getAttribute('prefix');
$hostnamePattern = $node->hasAttribute('hostname-pattern') ? $node->getAttribute('hostname-pattern') : null;
$defaults = array();
$requirements = array();
$options = array();
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
switch ($n->localName) {
case 'default':
$defaults[$n->getAttribute('key')] = trim($n->textContent);
break;
case 'requirement':
$requirements[$n->getAttribute('key')] = trim($n->textContent);
break;
case 'option':
$options[$n->getAttribute('key')] = trim($n->textContent);
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $file));
}
}
$this->setCurrentDir(dirname($path));
$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $hostnamePattern) {
$subCollection->setHostnamePattern($hostnamePattern);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$collection->addCollection($subCollection);
break; break;
default: default:
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $file)); throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
} }
} }
@ -139,37 +104,53 @@ class XmlFileLoader extends FileLoader
/** /**
* Parses a route and adds it to the RouteCollection. * Parses a route and adds it to the RouteCollection.
* *
* @param RouteCollection $collection A RouteCollection instance * @param RouteCollection $collection RouteCollection instance
* @param \DOMElement $definition Route definition * @param \DOMElement $node Element to parse that represents a Route
* @param string $file An XML file path * @param string $path Full path of the XML file being processed
* *
* @throws \InvalidArgumentException When the definition cannot be parsed * @throws \InvalidArgumentException When the XML is invalid
*/ */
protected function parseRoute(RouteCollection $collection, \DOMElement $definition, $file) protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
{ {
$defaults = array(); list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
$requirements = array();
$options = array();
foreach ($definition->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $node) { $route = new Route($node->getAttribute('pattern'), $defaults, $requirements, $options, $node->getAttribute('hostname-pattern'));
switch ($node->localName) {
case 'default': $collection->add($node->getAttribute('id'), $route);
$defaults[$node->getAttribute('key')] = trim($node->textContent); }
break;
case 'option': /**
$options[$node->getAttribute('key')] = trim($node->textContent); * Parses an import and adds the routes in the resource to the RouteCollection.
break; *
case 'requirement': * @param RouteCollection $collection RouteCollection instance
$requirements[$node->getAttribute('key')] = trim($node->textContent); * @param \DOMElement $node Element to parse that represents a Route
break; * @param string $path Full path of the XML file being processed
default: * @param string $file Loaded file name
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $node->localName, $file)); *
} * @throws \InvalidArgumentException When the XML is invalid
*/
protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
{
$resource = $node->getAttribute('resource');
$type = $node->getAttribute('type');
$prefix = $node->getAttribute('prefix');
$hostnamePattern = $node->hasAttribute('hostname-pattern') ? $node->getAttribute('hostname-pattern') : null;
list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
$this->setCurrentDir(dirname($path));
$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $hostnamePattern) {
$subCollection->setHostnamePattern($hostnamePattern);
} }
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$route = new Route($definition->getAttribute('pattern'), $defaults, $requirements, $options, $definition->getAttribute('hostname-pattern')); $collection->addCollection($subCollection);
$collection->add($definition->getAttribute('id'), $route);
} }
/** /**
@ -252,4 +233,39 @@ class XmlFileLoader extends FileLoader
return $errors; return $errors;
} }
/**
* Parses the config elements (default, requirement, option).
*
* @param \DOMElement $node Element to parse that contains the configs
* @param string $path Full path of the XML file being processed
*
* @return array An array with the defaults as first item, requirements as second and options as third.
*
* @throws \InvalidArgumentException When the XML is invalid
*/
private function parseConfigs(\DOMElement $node, $path)
{
$defaults = array();
$requirements = array();
$options = array();
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
switch ($n->localName) {
case 'default':
$defaults[$n->getAttribute('key')] = trim($n->textContent);
break;
case 'requirement':
$requirements[$n->getAttribute('key')] = trim($n->textContent);
break;
case 'option':
$options[$n->getAttribute('key')] = trim($n->textContent);
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
}
}
return array($defaults, $requirements, $options);
}
} }