PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/core/Associates/Symfony/Routes/vendor/symfony/routing/Symfony/Component/Routing/Loader/XmlFileLoader.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 256 lines | 137 code | 33 blank | 86 comment | 21 complexity | f624cd8cf81fdece6c39cca9b0d3b40f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Loader;
  11. use Symfony\Component\Routing\RouteCollection;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Config\Loader\FileLoader;
  15. use Symfony\Component\Config\Util\XmlUtils;
  16. /**
  17. * XmlFileLoader loads XML routing files.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Tobias Schultze <http://tobion.de>
  21. *
  22. * @api
  23. */
  24. class XmlFileLoader extends FileLoader
  25. {
  26. const NAMESPACE_URI = 'http://symfony.com/schema/routing';
  27. const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
  28. /**
  29. * Loads an XML file.
  30. *
  31. * @param string $file An XML file path
  32. * @param string|null $type The resource type
  33. *
  34. * @return RouteCollection A RouteCollection instance
  35. *
  36. * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
  37. * parsed because it does not validate against the scheme.
  38. *
  39. * @api
  40. */
  41. public function load($file, $type = null)
  42. {
  43. $path = $this->locator->locate($file);
  44. $xml = $this->loadFile($path);
  45. $collection = new RouteCollection();
  46. $collection->addResource(new FileResource($path));
  47. // process routes and imports
  48. foreach ($xml->documentElement->childNodes as $node) {
  49. if (!$node instanceof \DOMElement) {
  50. continue;
  51. }
  52. $this->parseNode($collection, $node, $path, $file);
  53. }
  54. return $collection;
  55. }
  56. /**
  57. * Parses a node from a loaded XML file.
  58. *
  59. * @param RouteCollection $collection Collection to associate with the node
  60. * @param \DOMElement $node Element to parse
  61. * @param string $path Full path of the XML file being processed
  62. * @param string $file Loaded file name
  63. *
  64. * @throws \InvalidArgumentException When the XML is invalid
  65. */
  66. protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
  67. {
  68. if (self::NAMESPACE_URI !== $node->namespaceURI) {
  69. return;
  70. }
  71. switch ($node->localName) {
  72. case 'route':
  73. $this->parseRoute($collection, $node, $path);
  74. break;
  75. case 'import':
  76. $this->parseImport($collection, $node, $path, $file);
  77. break;
  78. default:
  79. throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. *
  85. * @api
  86. */
  87. public function supports($resource, $type = null)
  88. {
  89. return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
  90. }
  91. /**
  92. * Parses a route and adds it to the RouteCollection.
  93. *
  94. * @param RouteCollection $collection RouteCollection instance
  95. * @param \DOMElement $node Element to parse that represents a Route
  96. * @param string $path Full path of the XML file being processed
  97. *
  98. * @throws \InvalidArgumentException When the XML is invalid
  99. */
  100. protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
  101. {
  102. if ('' === ($id = $node->getAttribute('id')) || (!$node->hasAttribute('pattern') && !$node->hasAttribute('path'))) {
  103. throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path));
  104. }
  105. if ($node->hasAttribute('pattern')) {
  106. if ($node->hasAttribute('path')) {
  107. throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
  108. }
  109. $node->setAttribute('path', $node->getAttribute('pattern'));
  110. $node->removeAttribute('pattern');
  111. }
  112. $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
  113. $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
  114. list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
  115. $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
  116. $collection->add($id, $route);
  117. }
  118. /**
  119. * Parses an import and adds the routes in the resource to the RouteCollection.
  120. *
  121. * @param RouteCollection $collection RouteCollection instance
  122. * @param \DOMElement $node Element to parse that represents a Route
  123. * @param string $path Full path of the XML file being processed
  124. * @param string $file Loaded file name
  125. *
  126. * @throws \InvalidArgumentException When the XML is invalid
  127. */
  128. protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
  129. {
  130. if ('' === $resource = $node->getAttribute('resource')) {
  131. throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path));
  132. }
  133. $type = $node->getAttribute('type');
  134. $prefix = $node->getAttribute('prefix');
  135. $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
  136. $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
  137. $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
  138. list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
  139. $this->setCurrentDir(dirname($path));
  140. $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
  141. /* @var $subCollection RouteCollection */
  142. $subCollection->addPrefix($prefix);
  143. if (null !== $host) {
  144. $subCollection->setHost($host);
  145. }
  146. if (null !== $condition) {
  147. $subCollection->setCondition($condition);
  148. }
  149. if (null !== $schemes) {
  150. $subCollection->setSchemes($schemes);
  151. }
  152. if (null !== $methods) {
  153. $subCollection->setMethods($methods);
  154. }
  155. $subCollection->addDefaults($defaults);
  156. $subCollection->addRequirements($requirements);
  157. $subCollection->addOptions($options);
  158. $collection->addCollection($subCollection);
  159. }
  160. /**
  161. * Loads an XML file.
  162. *
  163. * @param string $file An XML file path
  164. *
  165. * @return \DOMDocument
  166. *
  167. * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
  168. * or when the XML structure is not as expected by the scheme -
  169. * see validate()
  170. */
  171. protected function loadFile($file)
  172. {
  173. return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
  174. }
  175. /**
  176. * Parses the config elements (default, requirement, option).
  177. *
  178. * @param \DOMElement $node Element to parse that contains the configs
  179. * @param string $path Full path of the XML file being processed
  180. *
  181. * @return array An array with the defaults as first item, requirements as second and options as third.
  182. *
  183. * @throws \InvalidArgumentException When the XML is invalid
  184. */
  185. private function parseConfigs(\DOMElement $node, $path)
  186. {
  187. $defaults = array();
  188. $requirements = array();
  189. $options = array();
  190. $condition = null;
  191. foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
  192. switch ($n->localName) {
  193. case 'default':
  194. if ($this->isElementValueNull($n)) {
  195. $defaults[$n->getAttribute('key')] = null;
  196. } else {
  197. $defaults[$n->getAttribute('key')] = trim($n->textContent);
  198. }
  199. break;
  200. case 'requirement':
  201. $requirements[$n->getAttribute('key')] = trim($n->textContent);
  202. break;
  203. case 'option':
  204. $options[$n->getAttribute('key')] = trim($n->textContent);
  205. break;
  206. case 'condition':
  207. $condition = trim($n->textContent);
  208. break;
  209. default:
  210. throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
  211. }
  212. }
  213. return array($defaults, $requirements, $options, $condition);
  214. }
  215. private function isElementValueNull(\DOMElement $element)
  216. {
  217. $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
  218. if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
  219. return false;
  220. }
  221. return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
  222. }
  223. }