PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Loader/XmlFileLoader.php

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