YamlFileLoader.php 7.8 KB

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