annotate vendor/symfony/routing/Loader/XmlFileLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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@17 14 use Symfony\Component\Config\Loader\FileLoader;
Chris@17 15 use Symfony\Component\Config\Resource\FileResource;
Chris@17 16 use Symfony\Component\Config\Util\XmlUtils;
Chris@17 17 use Symfony\Component\Routing\Route;
Chris@0 18 use Symfony\Component\Routing\RouteCollection;
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@14 39 * @throws \InvalidArgumentException when the file cannot be loaded or when the XML cannot be
Chris@14 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@17 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@17 147 $this->setCurrentDir(\dirname($path));
Chris@0 148
Chris@14 149 $imported = $this->import($resource, ('' !== $type ? $type : null), false, $file);
Chris@14 150
Chris@17 151 if (!\is_array($imported)) {
Chris@17 152 $imported = [$imported];
Chris@0 153 }
Chris@14 154
Chris@14 155 foreach ($imported as $subCollection) {
Chris@14 156 /* @var $subCollection RouteCollection */
Chris@14 157 $subCollection->addPrefix($prefix);
Chris@14 158 if (null !== $host) {
Chris@14 159 $subCollection->setHost($host);
Chris@14 160 }
Chris@14 161 if (null !== $condition) {
Chris@14 162 $subCollection->setCondition($condition);
Chris@14 163 }
Chris@14 164 if (null !== $schemes) {
Chris@14 165 $subCollection->setSchemes($schemes);
Chris@14 166 }
Chris@14 167 if (null !== $methods) {
Chris@14 168 $subCollection->setMethods($methods);
Chris@14 169 }
Chris@14 170 $subCollection->addDefaults($defaults);
Chris@14 171 $subCollection->addRequirements($requirements);
Chris@14 172 $subCollection->addOptions($options);
Chris@14 173
Chris@14 174 $collection->addCollection($subCollection);
Chris@0 175 }
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Loads an XML file.
Chris@0 180 *
Chris@0 181 * @param string $file An XML file path
Chris@0 182 *
Chris@0 183 * @return \DOMDocument
Chris@0 184 *
Chris@0 185 * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
Chris@0 186 * or when the XML structure is not as expected by the scheme -
Chris@0 187 * see validate()
Chris@0 188 */
Chris@0 189 protected function loadFile($file)
Chris@0 190 {
Chris@0 191 return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
Chris@0 192 }
Chris@0 193
Chris@0 194 /**
Chris@0 195 * Parses the config elements (default, requirement, option).
Chris@0 196 *
Chris@0 197 * @param \DOMElement $node Element to parse that contains the configs
Chris@0 198 * @param string $path Full path of the XML file being processed
Chris@0 199 *
Chris@0 200 * @return array An array with the defaults as first item, requirements as second and options as third
Chris@0 201 *
Chris@0 202 * @throws \InvalidArgumentException When the XML is invalid
Chris@0 203 */
Chris@0 204 private function parseConfigs(\DOMElement $node, $path)
Chris@0 205 {
Chris@17 206 $defaults = [];
Chris@17 207 $requirements = [];
Chris@17 208 $options = [];
Chris@0 209 $condition = null;
Chris@0 210
Chris@18 211 /** @var \DOMElement $n */
Chris@0 212 foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
Chris@0 213 if ($node !== $n->parentNode) {
Chris@0 214 continue;
Chris@0 215 }
Chris@0 216
Chris@0 217 switch ($n->localName) {
Chris@0 218 case 'default':
Chris@0 219 if ($this->isElementValueNull($n)) {
Chris@0 220 $defaults[$n->getAttribute('key')] = null;
Chris@0 221 } else {
Chris@0 222 $defaults[$n->getAttribute('key')] = $this->parseDefaultsConfig($n, $path);
Chris@0 223 }
Chris@0 224
Chris@0 225 break;
Chris@0 226 case 'requirement':
Chris@0 227 $requirements[$n->getAttribute('key')] = trim($n->textContent);
Chris@0 228 break;
Chris@0 229 case 'option':
Chris@18 230 $options[$n->getAttribute('key')] = XmlUtils::phpize(trim($n->textContent));
Chris@0 231 break;
Chris@0 232 case 'condition':
Chris@0 233 $condition = trim($n->textContent);
Chris@0 234 break;
Chris@0 235 default:
Chris@12 236 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement", "option" or "condition".', $n->localName, $path));
Chris@0 237 }
Chris@0 238 }
Chris@0 239
Chris@14 240 if ($controller = $node->getAttribute('controller')) {
Chris@14 241 if (isset($defaults['_controller'])) {
Chris@14 242 $name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);
Chris@14 243
Chris@14 244 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 245 }
Chris@14 246
Chris@14 247 $defaults['_controller'] = $controller;
Chris@14 248 }
Chris@14 249
Chris@17 250 return [$defaults, $requirements, $options, $condition];
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * Parses the "default" elements.
Chris@0 255 *
Chris@0 256 * @param \DOMElement $element The "default" element to parse
Chris@0 257 * @param string $path Full path of the XML file being processed
Chris@0 258 *
Chris@0 259 * @return array|bool|float|int|string|null The parsed value of the "default" element
Chris@0 260 */
Chris@0 261 private function parseDefaultsConfig(\DOMElement $element, $path)
Chris@0 262 {
Chris@0 263 if ($this->isElementValueNull($element)) {
Chris@0 264 return;
Chris@0 265 }
Chris@0 266
Chris@0 267 // Check for existing element nodes in the default element. There can
Chris@0 268 // only be a single element inside a default element. So this element
Chris@0 269 // (if one was found) can safely be returned.
Chris@0 270 foreach ($element->childNodes as $child) {
Chris@0 271 if (!$child instanceof \DOMElement) {
Chris@0 272 continue;
Chris@0 273 }
Chris@0 274
Chris@0 275 if (self::NAMESPACE_URI !== $child->namespaceURI) {
Chris@0 276 continue;
Chris@0 277 }
Chris@0 278
Chris@0 279 return $this->parseDefaultNode($child, $path);
Chris@0 280 }
Chris@0 281
Chris@0 282 // If the default element doesn't contain a nested "bool", "int", "float",
Chris@0 283 // "string", "list", or "map" element, the element contents will be treated
Chris@0 284 // as the string value of the associated default option.
Chris@0 285 return trim($element->textContent);
Chris@0 286 }
Chris@0 287
Chris@0 288 /**
Chris@0 289 * Recursively parses the value of a "default" element.
Chris@0 290 *
Chris@0 291 * @param \DOMElement $node The node value
Chris@0 292 * @param string $path Full path of the XML file being processed
Chris@0 293 *
Chris@0 294 * @return array|bool|float|int|string The parsed value
Chris@0 295 *
Chris@0 296 * @throws \InvalidArgumentException when the XML is invalid
Chris@0 297 */
Chris@0 298 private function parseDefaultNode(\DOMElement $node, $path)
Chris@0 299 {
Chris@0 300 if ($this->isElementValueNull($node)) {
Chris@0 301 return;
Chris@0 302 }
Chris@0 303
Chris@0 304 switch ($node->localName) {
Chris@0 305 case 'bool':
Chris@0 306 return 'true' === trim($node->nodeValue) || '1' === trim($node->nodeValue);
Chris@0 307 case 'int':
Chris@0 308 return (int) trim($node->nodeValue);
Chris@0 309 case 'float':
Chris@0 310 return (float) trim($node->nodeValue);
Chris@0 311 case 'string':
Chris@0 312 return trim($node->nodeValue);
Chris@0 313 case 'list':
Chris@17 314 $list = [];
Chris@0 315
Chris@0 316 foreach ($node->childNodes as $element) {
Chris@0 317 if (!$element instanceof \DOMElement) {
Chris@0 318 continue;
Chris@0 319 }
Chris@0 320
Chris@0 321 if (self::NAMESPACE_URI !== $element->namespaceURI) {
Chris@0 322 continue;
Chris@0 323 }
Chris@0 324
Chris@0 325 $list[] = $this->parseDefaultNode($element, $path);
Chris@0 326 }
Chris@0 327
Chris@0 328 return $list;
Chris@0 329 case 'map':
Chris@17 330 $map = [];
Chris@0 331
Chris@0 332 foreach ($node->childNodes as $element) {
Chris@0 333 if (!$element instanceof \DOMElement) {
Chris@0 334 continue;
Chris@0 335 }
Chris@0 336
Chris@0 337 if (self::NAMESPACE_URI !== $element->namespaceURI) {
Chris@0 338 continue;
Chris@0 339 }
Chris@0 340
Chris@0 341 $map[$element->getAttribute('key')] = $this->parseDefaultNode($element, $path);
Chris@0 342 }
Chris@0 343
Chris@0 344 return $map;
Chris@0 345 default:
Chris@0 346 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "bool", "int", "float", "string", "list", or "map".', $node->localName, $path));
Chris@0 347 }
Chris@0 348 }
Chris@0 349
Chris@0 350 private function isElementValueNull(\DOMElement $element)
Chris@0 351 {
Chris@0 352 $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
Chris@0 353
Chris@0 354 if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
Chris@0 355 return false;
Chris@0 356 }
Chris@0 357
Chris@0 358 return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
Chris@0 359 }
Chris@0 360 }