PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/Routing/Loader/XmlFileLoader.php

https://github.com/tumf/tepco
PHP | 190 lines | 106 code | 22 blank | 62 comment | 10 complexity | 135b9334c44c7baa09ea204207ad9909 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. /**
  16. * XmlFileLoader loads XML routing files.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class XmlFileLoader extends FileLoader
  21. {
  22. /**
  23. * Loads an XML file.
  24. *
  25. * @param string $file An XML file path
  26. * @param string $type The resource type
  27. *
  28. * @return RouteCollection A RouteCollection instance
  29. *
  30. * @throws \InvalidArgumentException When a tag can't be parsed
  31. */
  32. public function load($file, $type = null)
  33. {
  34. $path = $this->locator->locate($file);
  35. $xml = $this->loadFile($path);
  36. $collection = new RouteCollection();
  37. $collection->addResource(new FileResource($path));
  38. // process routes and imports
  39. foreach ($xml->documentElement->childNodes as $node) {
  40. if (!$node instanceof \DOMElement) {
  41. continue;
  42. }
  43. switch ($node->tagName) {
  44. case 'route':
  45. $this->parseRoute($collection, $node, $path);
  46. break;
  47. case 'import':
  48. $resource = (string) $node->getAttribute('resource');
  49. $type = (string) $node->getAttribute('type');
  50. $prefix = (string) $node->getAttribute('prefix');
  51. $this->currentDir = dirname($path);
  52. $collection->addCollection($this->import($resource, ('' !== $type ? $type : null)), $prefix);
  53. break;
  54. default:
  55. throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
  56. }
  57. }
  58. return $collection;
  59. }
  60. /**
  61. * Returns true if this class supports the given resource.
  62. *
  63. * @param mixed $resource A resource
  64. * @param string $type The resource type
  65. *
  66. * @return Boolean True if this class supports the given resource, false otherwise
  67. */
  68. public function supports($resource, $type = null)
  69. {
  70. return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
  71. }
  72. /**
  73. * Parses a route and adds it to the RouteCollection.
  74. *
  75. * @param RouteCollection $collection A RouteCollection instance
  76. * @param \DOMElement $definition Route definition
  77. * @param string $file An XML file path
  78. *
  79. * @throws \InvalidArgumentException When the definition cannot be parsed
  80. */
  81. protected function parseRoute(RouteCollection $collection, \DOMElement $definition, $file)
  82. {
  83. $defaults = array();
  84. $requirements = array();
  85. $options = array();
  86. foreach ($definition->childNodes as $node) {
  87. if (!$node instanceof \DOMElement) {
  88. continue;
  89. }
  90. switch ($node->tagName) {
  91. case 'default':
  92. $defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
  93. break;
  94. case 'option':
  95. $options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
  96. break;
  97. case 'requirement':
  98. $requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
  99. break;
  100. default:
  101. throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
  102. }
  103. }
  104. $route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options);
  105. $collection->add((string) $definition->getAttribute('id'), $route);
  106. }
  107. /**
  108. * Loads an XML file.
  109. *
  110. * @param string $file An XML file path
  111. *
  112. * @return \DOMDocument
  113. *
  114. * @throws \InvalidArgumentException When loading of XML file returns error
  115. */
  116. protected function loadFile($file)
  117. {
  118. $dom = new \DOMDocument();
  119. libxml_use_internal_errors(true);
  120. if (!$dom->load($file, LIBXML_COMPACT)) {
  121. throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
  122. }
  123. $dom->validateOnParse = true;
  124. $dom->normalizeDocument();
  125. libxml_use_internal_errors(false);
  126. $this->validate($dom);
  127. return $dom;
  128. }
  129. /**
  130. * Validates a loaded XML file.
  131. *
  132. * @param \DOMDocument $dom A loaded XML file
  133. *
  134. * @throws \InvalidArgumentException When XML doesn't validate its XSD schema
  135. */
  136. protected function validate(\DOMDocument $dom)
  137. {
  138. $parts = explode('/', str_replace('\\', '/', __DIR__.'/schema/routing/routing-1.0.xsd'));
  139. $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
  140. $location = 'file:///'.$drive.implode('/', $parts);
  141. $current = libxml_use_internal_errors(true);
  142. if (!$dom->schemaValidate($location)) {
  143. throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
  144. }
  145. libxml_use_internal_errors($current);
  146. }
  147. /**
  148. * Retrieves libxml errors and clears them.
  149. *
  150. * @return array An array of libxml error strings
  151. */
  152. protected function getXmlErrors()
  153. {
  154. $errors = array();
  155. foreach (libxml_get_errors() as $error) {
  156. $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
  157. LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
  158. $error->code,
  159. trim($error->message),
  160. $error->file ? $error->file : 'n/a',
  161. $error->line,
  162. $error->column
  163. );
  164. }
  165. libxml_clear_errors();
  166. return $errors;
  167. }
  168. }