YamlFileLoader.php 8.0 KB

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