Keine Beschreibung

YamlFileLoader.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\Yaml\Exception\ParseException;
  15. use Symfony\Component\Yaml\Parser as YamlParser;
  16. use Symfony\Component\Config\Loader\FileLoader;
  17. /**
  18. * YamlFileLoader loads Yaml routing files.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Tobias Schultze <http://tobion.de>
  22. */
  23. class YamlFileLoader extends FileLoader
  24. {
  25. private static $availableKeys = array(
  26. 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
  27. );
  28. private $yamlParser;
  29. /**
  30. * Loads a Yaml file.
  31. *
  32. * @param string $file A Yaml file path
  33. * @param string|null $type The resource type
  34. *
  35. * @return RouteCollection A RouteCollection instance
  36. *
  37. * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
  38. */
  39. public function load($file, $type = null)
  40. {
  41. $path = $this->locator->locate($file);
  42. if (!stream_is_local($path)) {
  43. throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
  44. }
  45. if (!file_exists($path)) {
  46. throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
  47. }
  48. if (null === $this->yamlParser) {
  49. $this->yamlParser = new YamlParser();
  50. }
  51. try {
  52. $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
  53. } catch (ParseException $e) {
  54. throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
  55. }
  56. $collection = new RouteCollection();
  57. $collection->addResource(new FileResource($path));
  58. // empty file
  59. if (null === $parsedConfig) {
  60. return $collection;
  61. }
  62. // not an array
  63. if (!is_array($parsedConfig)) {
  64. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
  65. }
  66. foreach ($parsedConfig as $name => $config) {
  67. $this->validate($config, $name, $path);
  68. if (isset($config['resource'])) {
  69. $this->parseImport($collection, $config, $path, $file);
  70. } else {
  71. $this->parseRoute($collection, $name, $config, $path);
  72. }
  73. }
  74. return $collection;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function supports($resource, $type = null)
  80. {
  81. return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
  82. }
  83. /**
  84. * Parses a route and adds it to the RouteCollection.
  85. *
  86. * @param RouteCollection $collection A RouteCollection instance
  87. * @param string $name Route name
  88. * @param array $config Route definition
  89. * @param string $path Full path of the YAML file being processed
  90. */
  91. protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
  92. {
  93. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  94. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  95. $options = isset($config['options']) ? $config['options'] : array();
  96. $host = isset($config['host']) ? $config['host'] : '';
  97. $schemes = isset($config['schemes']) ? $config['schemes'] : array();
  98. $methods = isset($config['methods']) ? $config['methods'] : array();
  99. $condition = isset($config['condition']) ? $config['condition'] : null;
  100. $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
  101. $collection->add($name, $route);
  102. }
  103. /**
  104. * Parses an import and adds the routes in the resource to the RouteCollection.
  105. *
  106. * @param RouteCollection $collection A RouteCollection instance
  107. * @param array $config Route definition
  108. * @param string $path Full path of the YAML file being processed
  109. * @param string $file Loaded file name
  110. */
  111. protected function parseImport(RouteCollection $collection, array $config, $path, $file)
  112. {
  113. $type = isset($config['type']) ? $config['type'] : null;
  114. $prefix = isset($config['prefix']) ? $config['prefix'] : '';
  115. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  116. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  117. $options = isset($config['options']) ? $config['options'] : array();
  118. $host = isset($config['host']) ? $config['host'] : null;
  119. $condition = isset($config['condition']) ? $config['condition'] : null;
  120. $schemes = isset($config['schemes']) ? $config['schemes'] : null;
  121. $methods = isset($config['methods']) ? $config['methods'] : null;
  122. $this->setCurrentDir(dirname($path));
  123. $subCollection = $this->import($config['resource'], $type, false, $file);
  124. /* @var $subCollection RouteCollection */
  125. $subCollection->addPrefix($prefix);
  126. if (null !== $host) {
  127. $subCollection->setHost($host);
  128. }
  129. if (null !== $condition) {
  130. $subCollection->setCondition($condition);
  131. }
  132. if (null !== $schemes) {
  133. $subCollection->setSchemes($schemes);
  134. }
  135. if (null !== $methods) {
  136. $subCollection->setMethods($methods);
  137. }
  138. $subCollection->addDefaults($defaults);
  139. $subCollection->addRequirements($requirements);
  140. $subCollection->addOptions($options);
  141. $collection->addCollection($subCollection);
  142. }
  143. /**
  144. * Validates the route configuration.
  145. *
  146. * @param array $config A resource config
  147. * @param string $name The config key
  148. * @param string $path The loaded file path
  149. *
  150. * @throws \InvalidArgumentException If one of the provided config keys is not supported,
  151. * something is missing or the combination is nonsense
  152. */
  153. protected function validate($config, $name, $path)
  154. {
  155. if (!is_array($config)) {
  156. throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
  157. }
  158. if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
  159. throw new \InvalidArgumentException(sprintf(
  160. 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
  161. $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
  162. ));
  163. }
  164. if (isset($config['resource']) && isset($config['path'])) {
  165. throw new \InvalidArgumentException(sprintf(
  166. 'The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.',
  167. $path, $name
  168. ));
  169. }
  170. if (!isset($config['resource']) && isset($config['type'])) {
  171. throw new \InvalidArgumentException(sprintf(
  172. 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
  173. $name, $path
  174. ));
  175. }
  176. if (!isset($config['resource']) && !isset($config['path'])) {
  177. throw new \InvalidArgumentException(sprintf(
  178. 'You must define a "path" for the route "%s" in file "%s".',
  179. $name, $path
  180. ));
  181. }
  182. }
  183. }