comparison vendor/symfony/routing/Loader/YamlFileLoader.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Routing\Loader; 12 namespace Symfony\Component\Routing\Loader;
13 13
14 use Symfony\Component\Config\Loader\FileLoader;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Routing\Route;
14 use Symfony\Component\Routing\RouteCollection; 17 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\Exception\ParseException;
18 use Symfony\Component\Yaml\Parser as YamlParser; 19 use Symfony\Component\Yaml\Parser as YamlParser;
19 use Symfony\Component\Config\Loader\FileLoader;
20 20
21 /** 21 /**
22 * YamlFileLoader loads Yaml routing files. 22 * YamlFileLoader loads Yaml routing files.
23 * 23 *
24 * @author Fabien Potencier <fabien@symfony.com> 24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Tobias Schultze <http://tobion.de> 25 * @author Tobias Schultze <http://tobion.de>
26 */ 26 */
27 class YamlFileLoader extends FileLoader 27 class YamlFileLoader extends FileLoader
28 { 28 {
29 private static $availableKeys = array( 29 private static $availableKeys = [
30 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 30 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
31 ); 31 ];
32 private $yamlParser; 32 private $yamlParser;
33 33
34 /** 34 /**
35 * Loads a Yaml file. 35 * Loads a Yaml file.
36 * 36 *
78 if (null === $parsedConfig) { 78 if (null === $parsedConfig) {
79 return $collection; 79 return $collection;
80 } 80 }
81 81
82 // not an array 82 // not an array
83 if (!is_array($parsedConfig)) { 83 if (!\is_array($parsedConfig)) {
84 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path)); 84 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
85 } 85 }
86 86
87 foreach ($parsedConfig as $name => $config) { 87 foreach ($parsedConfig as $name => $config) {
88 $this->validate($config, $name, $path); 88 $this->validate($config, $name, $path);
100 /** 100 /**
101 * {@inheritdoc} 101 * {@inheritdoc}
102 */ 102 */
103 public function supports($resource, $type = null) 103 public function supports($resource, $type = null)
104 { 104 {
105 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type); 105 return \is_string($resource) && \in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yml', 'yaml'], true) && (!$type || 'yaml' === $type);
106 } 106 }
107 107
108 /** 108 /**
109 * Parses a route and adds it to the RouteCollection. 109 * Parses a route and adds it to the RouteCollection.
110 * 110 *
113 * @param array $config Route definition 113 * @param array $config Route definition
114 * @param string $path Full path of the YAML file being processed 114 * @param string $path Full path of the YAML file being processed
115 */ 115 */
116 protected function parseRoute(RouteCollection $collection, $name, array $config, $path) 116 protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
117 { 117 {
118 $defaults = isset($config['defaults']) ? $config['defaults'] : array(); 118 $defaults = isset($config['defaults']) ? $config['defaults'] : [];
119 $requirements = isset($config['requirements']) ? $config['requirements'] : array(); 119 $requirements = isset($config['requirements']) ? $config['requirements'] : [];
120 $options = isset($config['options']) ? $config['options'] : array(); 120 $options = isset($config['options']) ? $config['options'] : [];
121 $host = isset($config['host']) ? $config['host'] : ''; 121 $host = isset($config['host']) ? $config['host'] : '';
122 $schemes = isset($config['schemes']) ? $config['schemes'] : array(); 122 $schemes = isset($config['schemes']) ? $config['schemes'] : [];
123 $methods = isset($config['methods']) ? $config['methods'] : array(); 123 $methods = isset($config['methods']) ? $config['methods'] : [];
124 $condition = isset($config['condition']) ? $config['condition'] : null; 124 $condition = isset($config['condition']) ? $config['condition'] : null;
125 125
126 if (isset($config['controller'])) { 126 if (isset($config['controller'])) {
127 $defaults['_controller'] = $config['controller']; 127 $defaults['_controller'] = $config['controller'];
128 } 128 }
142 */ 142 */
143 protected function parseImport(RouteCollection $collection, array $config, $path, $file) 143 protected function parseImport(RouteCollection $collection, array $config, $path, $file)
144 { 144 {
145 $type = isset($config['type']) ? $config['type'] : null; 145 $type = isset($config['type']) ? $config['type'] : null;
146 $prefix = isset($config['prefix']) ? $config['prefix'] : ''; 146 $prefix = isset($config['prefix']) ? $config['prefix'] : '';
147 $defaults = isset($config['defaults']) ? $config['defaults'] : array(); 147 $defaults = isset($config['defaults']) ? $config['defaults'] : [];
148 $requirements = isset($config['requirements']) ? $config['requirements'] : array(); 148 $requirements = isset($config['requirements']) ? $config['requirements'] : [];
149 $options = isset($config['options']) ? $config['options'] : array(); 149 $options = isset($config['options']) ? $config['options'] : [];
150 $host = isset($config['host']) ? $config['host'] : null; 150 $host = isset($config['host']) ? $config['host'] : null;
151 $condition = isset($config['condition']) ? $config['condition'] : null; 151 $condition = isset($config['condition']) ? $config['condition'] : null;
152 $schemes = isset($config['schemes']) ? $config['schemes'] : null; 152 $schemes = isset($config['schemes']) ? $config['schemes'] : null;
153 $methods = isset($config['methods']) ? $config['methods'] : null; 153 $methods = isset($config['methods']) ? $config['methods'] : null;
154 154
155 if (isset($config['controller'])) { 155 if (isset($config['controller'])) {
156 $defaults['_controller'] = $config['controller']; 156 $defaults['_controller'] = $config['controller'];
157 } 157 }
158 158
159 $this->setCurrentDir(dirname($path)); 159 $this->setCurrentDir(\dirname($path));
160 160
161 $imported = $this->import($config['resource'], $type, false, $file); 161 $imported = $this->import($config['resource'], $type, false, $file);
162 162
163 if (!is_array($imported)) { 163 if (!\is_array($imported)) {
164 $imported = array($imported); 164 $imported = [$imported];
165 } 165 }
166 166
167 foreach ($imported as $subCollection) { 167 foreach ($imported as $subCollection) {
168 /* @var $subCollection RouteCollection */ 168 /* @var $subCollection RouteCollection */
169 $subCollection->addPrefix($prefix); 169 $subCollection->addPrefix($prefix);
197 * @throws \InvalidArgumentException If one of the provided config keys is not supported, 197 * @throws \InvalidArgumentException If one of the provided config keys is not supported,
198 * something is missing or the combination is nonsense 198 * something is missing or the combination is nonsense
199 */ 199 */
200 protected function validate($config, $name, $path) 200 protected function validate($config, $name, $path)
201 { 201 {
202 if (!is_array($config)) { 202 if (!\is_array($config)) {
203 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path)); 203 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
204 } 204 }
205 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) { 205 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
206 throw new \InvalidArgumentException(sprintf( 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)));
207 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
208 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
209 ));
210 } 207 }
211 if (isset($config['resource']) && isset($config['path'])) { 208 if (isset($config['resource']) && isset($config['path'])) {
212 throw new \InvalidArgumentException(sprintf( 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));
213 '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.',
214 $path, $name
215 ));
216 } 210 }
217 if (!isset($config['resource']) && isset($config['type'])) { 211 if (!isset($config['resource']) && isset($config['type'])) {
218 throw new \InvalidArgumentException(sprintf( 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));
219 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
220 $name, $path
221 ));
222 } 213 }
223 if (!isset($config['resource']) && !isset($config['path'])) { 214 if (!isset($config['resource']) && !isset($config['path'])) {
224 throw new \InvalidArgumentException(sprintf( 215 throw new \InvalidArgumentException(sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
225 'You must define a "path" for the route "%s" in file "%s".',
226 $name, $path
227 ));
228 } 216 }
229 if (isset($config['controller']) && isset($config['defaults']['_controller'])) { 217 if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
230 throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name)); 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));
231 } 219 }
232 } 220 }