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\Parser
as YamlParser
;
18 use Symfony\Component\Config\Loader\FileLoader
;
21 * YamlFileLoader loads Yaml routing files.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Tobias Schultze <http://tobion.de>
28 class YamlFileLoader
extends FileLoader
30 private static $availableKeys = array(
31 'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options',
38 * @param string $file A Yaml file path
39 * @param string|null $type The resource type
41 * @return RouteCollection A RouteCollection instance
43 * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
47 public function load($file, $type = null)
49 $path = $this->locator
->locate($file);
51 if (!stream_is_local($path)) {
52 throw new \
InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
55 if (!file_exists($path)) {
56 throw new \
InvalidArgumentException(sprintf('File "%s" not found.', $path));
59 if (null === $this->yamlParser
) {
60 $this->yamlParser
= new YamlParser();
63 $config = $this->yamlParser
->parse(file_get_contents($path));
65 $collection = new RouteCollection();
66 $collection->addResource(new FileResource($path));
69 if (null === $config) {
74 if (!is_array($config)) {
75 throw new \
InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
78 foreach ($config as $name => $config) {
79 if (isset($config['pattern'])) {
80 if (isset($config['path'])) {
81 throw new \
InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
84 $config['path'] = $config['pattern'];
85 unset($config['pattern']);
88 $this->validate($config, $name, $path);
90 if (isset($config['resource'])) {
91 $this->parseImport($collection, $config, $path, $file);
93 $this->parseRoute($collection, $name, $config, $path);
105 public function supports($resource, $type = null)
107 return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION
) && (!$type || 'yaml' === $type);
111 * Parses a route and adds it to the RouteCollection.
113 * @param RouteCollection $collection A RouteCollection instance
114 * @param string $name Route name
115 * @param array $config Route definition
116 * @param string $path Full path of the YAML file being processed
118 protected function parseRoute(RouteCollection
$collection, $name, array $config, $path)
120 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
121 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
122 $options = isset($config['options']) ? $config['options'] : array();
123 $host = isset($config['host']) ? $config['host'] : '';
124 $schemes = isset($config['schemes']) ? $config['schemes'] : array();
125 $methods = isset($config['methods']) ? $config['methods'] : array();
127 $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods);
129 $collection->add($name, $route);
133 * Parses an import and adds the routes in the resource to the RouteCollection.
135 * @param RouteCollection $collection A RouteCollection instance
136 * @param array $config Route definition
137 * @param string $path Full path of the YAML file being processed
138 * @param string $file Loaded file name
140 protected function parseImport(RouteCollection
$collection, array $config, $path, $file)
142 $type = isset($config['type']) ? $config['type'] : null;
143 $prefix = isset($config['prefix']) ? $config['prefix'] : '';
144 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
145 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
146 $options = isset($config['options']) ? $config['options'] : array();
147 $host = isset($config['host']) ? $config['host'] : null;
148 $schemes = isset($config['schemes']) ? $config['schemes'] : null;
149 $methods = isset($config['methods']) ? $config['methods'] : null;
151 $this->setCurrentDir(dirname($path));
153 $subCollection = $this->import($config['resource'], $type, false, $file);
154 /* @var $subCollection RouteCollection */
155 $subCollection->addPrefix($prefix);
156 if (null !== $host) {
157 $subCollection->setHost($host);
159 if (null !== $schemes) {
160 $subCollection->setSchemes($schemes);
162 if (null !== $methods) {
163 $subCollection->setMethods($methods);
165 $subCollection->addDefaults($defaults);
166 $subCollection->addRequirements($requirements);
167 $subCollection->addOptions($options);
169 $collection->addCollection($subCollection);
173 * Validates the route configuration.
175 * @param array $config A resource config
176 * @param string $name The config key
177 * @param string $path The loaded file path
179 * @throws \InvalidArgumentException If one of the provided config keys is not supported,
180 * something is missing or the combination is nonsense
182 protected function validate($config, $name, $path)
184 if (!is_array($config)) {
185 throw new \
InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
187 if ($extraKeys = array_diff(array_keys($config), self
::$availableKeys)) {
188 throw new \
InvalidArgumentException(sprintf(
189 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
190 $path, $name, implode('", "', $extraKeys), implode('", "', self
::$availableKeys)
193 if (isset($config['resource']) && isset($config['path'])) {
194 throw new \
InvalidArgumentException(sprintf(
195 '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.',
199 if (!isset($config['resource']) && isset($config['type'])) {
200 throw new \
InvalidArgumentException(sprintf(
201 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
205 if (!isset($config['resource']) && !isset($config['path'])) {
206 throw new \
InvalidArgumentException(sprintf(
207 'You must define a "path" for the route "%s" in file "%s".',