annotate vendor/symfony/routing/Loader/XmlFileLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Routing\Loader;
Chris@0 13
Chris@0 14 use Symfony\Component\Routing\RouteCollection;
Chris@0 15 use Symfony\Component\Routing\Route;
Chris@0 16 use Symfony\Component\Config\Resource\FileResource;
Chris@0 17 use Symfony\Component\Config\Loader\FileLoader;
Chris@0 18 use Symfony\Component\Config\Util\XmlUtils;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * XmlFileLoader loads XML routing files.
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 * @author Tobias Schultze <http://tobion.de>
Chris@0 25 */
Chris@0 26 class XmlFileLoader extends FileLoader
Chris@0 27 {
Chris@0 28 const NAMESPACE_URI = 'http://symfony.com/schema/routing';
Chris@0 29 const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Loads an XML file.
Chris@0 33 *
Chris@0 34 * @param string $file An XML file path
Chris@0 35 * @param string|null $type The resource type
Chris@0 36 *
Chris@0 37 * @return RouteCollection A RouteCollection instance
Chris@0 38 *
Chris@0 39 * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
Chris@0 40 * parsed because it does not validate against the scheme.
Chris@0 41 */
Chris@0 42 public function load($file, $type = null)
Chris@0 43 {
Chris@0 44 $path = $this->locator->locate($file);
Chris@0 45
Chris@0 46 $xml = $this->loadFile($path);
Chris@0 47
Chris@0 48 $collection = new RouteCollection();
Chris@0 49 $collection->addResource(new FileResource($path));
Chris@0 50
Chris@0 51 // process routes and imports
Chris@0 52 foreach ($xml->documentElement->childNodes as $node) {
Chris@0 53 if (!$node instanceof \DOMElement) {
Chris@0 54 continue;
Chris@0 55 }
Chris@0 56
Chris@0 57 $this->parseNode($collection, $node, $path, $file);
Chris@0 58 }
Chris@0 59
Chris@0 60 return $collection;
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Parses a node from a loaded XML file.
Chris@0 65 *
Chris@0 66 * @param RouteCollection $collection Collection to associate with the node
Chris@0 67 * @param \DOMElement $node Element to parse
Chris@0 68 * @param string $path Full path of the XML file being processed
Chris@0 69 * @param string $file Loaded file name
Chris@0 70 *
Chris@0 71 * @throws \InvalidArgumentException When the XML is invalid
Chris@0 72 */
Chris@0 73 protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
Chris@0 74 {
Chris@0 75 if (self::NAMESPACE_URI !== $node->namespaceURI) {
Chris@0 76 return;
Chris@0 77 }
Chris@0 78
Chris@0 79 switch ($node->localName) {
Chris@0 80 case 'route':
Chris@0 81 $this->parseRoute($collection, $node, $path);
Chris@0 82 break;
Chris@0 83 case 'import':
Chris@0 84 $this->parseImport($collection, $node, $path, $file);
Chris@0 85 break;
Chris@0 86 default:
Chris@0 87 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
Chris@0 88 }
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function supports($resource, $type = null)
Chris@0 95 {
Chris@0 96 return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Parses a route and adds it to the RouteCollection.
Chris@0 101 *
Chris@0 102 * @param RouteCollection $collection RouteCollection instance
Chris@0 103 * @param \DOMElement $node Element to parse that represents a Route
Chris@0 104 * @param string $path Full path of the XML file being processed
Chris@0 105 *
Chris@0 106 * @throws \InvalidArgumentException When the XML is invalid
Chris@0 107 */
Chris@0 108 protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
Chris@0 109 {
Chris@0 110 if ('' === ($id = $node->getAttribute('id')) || !$node->hasAttribute('path')) {
Chris@0 111 throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path));
Chris@0 112 }
Chris@0 113
Chris@0 114 $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
Chris@0 115 $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
Chris@0 116
Chris@0 117 list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
Chris@0 118
Chris@0 119 $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
Chris@0 120 $collection->add($id, $route);
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Parses an import and adds the routes in the resource to the RouteCollection.
Chris@0 125 *
Chris@0 126 * @param RouteCollection $collection RouteCollection instance
Chris@0 127 * @param \DOMElement $node Element to parse that represents a Route
Chris@0 128 * @param string $path Full path of the XML file being processed
Chris@0 129 * @param string $file Loaded file name
Chris@0 130 *
Chris@0 131 * @throws \InvalidArgumentException When the XML is invalid
Chris@0 132 */
Chris@0 133 protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
Chris@0 134 {
Chris@0 135 if ('' === $resource = $node->getAttribute('resource')) {
Chris@0 136 throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path));
Chris@0 137 }
Chris@0 138
Chris@0 139 $type = $node->getAttribute('type');
Chris@0 140 $prefix = $node->getAttribute('prefix');
Chris@0 141 $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
Chris@0 142 $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
Chris@0 143 $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
Chris@0 144
Chris@0 145 list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
Chris@0 146
Chris@0 147 $this->setCurrentDir(dirname($path));
Chris@0 148
Chris@0 149 $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
Chris@0 150 /* @var $subCollection RouteCollection */
Chris@0 151 $subCollection->addPrefix($prefix);
Chris@0 152 if (null !== $host) {
Chris@0 153 $subCollection->setHost($host);
Chris@0 154 }
Chris@0 155 if (null !== $condition) {
Chris@0 156 $subCollection->setCondition($condition);
Chris@0 157 }
Chris@0 158 if (null !== $schemes) {
Chris@0 159 $subCollection->setSchemes($schemes);
Chris@0 160 }
Chris@0 161 if (null !== $methods) {
Chris@0 162 $subCollection->setMethods($methods);
Chris@0 163 }
Chris@0 164 $subCollection->addDefaults($defaults);
Chris@0 165 $subCollection->addRequirements($requirements);
Chris@0 166 $subCollection->addOptions($options);
Chris@0 167
Chris@0 168 $collection->addCollection($subCollection);
Chris@0 169 }
Chris@0 170
Chris@0 171 /**
Chris@0 172 * Loads an XML file.
Chris@0 173 *
Chris@0 174 * @param string $file An XML file path
Chris@0 175 *
Chris@0 176 * @return \DOMDocument
Chris@0 177 *
Chris@0 178 * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
Chris@0 179 * or when the XML structure is not as expected by the scheme -
Chris@0 180 * see validate()
Chris@0 181 */
Chris@0 182 protected function loadFile($file)
Chris@0 183 {
Chris@0 184 return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Parses the config elements (default, requirement, option).
Chris@0 189 *
Chris@0 190 * @param \DOMElement $node Element to parse that contains the configs
Chris@0 191 * @param string $path Full path of the XML file being processed
Chris@0 192 *
Chris@0 193 * @return array An array with the defaults as first item, requirements as second and options as third
Chris@0 194 *
Chris@0 195 * @throws \InvalidArgumentException When the XML is invalid
Chris@0 196 */
Chris@0 197 private function parseConfigs(\DOMElement $node, $path)
Chris@0 198 {
Chris@0 199 $defaults = array();
Chris@0 200 $requirements = array();
Chris@0 201 $options = array();
Chris@0 202 $condition = null;
Chris@0 203
Chris@0 204 foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
Chris@0 205 if ($node !== $n->parentNode) {
Chris@0 206 continue;
Chris@0 207 }
Chris@0 208
Chris@0 209 switch ($n->localName) {
Chris@0 210 case 'default':
Chris@0 211 if ($this->isElementValueNull($n)) {
Chris@0 212 $defaults[$n->getAttribute('key')] = null;
Chris@0 213 } else {
Chris@0 214 $defaults[$n->getAttribute('key')] = $this->parseDefaultsConfig($n, $path);
Chris@0 215 }
Chris@0 216
Chris@0 217 break;
Chris@0 218 case 'requirement':
Chris@0 219 $requirements[$n->getAttribute('key')] = trim($n->textContent);
Chris@0 220 break;
Chris@0 221 case 'option':
Chris@0 222 $options[$n->getAttribute('key')] = trim($n->textContent);
Chris@0 223 break;
Chris@0 224 case 'condition':
Chris@0 225 $condition = trim($n->textContent);
Chris@0 226 break;
Chris@0 227 default:
Chris@0 228 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
Chris@0 229 }
Chris@0 230 }
Chris@0 231
Chris@0 232 return array($defaults, $requirements, $options, $condition);
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Parses the "default" elements.
Chris@0 237 *
Chris@0 238 * @param \DOMElement $element The "default" element to parse
Chris@0 239 * @param string $path Full path of the XML file being processed
Chris@0 240 *
Chris@0 241 * @return array|bool|float|int|string|null The parsed value of the "default" element
Chris@0 242 */
Chris@0 243 private function parseDefaultsConfig(\DOMElement $element, $path)
Chris@0 244 {
Chris@0 245 if ($this->isElementValueNull($element)) {
Chris@0 246 return;
Chris@0 247 }
Chris@0 248
Chris@0 249 // Check for existing element nodes in the default element. There can
Chris@0 250 // only be a single element inside a default element. So this element
Chris@0 251 // (if one was found) can safely be returned.
Chris@0 252 foreach ($element->childNodes as $child) {
Chris@0 253 if (!$child instanceof \DOMElement) {
Chris@0 254 continue;
Chris@0 255 }
Chris@0 256
Chris@0 257 if (self::NAMESPACE_URI !== $child->namespaceURI) {
Chris@0 258 continue;
Chris@0 259 }
Chris@0 260
Chris@0 261 return $this->parseDefaultNode($child, $path);
Chris@0 262 }
Chris@0 263
Chris@0 264 // If the default element doesn't contain a nested "bool", "int", "float",
Chris@0 265 // "string", "list", or "map" element, the element contents will be treated
Chris@0 266 // as the string value of the associated default option.
Chris@0 267 return trim($element->textContent);
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * Recursively parses the value of a "default" element.
Chris@0 272 *
Chris@0 273 * @param \DOMElement $node The node value
Chris@0 274 * @param string $path Full path of the XML file being processed
Chris@0 275 *
Chris@0 276 * @return array|bool|float|int|string The parsed value
Chris@0 277 *
Chris@0 278 * @throws \InvalidArgumentException when the XML is invalid
Chris@0 279 */
Chris@0 280 private function parseDefaultNode(\DOMElement $node, $path)
Chris@0 281 {
Chris@0 282 if ($this->isElementValueNull($node)) {
Chris@0 283 return;
Chris@0 284 }
Chris@0 285
Chris@0 286 switch ($node->localName) {
Chris@0 287 case 'bool':
Chris@0 288 return 'true' === trim($node->nodeValue) || '1' === trim($node->nodeValue);
Chris@0 289 case 'int':
Chris@0 290 return (int) trim($node->nodeValue);
Chris@0 291 case 'float':
Chris@0 292 return (float) trim($node->nodeValue);
Chris@0 293 case 'string':
Chris@0 294 return trim($node->nodeValue);
Chris@0 295 case 'list':
Chris@0 296 $list = array();
Chris@0 297
Chris@0 298 foreach ($node->childNodes as $element) {
Chris@0 299 if (!$element instanceof \DOMElement) {
Chris@0 300 continue;
Chris@0 301 }
Chris@0 302
Chris@0 303 if (self::NAMESPACE_URI !== $element->namespaceURI) {
Chris@0 304 continue;
Chris@0 305 }
Chris@0 306
Chris@0 307 $list[] = $this->parseDefaultNode($element, $path);
Chris@0 308 }
Chris@0 309
Chris@0 310 return $list;
Chris@0 311 case 'map':
Chris@0 312 $map = array();
Chris@0 313
Chris@0 314 foreach ($node->childNodes as $element) {
Chris@0 315 if (!$element instanceof \DOMElement) {
Chris@0 316 continue;
Chris@0 317 }
Chris@0 318
Chris@0 319 if (self::NAMESPACE_URI !== $element->namespaceURI) {
Chris@0 320 continue;
Chris@0 321 }
Chris@0 322
Chris@0 323 $map[$element->getAttribute('key')] = $this->parseDefaultNode($element, $path);
Chris@0 324 }
Chris@0 325
Chris@0 326 return $map;
Chris@0 327 default:
Chris@0 328 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "bool", "int", "float", "string", "list", or "map".', $node->localName, $path));
Chris@0 329 }
Chris@0 330 }
Chris@0 331
Chris@0 332 private function isElementValueNull(\DOMElement $element)
Chris@0 333 {
Chris@0 334 $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
Chris@0 335
Chris@0 336 if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
Chris@0 337 return false;
Chris@0 338 }
Chris@0 339
Chris@0 340 return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
Chris@0 341 }
Chris@0 342 }