4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Loader;
14 use Symfony\Component\Routing\RouteCollection;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Config\Resource\FileResource;
17 use Symfony\Component\Yaml\Exception\ParseException;
18 use Symfony\Component\Yaml\Parser as YamlParser;
19 use Symfony\Component\Config\Loader\FileLoader;
22 * YamlFileLoader loads Yaml routing files.
24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Tobias Schultze <http://tobion.de>
27 class YamlFileLoader extends FileLoader
29 private static $availableKeys = array(
30 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
37 * @param string $file A Yaml file path
38 * @param string|null $type The resource type
40 * @return RouteCollection A RouteCollection instance
42 * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
44 public function load($file, $type = null)
46 $path = $this->locator->locate($file);
48 if (!stream_is_local($path)) {
49 throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
52 if (!file_exists($path)) {
53 throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
56 if (null === $this->yamlParser) {
57 $this->yamlParser = new YamlParser();
61 $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
62 } catch (ParseException $e) {
63 throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
66 $collection = new RouteCollection();
67 $collection->addResource(new FileResource($path));
70 if (null === $parsedConfig) {
75 if (!is_array($parsedConfig)) {
76 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
79 foreach ($parsedConfig as $name => $config) {
80 $this->validate($config, $name, $path);
82 if (isset($config['resource'])) {
83 $this->parseImport($collection, $config, $path, $file);
85 $this->parseRoute($collection, $name, $config, $path);
95 public function supports($resource, $type = null)
97 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
101 * Parses a route and adds it to the RouteCollection.
103 * @param RouteCollection $collection A RouteCollection instance
104 * @param string $name Route name
105 * @param array $config Route definition
106 * @param string $path Full path of the YAML file being processed
108 protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
110 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
111 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
112 $options = isset($config['options']) ? $config['options'] : array();
113 $host = isset($config['host']) ? $config['host'] : '';
114 $schemes = isset($config['schemes']) ? $config['schemes'] : array();
115 $methods = isset($config['methods']) ? $config['methods'] : array();
116 $condition = isset($config['condition']) ? $config['condition'] : null;
118 $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
120 $collection->add($name, $route);
124 * Parses an import and adds the routes in the resource to the RouteCollection.
126 * @param RouteCollection $collection A RouteCollection instance
127 * @param array $config Route definition
128 * @param string $path Full path of the YAML file being processed
129 * @param string $file Loaded file name
131 protected function parseImport(RouteCollection $collection, array $config, $path, $file)
133 $type = isset($config['type']) ? $config['type'] : null;
134 $prefix = isset($config['prefix']) ? $config['prefix'] : '';
135 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
136 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
137 $options = isset($config['options']) ? $config['options'] : array();
138 $host = isset($config['host']) ? $config['host'] : null;
139 $condition = isset($config['condition']) ? $config['condition'] : null;
140 $schemes = isset($config['schemes']) ? $config['schemes'] : null;
141 $methods = isset($config['methods']) ? $config['methods'] : null;
143 $this->setCurrentDir(dirname($path));
145 $subCollection = $this->import($config['resource'], $type, false, $file);
146 /* @var $subCollection RouteCollection */
147 $subCollection->addPrefix($prefix);
148 if (null !== $host) {
149 $subCollection->setHost($host);
151 if (null !== $condition) {
152 $subCollection->setCondition($condition);
154 if (null !== $schemes) {
155 $subCollection->setSchemes($schemes);
157 if (null !== $methods) {
158 $subCollection->setMethods($methods);
160 $subCollection->addDefaults($defaults);
161 $subCollection->addRequirements($requirements);
162 $subCollection->addOptions($options);
164 $collection->addCollection($subCollection);
168 * Validates the route configuration.
170 * @param array $config A resource config
171 * @param string $name The config key
172 * @param string $path The loaded file path
174 * @throws \InvalidArgumentException If one of the provided config keys is not supported,
175 * something is missing or the combination is nonsense
177 protected function validate($config, $name, $path)
179 if (!is_array($config)) {
180 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
182 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
183 throw new \InvalidArgumentException(sprintf(
184 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
185 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
188 if (isset($config['resource']) && isset($config['path'])) {
189 throw new \InvalidArgumentException(sprintf(
190 '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.',
194 if (!isset($config['resource']) && isset($config['type'])) {
195 throw new \InvalidArgumentException(sprintf(
196 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
200 if (!isset($config['resource']) && !isset($config['path'])) {
201 throw new \InvalidArgumentException(sprintf(
202 'You must define a "path" for the route "%s" in file "%s".',