Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / routing / Loader / YamlFileLoader.php
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\Exception\ParseException;
18 use Symfony\Component\Yaml\Parser as YamlParser;
19 use Symfony\Component\Config\Loader\FileLoader;
20
21 /**
22  * YamlFileLoader loads Yaml routing files.
23  *
24  * @author Fabien Potencier <fabien@symfony.com>
25  * @author Tobias Schultze <http://tobion.de>
26  */
27 class YamlFileLoader extends FileLoader
28 {
29     private static $availableKeys = array(
30         'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
31     );
32     private $yamlParser;
33
34     /**
35      * Loads a Yaml file.
36      *
37      * @param string      $file A Yaml file path
38      * @param string|null $type The resource type
39      *
40      * @return RouteCollection A RouteCollection instance
41      *
42      * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
43      */
44     public function load($file, $type = null)
45     {
46         $path = $this->locator->locate($file);
47
48         if (!stream_is_local($path)) {
49             throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
50         }
51
52         if (!file_exists($path)) {
53             throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
54         }
55
56         if (null === $this->yamlParser) {
57             $this->yamlParser = new YamlParser();
58         }
59
60         try {
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);
64         }
65
66         $collection = new RouteCollection();
67         $collection->addResource(new FileResource($path));
68
69         // empty file
70         if (null === $parsedConfig) {
71             return $collection;
72         }
73
74         // not an array
75         if (!is_array($parsedConfig)) {
76             throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
77         }
78
79         foreach ($parsedConfig as $name => $config) {
80             $this->validate($config, $name, $path);
81
82             if (isset($config['resource'])) {
83                 $this->parseImport($collection, $config, $path, $file);
84             } else {
85                 $this->parseRoute($collection, $name, $config, $path);
86             }
87         }
88
89         return $collection;
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function supports($resource, $type = null)
96     {
97         return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
98     }
99
100     /**
101      * Parses a route and adds it to the RouteCollection.
102      *
103      * @param RouteCollection $collection A RouteCollection instance
104      * @param string          $name       Route name
105      * @param array           $config     Route definition
106      * @param string          $path       Full path of the YAML file being processed
107      */
108     protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
109     {
110         $defaults = isset($config['defaults']) ? $config['defaults'] : array();
111         $requirements = isset($config['requirements']) ? $config['requirements'] : array();
112         $options = isset($config['options']) ? $config['options'] : array();
113         $host = isset($config['host']) ? $config['host'] : '';
114         $schemes = isset($config['schemes']) ? $config['schemes'] : array();
115         $methods = isset($config['methods']) ? $config['methods'] : array();
116         $condition = isset($config['condition']) ? $config['condition'] : null;
117
118         $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
119
120         $collection->add($name, $route);
121     }
122
123     /**
124      * Parses an import and adds the routes in the resource to the RouteCollection.
125      *
126      * @param RouteCollection $collection A RouteCollection instance
127      * @param array           $config     Route definition
128      * @param string          $path       Full path of the YAML file being processed
129      * @param string          $file       Loaded file name
130      */
131     protected function parseImport(RouteCollection $collection, array $config, $path, $file)
132     {
133         $type = isset($config['type']) ? $config['type'] : null;
134         $prefix = isset($config['prefix']) ? $config['prefix'] : '';
135         $defaults = isset($config['defaults']) ? $config['defaults'] : array();
136         $requirements = isset($config['requirements']) ? $config['requirements'] : array();
137         $options = isset($config['options']) ? $config['options'] : array();
138         $host = isset($config['host']) ? $config['host'] : null;
139         $condition = isset($config['condition']) ? $config['condition'] : null;
140         $schemes = isset($config['schemes']) ? $config['schemes'] : null;
141         $methods = isset($config['methods']) ? $config['methods'] : null;
142
143         $this->setCurrentDir(dirname($path));
144
145         $subCollection = $this->import($config['resource'], $type, false, $file);
146         /* @var $subCollection RouteCollection */
147         $subCollection->addPrefix($prefix);
148         if (null !== $host) {
149             $subCollection->setHost($host);
150         }
151         if (null !== $condition) {
152             $subCollection->setCondition($condition);
153         }
154         if (null !== $schemes) {
155             $subCollection->setSchemes($schemes);
156         }
157         if (null !== $methods) {
158             $subCollection->setMethods($methods);
159         }
160         $subCollection->addDefaults($defaults);
161         $subCollection->addRequirements($requirements);
162         $subCollection->addOptions($options);
163
164         $collection->addCollection($subCollection);
165     }
166
167     /**
168      * Validates the route configuration.
169      *
170      * @param array  $config A resource config
171      * @param string $name   The config key
172      * @param string $path   The loaded file path
173      *
174      * @throws \InvalidArgumentException If one of the provided config keys is not supported,
175      *                                   something is missing or the combination is nonsense
176      */
177     protected function validate($config, $name, $path)
178     {
179         if (!is_array($config)) {
180             throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
181         }
182         if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
183             throw new \InvalidArgumentException(sprintf(
184                 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
185                 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
186             ));
187         }
188         if (isset($config['resource']) && isset($config['path'])) {
189             throw new \InvalidArgumentException(sprintf(
190                 '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.',
191                 $path, $name
192             ));
193         }
194         if (!isset($config['resource']) && isset($config['type'])) {
195             throw new \InvalidArgumentException(sprintf(
196                 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
197                 $name, $path
198             ));
199         }
200         if (!isset($config['resource']) && !isset($config['path'])) {
201             throw new \InvalidArgumentException(sprintf(
202                 'You must define a "path" for the route "%s" in file "%s".',
203                 $name, $path
204             ));
205         }
206     }
207 }