2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\Routing\Loader;
|
|
|
|
|
|
|
|
use Symfony\Components\Routing\RouteCollection;
|
|
|
|
use Symfony\Components\Routing\Route;
|
2010-06-28 08:15:15 +01:00
|
|
|
use Symfony\Components\Routing\Resource\FileResource;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XmlFileLoader loads XML routing files.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class XmlFileLoader extends FileLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Loads an XML file.
|
|
|
|
*
|
|
|
|
* @param string $file A XML file path
|
|
|
|
*
|
|
|
|
* @return RouteCollection A RouteCollection instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When a tag can't be parsed
|
|
|
|
*/
|
|
|
|
public function load($file)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$path = $this->findFile($file);
|
|
|
|
|
|
|
|
$xml = $this->loadFile($path);
|
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->addResource(new FileResource($path));
|
|
|
|
|
|
|
|
// process routes and imports
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($xml->documentElement->childNodes as $node) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$node instanceof \DOMElement) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
switch ($node->tagName) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'route':
|
|
|
|
$this->parseRoute($collection, $node, $path);
|
|
|
|
break;
|
|
|
|
case 'import':
|
2010-07-17 16:18:30 +01:00
|
|
|
$resource = (string) $node->getAttribute('resource');
|
|
|
|
$prefix = (string) $node->getAttribute('prefix');
|
|
|
|
$this->currentDir = dirname($path);
|
|
|
|
$collection->addCollection($this->import($resource), $prefix);
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-07-17 16:18:30 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
|
|
|
* @param mixed $resource A resource
|
|
|
|
*
|
|
|
|
* @return Boolean true if this class supports the given resource, false otherwise
|
|
|
|
*/
|
|
|
|
public function supports($resource)
|
|
|
|
{
|
|
|
|
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseRoute(RouteCollection $collection, $definition, $file)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$defaults = array();
|
|
|
|
$requirements = array();
|
|
|
|
$options = array();
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definition->childNodes as $node) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$node instanceof \DOMElement) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
switch ($node->tagName) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'default':
|
|
|
|
$defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
|
|
|
|
break;
|
|
|
|
case 'option':
|
|
|
|
$options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
|
|
|
|
break;
|
|
|
|
case 'requirement':
|
|
|
|
$requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options);
|
|
|
|
|
|
|
|
$collection->addRoute((string) $definition->getAttribute('id'), $route);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When loading of XML file returns error
|
|
|
|
*/
|
|
|
|
protected function loadFile($path)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
libxml_use_internal_errors(true);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$dom->load($path, LIBXML_COMPACT)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
|
|
|
$dom->validateOnParse = true;
|
|
|
|
$dom->normalizeDocument();
|
|
|
|
libxml_use_internal_errors(false);
|
|
|
|
$this->validate($dom, $path);
|
|
|
|
|
|
|
|
return $dom;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When xml doesn't validate its xsd schema
|
|
|
|
*/
|
|
|
|
protected function validate($dom, $file)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-21 15:18:29 +01:00
|
|
|
$parts = explode('/', str_replace('\\', '/', __DIR__.'/schema/routing/routing-1.0.xsd'));
|
2010-07-19 23:38:58 +01:00
|
|
|
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
2010-05-21 15:18:29 +01:00
|
|
|
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
|
|
|
|
|
|
|
|
$current = libxml_use_internal_errors(true);
|
|
|
|
if (!$dom->schemaValidate($location)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
2010-05-21 15:18:29 +01:00
|
|
|
libxml_use_internal_errors($current);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function getXmlErrors()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$errors = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach (libxml_get_errors() as $error) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$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();
|
|
|
|
|
|
|
|
return $errors;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|