YamlFileLoader.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Routing\Resource\FileResource;
  14. use Symfony\Component\Yaml\Yaml;
  15. /**
  16. * YamlFileLoader loads Yaml routing files.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class YamlFileLoader extends FileLoader
  21. {
  22. /**
  23. * Loads a Yaml file.
  24. *
  25. * @param string $file A Yaml file path
  26. * @param string $type The resource type
  27. *
  28. * @return RouteCollection A RouteCollection instance
  29. *
  30. * @throws \InvalidArgumentException When route can't be parsed
  31. */
  32. public function load($file, $type = null)
  33. {
  34. $path = $this->locator->locate($file);
  35. $config = $this->loadFile($path);
  36. $collection = new RouteCollection();
  37. $collection->addResource(new FileResource($path));
  38. // empty file
  39. if (null === $config) {
  40. $config = array();
  41. }
  42. // not an array
  43. if (!is_array($config)) {
  44. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
  45. }
  46. foreach ($config as $name => $config) {
  47. if (isset($config['resource'])) {
  48. $type = isset($config['type']) ? $config['type'] : null;
  49. $prefix = isset($config['prefix']) ? $config['prefix'] : null;
  50. $this->currentDir = dirname($path);
  51. $file = $this->locator->locate($config['resource'], $this->currentDir);
  52. $collection->addCollection($this->import($file, $type), $prefix);
  53. } elseif (isset($config['pattern'])) {
  54. $this->parseRoute($collection, $name, $config, $path);
  55. } else {
  56. throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name));
  57. }
  58. }
  59. return $collection;
  60. }
  61. /**
  62. * Returns true if this class supports the given resource.
  63. *
  64. * @param mixed $resource A resource
  65. * @param string $type The resource type
  66. *
  67. * @return Boolean True if this class supports the given resource, false otherwise
  68. */
  69. public function supports($resource, $type = null)
  70. {
  71. return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
  72. }
  73. /**
  74. * Parses a route and adds it to the RouteCollection.
  75. *
  76. * @param RouteCollection $collection A RouteCollection instance
  77. * @param string $name Route name
  78. * @param array $config Route definition
  79. * @param string $file A Yaml file path
  80. *
  81. * @throws \InvalidArgumentException When config pattern is not defined for the given route
  82. */
  83. protected function parseRoute(RouteCollection $collection, $name, $config, $file)
  84. {
  85. $defaults = isset($config['defaults']) ? $config['defaults'] : array();
  86. $requirements = isset($config['requirements']) ? $config['requirements'] : array();
  87. $options = isset($config['options']) ? $config['options'] : array();
  88. if (!isset($config['pattern'])) {
  89. throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name));
  90. }
  91. $route = new Route($config['pattern'], $defaults, $requirements, $options);
  92. $collection->add($name, $route);
  93. }
  94. /**
  95. * Loads a Yaml file.
  96. *
  97. * @param string $file A Yaml file path
  98. *
  99. * @return array
  100. */
  101. protected function loadFile($file)
  102. {
  103. return Yaml::load($file);
  104. }
  105. }