]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Routing\Loader; | |
13 | ||
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; | |
19 | ||
20 | /** | |
21 | * YamlFileLoader loads Yaml routing files. | |
22 | * | |
23 | * @author Fabien Potencier <fabien@symfony.com> | |
24 | * @author Tobias Schultze <http://tobion.de> | |
25 | * | |
26 | * @api | |
27 | */ | |
28 | class YamlFileLoader extends FileLoader | |
29 | { | |
30 | private static $availableKeys = array( | |
31 | 'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', | |
32 | ); | |
33 | private $yamlParser; | |
34 | ||
35 | /** | |
36 | * Loads a Yaml file. | |
37 | * | |
38 | * @param string $file A Yaml file path | |
39 | * @param string|null $type The resource type | |
40 | * | |
41 | * @return RouteCollection A RouteCollection instance | |
42 | * | |
43 | * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid | |
44 | * | |
45 | * @api | |
46 | */ | |
47 | public function load($file, $type = null) | |
48 | { | |
49 | $path = $this->locator->locate($file); | |
50 | ||
51 | if (!stream_is_local($path)) { | |
52 | throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path)); | |
53 | } | |
54 | ||
55 | if (!file_exists($path)) { | |
56 | throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path)); | |
57 | } | |
58 | ||
59 | if (null === $this->yamlParser) { | |
60 | $this->yamlParser = new YamlParser(); | |
61 | } | |
62 | ||
63 | $config = $this->yamlParser->parse(file_get_contents($path)); | |
64 | ||
65 | $collection = new RouteCollection(); | |
66 | $collection->addResource(new FileResource($path)); | |
67 | ||
68 | // empty file | |
69 | if (null === $config) { | |
70 | return $collection; | |
71 | } | |
72 | ||
73 | // not an array | |
74 | if (!is_array($config)) { | |
75 | throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path)); | |
76 | } | |
77 | ||
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)); | |
82 | } | |
83 | ||
84 | $config['path'] = $config['pattern']; | |
85 | unset($config['pattern']); | |
86 | } | |
87 | ||
88 | $this->validate($config, $name, $path); | |
89 | ||
90 | if (isset($config['resource'])) { | |
91 | $this->parseImport($collection, $config, $path, $file); | |
92 | } else { | |
93 | $this->parseRoute($collection, $name, $config, $path); | |
94 | } | |
95 | } | |
96 | ||
97 | return $collection; | |
98 | } | |
99 | ||
100 | /** | |
101 | * {@inheritdoc} | |
102 | * | |
103 | * @api | |
104 | */ | |
105 | public function supports($resource, $type = null) | |
106 | { | |
107 | return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Parses a route and adds it to the RouteCollection. | |
112 | * | |
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 | |
117 | */ | |
118 | protected function parseRoute(RouteCollection $collection, $name, array $config, $path) | |
119 | { | |
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(); | |
126 | ||
127 | $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods); | |
128 | ||
129 | $collection->add($name, $route); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Parses an import and adds the routes in the resource to the RouteCollection. | |
134 | * | |
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 | |
139 | */ | |
140 | protected function parseImport(RouteCollection $collection, array $config, $path, $file) | |
141 | { | |
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; | |
150 | ||
151 | $this->setCurrentDir(dirname($path)); | |
152 | ||
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); | |
158 | } | |
159 | if (null !== $schemes) { | |
160 | $subCollection->setSchemes($schemes); | |
161 | } | |
162 | if (null !== $methods) { | |
163 | $subCollection->setMethods($methods); | |
164 | } | |
165 | $subCollection->addDefaults($defaults); | |
166 | $subCollection->addRequirements($requirements); | |
167 | $subCollection->addOptions($options); | |
168 | ||
169 | $collection->addCollection($subCollection); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Validates the route configuration. | |
174 | * | |
175 | * @param array $config A resource config | |
176 | * @param string $name The config key | |
177 | * @param string $path The loaded file path | |
178 | * | |
179 | * @throws \InvalidArgumentException If one of the provided config keys is not supported, | |
180 | * something is missing or the combination is nonsense | |
181 | */ | |
182 | protected function validate($config, $name, $path) | |
183 | { | |
184 | if (!is_array($config)) { | |
185 | throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path)); | |
186 | } | |
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) | |
191 | )); | |
192 | } | |
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.', | |
196 | $path, $name | |
197 | )); | |
198 | } | |
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.', | |
202 | $name, $path | |
203 | )); | |
204 | } | |
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".', | |
208 | $name, $path | |
209 | )); | |
210 | } | |
211 | } | |
212 | } |