|
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\Yaml; |
|
18 use Symfony\Component\Config\Loader\FileLoader; |
|
19 |
|
20 /** |
|
21 * YamlFileLoader loads Yaml routing files. |
|
22 * |
|
23 * @author Fabien Potencier <fabien@symfony.com> |
|
24 * |
|
25 * @api |
|
26 */ |
|
27 class YamlFileLoader extends FileLoader |
|
28 { |
|
29 private static $availableKeys = array( |
|
30 'type', 'resource', 'prefix', 'pattern', 'options', 'defaults', 'requirements' |
|
31 ); |
|
32 |
|
33 /** |
|
34 * Loads a Yaml file. |
|
35 * |
|
36 * @param string $file A Yaml file path |
|
37 * @param string $type The resource type |
|
38 * |
|
39 * @return RouteCollection A RouteCollection instance |
|
40 * |
|
41 * @throws \InvalidArgumentException When route can't be parsed |
|
42 * |
|
43 * @api |
|
44 */ |
|
45 public function load($file, $type = null) |
|
46 { |
|
47 $path = $this->locator->locate($file); |
|
48 |
|
49 $config = Yaml::parse($path); |
|
50 |
|
51 $collection = new RouteCollection(); |
|
52 $collection->addResource(new FileResource($path)); |
|
53 |
|
54 // empty file |
|
55 if (null === $config) { |
|
56 $config = array(); |
|
57 } |
|
58 |
|
59 // not an array |
|
60 if (!is_array($config)) { |
|
61 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file)); |
|
62 } |
|
63 |
|
64 foreach ($config as $name => $config) { |
|
65 $config = $this->normalizeRouteConfig($config); |
|
66 |
|
67 if (isset($config['resource'])) { |
|
68 $type = isset($config['type']) ? $config['type'] : null; |
|
69 $prefix = isset($config['prefix']) ? $config['prefix'] : null; |
|
70 $this->setCurrentDir(dirname($path)); |
|
71 $collection->addCollection($this->import($config['resource'], $type, false, $file), $prefix); |
|
72 } else { |
|
73 $this->parseRoute($collection, $name, $config, $path); |
|
74 } |
|
75 } |
|
76 |
|
77 return $collection; |
|
78 } |
|
79 |
|
80 /** |
|
81 * Returns true if this class supports the given resource. |
|
82 * |
|
83 * @param mixed $resource A resource |
|
84 * @param string $type The resource type |
|
85 * |
|
86 * @return Boolean True if this class supports the given resource, false otherwise |
|
87 * |
|
88 * @api |
|
89 */ |
|
90 public function supports($resource, $type = null) |
|
91 { |
|
92 return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Parses a route and adds it to the RouteCollection. |
|
97 * |
|
98 * @param RouteCollection $collection A RouteCollection instance |
|
99 * @param string $name Route name |
|
100 * @param array $config Route definition |
|
101 * @param string $file A Yaml file path |
|
102 * |
|
103 * @throws \InvalidArgumentException When config pattern is not defined for the given route |
|
104 */ |
|
105 protected function parseRoute(RouteCollection $collection, $name, $config, $file) |
|
106 { |
|
107 $defaults = isset($config['defaults']) ? $config['defaults'] : array(); |
|
108 $requirements = isset($config['requirements']) ? $config['requirements'] : array(); |
|
109 $options = isset($config['options']) ? $config['options'] : array(); |
|
110 |
|
111 if (!isset($config['pattern'])) { |
|
112 throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name)); |
|
113 } |
|
114 |
|
115 $route = new Route($config['pattern'], $defaults, $requirements, $options); |
|
116 |
|
117 $collection->add($name, $route); |
|
118 } |
|
119 |
|
120 /** |
|
121 * Normalize route configuration. |
|
122 * |
|
123 * @param array $config A resource config |
|
124 * |
|
125 * @return array |
|
126 * |
|
127 * @throws InvalidArgumentException if one of the provided config keys is not supported |
|
128 */ |
|
129 private function normalizeRouteConfig(array $config) |
|
130 { |
|
131 foreach ($config as $key => $value) { |
|
132 if (!in_array($key, self::$availableKeys)) { |
|
133 throw new \InvalidArgumentException(sprintf( |
|
134 'Yaml routing loader does not support given key: "%s". Expected one of the (%s).', |
|
135 $key, implode(', ', self::$availableKeys) |
|
136 )); |
|
137 } |
|
138 } |
|
139 |
|
140 return $config; |
|
141 } |
|
142 } |