菜谱项目

YamlFileLoader.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
  53. $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
  54. return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
  55. });
  56. try {
  57. $parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
  58. } catch (ParseException $e) {
  59. throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
  60. } finally {
  61. restore_error_handler();
  62. }
  63. $collection = new RouteCollection();
  64. $collection->addResource(new FileResource($path));
  65. // empty file
  66. if (null === $parsedConfig) {
  67. return $collection;
  68. }
  69. // not an array
  70. if (!is_array($parsedConfig)) {
  71. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
  72. }
  73. foreach ($parsedConfig as $name => $config) {
  74. $this->validate($config, $name, $path);
  75. if (isset($config['resource'])) {
  76. $this->parseImport($collection, $config, $path, $file);
  77. } else {
  78. $this->parseRoute($collection, $name, $config, $path);
  79. }
  80. }
  81. return $collection;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function supports($resource, $type = null)
  87. {
  88. return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
  89. }
  90. /**
  91. * Parses a route and adds it to the RouteCollection.
  92. *
  93. * @param RouteCollection $collection A RouteCollection instance
  94. * @param string $name Route name
  95. * @param array $config Route definition
  96. * @param string $path Full path of the YAML file being processed
  97. */
  98. protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
  99. {
  100. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  101. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  102. $options = isset($config['options']) ? $config['options'] : array();
  103. $host = isset($config['host']) ? $config['host'] : '';
  104. $schemes = isset($config['schemes']) ? $config['schemes'] : array();
  105. $methods = isset($config['methods']) ? $config['methods'] : array();
  106. $condition = isset($config['condition']) ? $config['condition'] : null;
  107. $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
  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. $condition = isset($config['condition']) ? $config['condition'] : null;
  127. $schemes = isset($config['schemes']) ? $config['schemes'] : null;
  128. $methods = isset($config['methods']) ? $config['methods'] : null;
  129. $this->setCurrentDir(dirname($path));
  130. $subCollection = $this->import($config['resource'], $type, false, $file);
  131. /* @var $subCollection RouteCollection */
  132. $subCollection->addPrefix($prefix);
  133. if (null !== $host) {
  134. $subCollection->setHost($host);
  135. }
  136. if (null !== $condition) {
  137. $subCollection->setCondition($condition);
  138. }
  139. if (null !== $schemes) {
  140. $subCollection->setSchemes($schemes);
  141. }
  142. if (null !== $methods) {
  143. $subCollection->setMethods($methods);
  144. }
  145. $subCollection->addDefaults($defaults);
  146. $subCollection->addRequirements($requirements);
  147. $subCollection->addOptions($options);
  148. $collection->addCollection($subCollection);
  149. }
  150. /**
  151. * Validates the route configuration.
  152. *
  153. * @param array $config A resource config
  154. * @param string $name The config key
  155. * @param string $path The loaded file path
  156. *
  157. * @throws \InvalidArgumentException If one of the provided config keys is not supported,
  158. * something is missing or the combination is nonsense
  159. */
  160. protected function validate($config, $name, $path)
  161. {
  162. if (!is_array($config)) {
  163. throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
  164. }
  165. if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
  166. throw new \InvalidArgumentException(sprintf(
  167. 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
  168. $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
  169. ));
  170. }
  171. if (isset($config['resource']) && isset($config['path'])) {
  172. throw new \InvalidArgumentException(sprintf(
  173. '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.',
  174. $path, $name
  175. ));
  176. }
  177. if (!isset($config['resource']) && isset($config['type'])) {
  178. throw new \InvalidArgumentException(sprintf(
  179. 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
  180. $name, $path
  181. ));
  182. }
  183. if (!isset($config['resource']) && !isset($config['path'])) {
  184. throw new \InvalidArgumentException(sprintf(
  185. 'You must define a "path" for the route "%s" in file "%s".',
  186. $name, $path
  187. ));
  188. }
  189. }
  190. }