4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Loader;
14 use Symfony\Component\Routing\RouteCollection;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Config\Resource\FileResource;
17 use Symfony\Component\Config\Loader\FileLoader;
18 use Symfony\Component\Config\Util\XmlUtils;
21 * XmlFileLoader loads XML routing files.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Tobias Schultze <http://tobion.de>
26 class XmlFileLoader extends FileLoader
28 const NAMESPACE_URI = 'http://symfony.com/schema/routing';
29 const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
34 * @param string $file An XML file path
35 * @param string|null $type The resource type
37 * @return RouteCollection A RouteCollection instance
39 * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
40 * parsed because it does not validate against the scheme.
42 public function load($file, $type = null)
44 $path = $this->locator->locate($file);
46 $xml = $this->loadFile($path);
48 $collection = new RouteCollection();
49 $collection->addResource(new FileResource($path));
51 // process routes and imports
52 foreach ($xml->documentElement->childNodes as $node) {
53 if (!$node instanceof \DOMElement) {
57 $this->parseNode($collection, $node, $path, $file);
64 * Parses a node from a loaded XML file.
66 * @param RouteCollection $collection Collection to associate with the node
67 * @param \DOMElement $node Element to parse
68 * @param string $path Full path of the XML file being processed
69 * @param string $file Loaded file name
71 * @throws \InvalidArgumentException When the XML is invalid
73 protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
75 if (self::NAMESPACE_URI !== $node->namespaceURI) {
79 switch ($node->localName) {
81 $this->parseRoute($collection, $node, $path);
84 $this->parseImport($collection, $node, $path, $file);
87 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
94 public function supports($resource, $type = null)
96 return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
100 * Parses a route and adds it to the RouteCollection.
102 * @param RouteCollection $collection RouteCollection instance
103 * @param \DOMElement $node Element to parse that represents a Route
104 * @param string $path Full path of the XML file being processed
106 * @throws \InvalidArgumentException When the XML is invalid
108 protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
110 if ('' === ($id = $node->getAttribute('id')) || !$node->hasAttribute('path')) {
111 throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path));
114 $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
115 $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
117 list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
119 $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
120 $collection->add($id, $route);
124 * Parses an import and adds the routes in the resource to the RouteCollection.
126 * @param RouteCollection $collection RouteCollection instance
127 * @param \DOMElement $node Element to parse that represents a Route
128 * @param string $path Full path of the XML file being processed
129 * @param string $file Loaded file name
131 * @throws \InvalidArgumentException When the XML is invalid
133 protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
135 if ('' === $resource = $node->getAttribute('resource')) {
136 throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path));
139 $type = $node->getAttribute('type');
140 $prefix = $node->getAttribute('prefix');
141 $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
142 $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
143 $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
145 list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
147 $this->setCurrentDir(dirname($path));
149 $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
150 /* @var $subCollection RouteCollection */
151 $subCollection->addPrefix($prefix);
152 if (null !== $host) {
153 $subCollection->setHost($host);
155 if (null !== $condition) {
156 $subCollection->setCondition($condition);
158 if (null !== $schemes) {
159 $subCollection->setSchemes($schemes);
161 if (null !== $methods) {
162 $subCollection->setMethods($methods);
164 $subCollection->addDefaults($defaults);
165 $subCollection->addRequirements($requirements);
166 $subCollection->addOptions($options);
168 $collection->addCollection($subCollection);
174 * @param string $file An XML file path
176 * @return \DOMDocument
178 * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
179 * or when the XML structure is not as expected by the scheme -
182 protected function loadFile($file)
184 return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
188 * Parses the config elements (default, requirement, option).
190 * @param \DOMElement $node Element to parse that contains the configs
191 * @param string $path Full path of the XML file being processed
193 * @return array An array with the defaults as first item, requirements as second and options as third
195 * @throws \InvalidArgumentException When the XML is invalid
197 private function parseConfigs(\DOMElement $node, $path)
200 $requirements = array();
204 foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
205 if ($node !== $n->parentNode) {
209 switch ($n->localName) {
211 if ($this->isElementValueNull($n)) {
212 $defaults[$n->getAttribute('key')] = null;
214 $defaults[$n->getAttribute('key')] = $this->parseDefaultsConfig($n, $path);
219 $requirements[$n->getAttribute('key')] = trim($n->textContent);
222 $options[$n->getAttribute('key')] = trim($n->textContent);
225 $condition = trim($n->textContent);
228 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement", "option" or "condition".', $n->localName, $path));
232 return array($defaults, $requirements, $options, $condition);
236 * Parses the "default" elements.
238 * @param \DOMElement $element The "default" element to parse
239 * @param string $path Full path of the XML file being processed
241 * @return array|bool|float|int|string|null The parsed value of the "default" element
243 private function parseDefaultsConfig(\DOMElement $element, $path)
245 if ($this->isElementValueNull($element)) {
249 // Check for existing element nodes in the default element. There can
250 // only be a single element inside a default element. So this element
251 // (if one was found) can safely be returned.
252 foreach ($element->childNodes as $child) {
253 if (!$child instanceof \DOMElement) {
257 if (self::NAMESPACE_URI !== $child->namespaceURI) {
261 return $this->parseDefaultNode($child, $path);
264 // If the default element doesn't contain a nested "bool", "int", "float",
265 // "string", "list", or "map" element, the element contents will be treated
266 // as the string value of the associated default option.
267 return trim($element->textContent);
271 * Recursively parses the value of a "default" element.
273 * @param \DOMElement $node The node value
274 * @param string $path Full path of the XML file being processed
276 * @return array|bool|float|int|string The parsed value
278 * @throws \InvalidArgumentException when the XML is invalid
280 private function parseDefaultNode(\DOMElement $node, $path)
282 if ($this->isElementValueNull($node)) {
286 switch ($node->localName) {
288 return 'true' === trim($node->nodeValue) || '1' === trim($node->nodeValue);
290 return (int) trim($node->nodeValue);
292 return (float) trim($node->nodeValue);
294 return trim($node->nodeValue);
298 foreach ($node->childNodes as $element) {
299 if (!$element instanceof \DOMElement) {
303 if (self::NAMESPACE_URI !== $element->namespaceURI) {
307 $list[] = $this->parseDefaultNode($element, $path);
314 foreach ($node->childNodes as $element) {
315 if (!$element instanceof \DOMElement) {
319 if (self::NAMESPACE_URI !== $element->namespaceURI) {
323 $map[$element->getAttribute('key')] = $this->parseDefaultNode($element, $path);
328 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "bool", "int", "float", "string", "list", or "map".', $node->localName, $path));
332 private function isElementValueNull(\DOMElement $element)
334 $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
336 if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
340 return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');