Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Routing\Loader; Chris@0: Chris@17: use Symfony\Component\Config\Loader\FileLoader; Chris@17: use Symfony\Component\Config\Resource\FileResource; Chris@17: use Symfony\Component\Config\Util\XmlUtils; Chris@17: use Symfony\Component\Routing\Route; Chris@0: use Symfony\Component\Routing\RouteCollection; Chris@0: Chris@0: /** Chris@0: * XmlFileLoader loads XML routing files. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Tobias Schultze Chris@0: */ Chris@0: class XmlFileLoader extends FileLoader Chris@0: { Chris@0: const NAMESPACE_URI = 'http://symfony.com/schema/routing'; Chris@0: const SCHEME_PATH = '/schema/routing/routing-1.0.xsd'; Chris@0: Chris@0: /** Chris@0: * Loads an XML file. Chris@0: * Chris@0: * @param string $file An XML file path Chris@0: * @param string|null $type The resource type Chris@0: * Chris@0: * @return RouteCollection A RouteCollection instance Chris@0: * Chris@14: * @throws \InvalidArgumentException when the file cannot be loaded or when the XML cannot be Chris@14: * parsed because it does not validate against the scheme Chris@0: */ Chris@0: public function load($file, $type = null) Chris@0: { Chris@0: $path = $this->locator->locate($file); Chris@0: Chris@0: $xml = $this->loadFile($path); Chris@0: Chris@0: $collection = new RouteCollection(); Chris@0: $collection->addResource(new FileResource($path)); Chris@0: Chris@0: // process routes and imports Chris@0: foreach ($xml->documentElement->childNodes as $node) { Chris@0: if (!$node instanceof \DOMElement) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $this->parseNode($collection, $node, $path, $file); Chris@0: } Chris@0: Chris@0: return $collection; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a node from a loaded XML file. Chris@0: * Chris@0: * @param RouteCollection $collection Collection to associate with the node Chris@0: * @param \DOMElement $node Element to parse Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * @param string $file Loaded file name Chris@0: * Chris@0: * @throws \InvalidArgumentException When the XML is invalid Chris@0: */ Chris@0: protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) Chris@0: { Chris@0: if (self::NAMESPACE_URI !== $node->namespaceURI) { Chris@0: return; Chris@0: } Chris@0: Chris@0: switch ($node->localName) { Chris@0: case 'route': Chris@0: $this->parseRoute($collection, $node, $path); Chris@0: break; Chris@0: case 'import': Chris@0: $this->parseImport($collection, $node, $path, $file); Chris@0: break; Chris@0: default: Chris@0: throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supports($resource, $type = null) Chris@0: { Chris@17: return \is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a route and adds it to the RouteCollection. Chris@0: * Chris@0: * @param RouteCollection $collection RouteCollection instance Chris@0: * @param \DOMElement $node Element to parse that represents a Route Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * Chris@0: * @throws \InvalidArgumentException When the XML is invalid Chris@0: */ Chris@0: protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path) Chris@0: { Chris@0: if ('' === ($id = $node->getAttribute('id')) || !$node->hasAttribute('path')) { Chris@0: throw new \InvalidArgumentException(sprintf('The element in file "%s" must have an "id" and a "path" attribute.', $path)); Chris@0: } Chris@0: Chris@0: $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY); Chris@0: $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY); Chris@0: Chris@0: list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path); Chris@0: Chris@0: $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition); Chris@0: $collection->add($id, $route); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses an import and adds the routes in the resource to the RouteCollection. Chris@0: * Chris@0: * @param RouteCollection $collection RouteCollection instance Chris@0: * @param \DOMElement $node Element to parse that represents a Route Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * @param string $file Loaded file name Chris@0: * Chris@0: * @throws \InvalidArgumentException When the XML is invalid Chris@0: */ Chris@0: protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file) Chris@0: { Chris@0: if ('' === $resource = $node->getAttribute('resource')) { Chris@0: throw new \InvalidArgumentException(sprintf('The element in file "%s" must have a "resource" attribute.', $path)); Chris@0: } Chris@0: Chris@0: $type = $node->getAttribute('type'); Chris@0: $prefix = $node->getAttribute('prefix'); Chris@0: $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null; Chris@0: $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null; Chris@0: $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null; Chris@0: Chris@0: list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path); Chris@0: Chris@17: $this->setCurrentDir(\dirname($path)); Chris@0: Chris@14: $imported = $this->import($resource, ('' !== $type ? $type : null), false, $file); Chris@14: Chris@17: if (!\is_array($imported)) { Chris@17: $imported = [$imported]; Chris@0: } Chris@14: Chris@14: foreach ($imported as $subCollection) { Chris@14: /* @var $subCollection RouteCollection */ Chris@14: $subCollection->addPrefix($prefix); Chris@14: if (null !== $host) { Chris@14: $subCollection->setHost($host); Chris@14: } Chris@14: if (null !== $condition) { Chris@14: $subCollection->setCondition($condition); Chris@14: } Chris@14: if (null !== $schemes) { Chris@14: $subCollection->setSchemes($schemes); Chris@14: } Chris@14: if (null !== $methods) { Chris@14: $subCollection->setMethods($methods); Chris@14: } Chris@14: $subCollection->addDefaults($defaults); Chris@14: $subCollection->addRequirements($requirements); Chris@14: $subCollection->addOptions($options); Chris@14: Chris@14: $collection->addCollection($subCollection); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads an XML file. Chris@0: * Chris@0: * @param string $file An XML file path Chris@0: * Chris@0: * @return \DOMDocument Chris@0: * Chris@0: * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors Chris@0: * or when the XML structure is not as expected by the scheme - Chris@0: * see validate() Chris@0: */ Chris@0: protected function loadFile($file) Chris@0: { Chris@0: return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses the config elements (default, requirement, option). Chris@0: * Chris@0: * @param \DOMElement $node Element to parse that contains the configs Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * Chris@0: * @return array An array with the defaults as first item, requirements as second and options as third Chris@0: * Chris@0: * @throws \InvalidArgumentException When the XML is invalid Chris@0: */ Chris@0: private function parseConfigs(\DOMElement $node, $path) Chris@0: { Chris@17: $defaults = []; Chris@17: $requirements = []; Chris@17: $options = []; Chris@0: $condition = null; Chris@0: Chris@18: /** @var \DOMElement $n */ Chris@0: foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) { Chris@0: if ($node !== $n->parentNode) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: switch ($n->localName) { Chris@0: case 'default': Chris@0: if ($this->isElementValueNull($n)) { Chris@0: $defaults[$n->getAttribute('key')] = null; Chris@0: } else { Chris@0: $defaults[$n->getAttribute('key')] = $this->parseDefaultsConfig($n, $path); Chris@0: } Chris@0: Chris@0: break; Chris@0: case 'requirement': Chris@0: $requirements[$n->getAttribute('key')] = trim($n->textContent); Chris@0: break; Chris@0: case 'option': Chris@18: $options[$n->getAttribute('key')] = XmlUtils::phpize(trim($n->textContent)); Chris@0: break; Chris@0: case 'condition': Chris@0: $condition = trim($n->textContent); Chris@0: break; Chris@0: default: Chris@12: throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement", "option" or "condition".', $n->localName, $path)); Chris@0: } Chris@0: } Chris@0: Chris@14: if ($controller = $node->getAttribute('controller')) { Chris@14: if (isset($defaults['_controller'])) { Chris@14: $name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName); Chris@14: Chris@14: throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" attribute and the defaults key "_controller" for %s.', $path, $name)); Chris@14: } Chris@14: Chris@14: $defaults['_controller'] = $controller; Chris@14: } Chris@14: Chris@17: return [$defaults, $requirements, $options, $condition]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses the "default" elements. Chris@0: * Chris@0: * @param \DOMElement $element The "default" element to parse Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * Chris@0: * @return array|bool|float|int|string|null The parsed value of the "default" element Chris@0: */ Chris@0: private function parseDefaultsConfig(\DOMElement $element, $path) Chris@0: { Chris@0: if ($this->isElementValueNull($element)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Check for existing element nodes in the default element. There can Chris@0: // only be a single element inside a default element. So this element Chris@0: // (if one was found) can safely be returned. Chris@0: foreach ($element->childNodes as $child) { Chris@0: if (!$child instanceof \DOMElement) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (self::NAMESPACE_URI !== $child->namespaceURI) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: return $this->parseDefaultNode($child, $path); Chris@0: } Chris@0: Chris@0: // If the default element doesn't contain a nested "bool", "int", "float", Chris@0: // "string", "list", or "map" element, the element contents will be treated Chris@0: // as the string value of the associated default option. Chris@0: return trim($element->textContent); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Recursively parses the value of a "default" element. Chris@0: * Chris@0: * @param \DOMElement $node The node value Chris@0: * @param string $path Full path of the XML file being processed Chris@0: * Chris@0: * @return array|bool|float|int|string The parsed value Chris@0: * Chris@0: * @throws \InvalidArgumentException when the XML is invalid Chris@0: */ Chris@0: private function parseDefaultNode(\DOMElement $node, $path) Chris@0: { Chris@0: if ($this->isElementValueNull($node)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: switch ($node->localName) { Chris@0: case 'bool': Chris@0: return 'true' === trim($node->nodeValue) || '1' === trim($node->nodeValue); Chris@0: case 'int': Chris@0: return (int) trim($node->nodeValue); Chris@0: case 'float': Chris@0: return (float) trim($node->nodeValue); Chris@0: case 'string': Chris@0: return trim($node->nodeValue); Chris@0: case 'list': Chris@17: $list = []; Chris@0: Chris@0: foreach ($node->childNodes as $element) { Chris@0: if (!$element instanceof \DOMElement) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (self::NAMESPACE_URI !== $element->namespaceURI) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $list[] = $this->parseDefaultNode($element, $path); Chris@0: } Chris@0: Chris@0: return $list; Chris@0: case 'map': Chris@17: $map = []; Chris@0: Chris@0: foreach ($node->childNodes as $element) { Chris@0: if (!$element instanceof \DOMElement) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (self::NAMESPACE_URI !== $element->namespaceURI) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $map[$element->getAttribute('key')] = $this->parseDefaultNode($element, $path); Chris@0: } Chris@0: Chris@0: return $map; Chris@0: default: Chris@0: throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "bool", "int", "float", "string", "list", or "map".', $node->localName, $path)); Chris@0: } Chris@0: } Chris@0: Chris@0: private function isElementValueNull(\DOMElement $element) Chris@0: { Chris@0: $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance'; Chris@0: Chris@0: if (!$element->hasAttributeNS($namespaceUri, 'nil')) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil'); Chris@0: } Chris@0: }