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\Config\Loader\FileLoader;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\RouteCollection;
18 use Symfony\Component\Yaml\Exception\ParseException;
19 use Symfony\Component\Yaml\Parser as YamlParser;
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', 'controller',
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();
60 $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
61 $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
63 return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
67 $parsedConfig = $this->yamlParser->parseFile($path);
68 } catch (ParseException $e) {
69 throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
71 restore_error_handler();
74 $collection = new RouteCollection();
75 $collection->addResource(new FileResource($path));
78 if (null === $parsedConfig) {
83 if (!\is_array($parsedConfig)) {
84 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
87 foreach ($parsedConfig as $name => $config) {
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);
103 public function supports($resource, $type = null)
105 return \is_string($resource) && \in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
109 * Parses a route and adds it to the RouteCollection.
111 * @param RouteCollection $collection A RouteCollection instance
112 * @param string $name Route name
113 * @param array $config Route definition
114 * @param string $path Full path of the YAML file being processed
116 protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
118 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
119 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
120 $options = isset($config['options']) ? $config['options'] : array();
121 $host = isset($config['host']) ? $config['host'] : '';
122 $schemes = isset($config['schemes']) ? $config['schemes'] : array();
123 $methods = isset($config['methods']) ? $config['methods'] : array();
124 $condition = isset($config['condition']) ? $config['condition'] : null;
126 if (isset($config['controller'])) {
127 $defaults['_controller'] = $config['controller'];
130 $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
132 $collection->add($name, $route);
136 * Parses an import and adds the routes in the resource to the RouteCollection.
138 * @param RouteCollection $collection A RouteCollection instance
139 * @param array $config Route definition
140 * @param string $path Full path of the YAML file being processed
141 * @param string $file Loaded file name
143 protected function parseImport(RouteCollection $collection, array $config, $path, $file)
145 $type = isset($config['type']) ? $config['type'] : null;
146 $prefix = isset($config['prefix']) ? $config['prefix'] : '';
147 $defaults = isset($config['defaults']) ? $config['defaults'] : array();
148 $requirements = isset($config['requirements']) ? $config['requirements'] : array();
149 $options = isset($config['options']) ? $config['options'] : array();
150 $host = isset($config['host']) ? $config['host'] : null;
151 $condition = isset($config['condition']) ? $config['condition'] : null;
152 $schemes = isset($config['schemes']) ? $config['schemes'] : null;
153 $methods = isset($config['methods']) ? $config['methods'] : null;
155 if (isset($config['controller'])) {
156 $defaults['_controller'] = $config['controller'];
159 $this->setCurrentDir(\dirname($path));
161 $imported = $this->import($config['resource'], $type, false, $file);
163 if (!\is_array($imported)) {
164 $imported = array($imported);
167 foreach ($imported as $subCollection) {
168 /* @var $subCollection RouteCollection */
169 $subCollection->addPrefix($prefix);
170 if (null !== $host) {
171 $subCollection->setHost($host);
173 if (null !== $condition) {
174 $subCollection->setCondition($condition);
176 if (null !== $schemes) {
177 $subCollection->setSchemes($schemes);
179 if (null !== $methods) {
180 $subCollection->setMethods($methods);
182 $subCollection->addDefaults($defaults);
183 $subCollection->addRequirements($requirements);
184 $subCollection->addOptions($options);
186 $collection->addCollection($subCollection);
191 * Validates the route configuration.
193 * @param array $config A resource config
194 * @param string $name The config key
195 * @param string $path The loaded file path
197 * @throws \InvalidArgumentException If one of the provided config keys is not supported,
198 * something is missing or the combination is nonsense
200 protected function validate($config, $name, $path)
202 if (!\is_array($config)) {
203 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
205 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
206 throw new \InvalidArgumentException(sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)));
208 if (isset($config['resource']) && isset($config['path'])) {
209 throw new \InvalidArgumentException(sprintf('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.', $path, $name));
211 if (!isset($config['resource']) && isset($config['type'])) {
212 throw new \InvalidArgumentException(sprintf('The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', $name, $path));
214 if (!isset($config['resource']) && !isset($config['path'])) {
215 throw new \InvalidArgumentException(sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
217 if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
218 throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));