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', 'pattern', '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 if (isset($config['pattern'])) {
81 if (isset($config['path'])) {
82 throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
85 @trigger_error(sprintf('The "pattern" option in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', $path), E_USER_DEPRECATED);
87 $config['path'] = $config['pattern'];
88 unset($config['pattern']);
91 $this->validate($config, $name, $path);
93 if (isset($config['resource'])) {
94 $this->parseImport($collection, $config, $path, $file);
96 $this->parseRoute($collection, $name, $config, $path);
106 public function supports($resource, $type = null)
108 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
112 * Parses a route and adds it to the RouteCollection.
114 * @param RouteCollection $collection A RouteCollection instance
115 * @param string $name Route name
116 * @param array $config Route definition
117 * @param string $path Full path of the YAML file being processed
119 protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
121 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
122 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
123 $options = isset($config['options']) ? $config['options'] : array();
124 $host = isset($config['host']) ? $config['host'] : '';
125 $schemes = isset($config['schemes']) ? $config['schemes'] : array();
126 $methods = isset($config['methods']) ? $config['methods'] : array();
127 $condition = isset($config['condition']) ? $config['condition'] : null;
129 if (isset($requirements['_method'])) {
130 if (0 === count($methods)) {
131 $methods = explode('|', $requirements['_method']);
134 unset($requirements['_method']);
135 @trigger_error(sprintf('The "_method" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "methods" option instead.', $name, $path), E_USER_DEPRECATED);
138 if (isset($requirements['_scheme'])) {
139 if (0 === count($schemes)) {
140 $schemes = explode('|', $requirements['_scheme']);
143 unset($requirements['_scheme']);
144 @trigger_error(sprintf('The "_scheme" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "schemes" option instead.', $name, $path), E_USER_DEPRECATED);
147 $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
149 $collection->add($name, $route);
153 * Parses an import and adds the routes in the resource to the RouteCollection.
155 * @param RouteCollection $collection A RouteCollection instance
156 * @param array $config Route definition
157 * @param string $path Full path of the YAML file being processed
158 * @param string $file Loaded file name
160 protected function parseImport(RouteCollection $collection, array $config, $path, $file)
162 $type = isset($config['type']) ? $config['type'] : null;
163 $prefix = isset($config['prefix']) ? $config['prefix'] : '';
164 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
165 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
166 $options = isset($config['options']) ? $config['options'] : array();
167 $host = isset($config['host']) ? $config['host'] : null;
168 $condition = isset($config['condition']) ? $config['condition'] : null;
169 $schemes = isset($config['schemes']) ? $config['schemes'] : null;
170 $methods = isset($config['methods']) ? $config['methods'] : null;
172 $this->setCurrentDir(dirname($path));
174 $subCollection = $this->import($config['resource'], $type, false, $file);
175 /* @var $subCollection RouteCollection */
176 $subCollection->addPrefix($prefix);
177 if (null !== $host) {
178 $subCollection->setHost($host);
180 if (null !== $condition) {
181 $subCollection->setCondition($condition);
183 if (null !== $schemes) {
184 $subCollection->setSchemes($schemes);
186 if (null !== $methods) {
187 $subCollection->setMethods($methods);
189 $subCollection->addDefaults($defaults);
190 $subCollection->addRequirements($requirements);
191 $subCollection->addOptions($options);
193 $collection->addCollection($subCollection);
197 * Validates the route configuration.
199 * @param array $config A resource config
200 * @param string $name The config key
201 * @param string $path The loaded file path
203 * @throws \InvalidArgumentException If one of the provided config keys is not supported,
204 * something is missing or the combination is nonsense
206 protected function validate($config, $name, $path)
208 if (!is_array($config)) {
209 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
211 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
212 throw new \InvalidArgumentException(sprintf(
213 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
214 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
217 if (isset($config['resource']) && isset($config['path'])) {
218 throw new \InvalidArgumentException(sprintf(
219 '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.',
223 if (!isset($config['resource']) && isset($config['type'])) {
224 throw new \InvalidArgumentException(sprintf(
225 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
229 if (!isset($config['resource']) && !isset($config['path'])) {
230 throw new \InvalidArgumentException(sprintf(
231 'You must define a "path" for the route "%s" in file "%s".',