XmlFileLoader.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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) = $this->parseConfigs($node, $path);
  115. $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods);
  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) = $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 !== $schemes) {
  147. $subCollection->setSchemes($schemes);
  148. }
  149. if (null !== $methods) {
  150. $subCollection->setMethods($methods);
  151. }
  152. $subCollection->addDefaults($defaults);
  153. $subCollection->addRequirements($requirements);
  154. $subCollection->addOptions($options);
  155. $collection->addCollection($subCollection);
  156. }
  157. /**
  158. * Loads an XML file.
  159. *
  160. * @param string $file An XML file path
  161. *
  162. * @return \DOMDocument
  163. *
  164. * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
  165. * or when the XML structure is not as expected by the scheme -
  166. * see validate()
  167. */
  168. protected function loadFile($file)
  169. {
  170. return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
  171. }
  172. /**
  173. * Parses the config elements (default, requirement, option).
  174. *
  175. * @param \DOMElement $node Element to parse that contains the configs
  176. * @param string $path Full path of the XML file being processed
  177. *
  178. * @return array An array with the defaults as first item, requirements as second and options as third.
  179. *
  180. * @throws \InvalidArgumentException When the XML is invalid
  181. */
  182. private function parseConfigs(\DOMElement $node, $path)
  183. {
  184. $defaults = array();
  185. $requirements = array();
  186. $options = array();
  187. foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
  188. switch ($n->localName) {
  189. case 'default':
  190. if ($n->hasAttribute('xsi:nil') && 'true' == $n->getAttribute('xsi:nil')) {
  191. $defaults[$n->getAttribute('key')] = null;
  192. } else {
  193. $defaults[$n->getAttribute('key')] = trim($n->textContent);
  194. }
  195. break;
  196. case 'requirement':
  197. $requirements[$n->getAttribute('key')] = trim($n->textContent);
  198. break;
  199. case 'option':
  200. $options[$n->getAttribute('key')] = trim($n->textContent);
  201. break;
  202. default:
  203. throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
  204. }
  205. }
  206. return array($defaults, $requirements, $options);
  207. }
  208. }