[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 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 if (isset($config['pattern'])) { 81 if (isset($config['path'])) { 82 throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path)); 83 } 84 85 $config['path'] = $config['pattern']; 86 unset($config['pattern']); 87 } 88 89 $this->validate($config, $name, $path); 90 91 if (isset($config['resource'])) { 92 $this->parseImport($collection, $config, $path, $file); 93 } else { 94 $this->parseRoute($collection, $name, $config, $path); 95 } 96 } 97 98 return $collection; 99 } 100 101 /** 102 * {@inheritdoc} 103 */ 104 public function supports($resource, $type = null) 105 { 106 return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type); 107 } 108 109 /** 110 * Parses a route and adds it to the RouteCollection. 111 * 112 * @param RouteCollection $collection A RouteCollection instance 113 * @param string $name Route name 114 * @param array $config Route definition 115 * @param string $path Full path of the YAML file being processed 116 */ 117 protected function parseRoute(RouteCollection $collection, $name, array $config, $path) 118 { 119 $defaults = isset($config['defaults']) ? $config['defaults'] : array(); 120 $requirements = isset($config['requirements']) ? $config['requirements'] : array(); 121 $options = isset($config['options']) ? $config['options'] : array(); 122 $host = isset($config['host']) ? $config['host'] : ''; 123 $schemes = isset($config['schemes']) ? $config['schemes'] : array(); 124 $methods = isset($config['methods']) ? $config['methods'] : array(); 125 126 $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods); 127 128 $collection->add($name, $route); 129 } 130 131 /** 132 * Parses an import and adds the routes in the resource to the RouteCollection. 133 * 134 * @param RouteCollection $collection A RouteCollection instance 135 * @param array $config Route definition 136 * @param string $path Full path of the YAML file being processed 137 * @param string $file Loaded file name 138 */ 139 protected function parseImport(RouteCollection $collection, array $config, $path, $file) 140 { 141 $type = isset($config['type']) ? $config['type'] : null; 142 $prefix = isset($config['prefix']) ? $config['prefix'] : ''; 143 $defaults = isset($config['defaults']) ? $config['defaults'] : array(); 144 $requirements = isset($config['requirements']) ? $config['requirements'] : array(); 145 $options = isset($config['options']) ? $config['options'] : array(); 146 $host = isset($config['host']) ? $config['host'] : null; 147 $schemes = isset($config['schemes']) ? $config['schemes'] : null; 148 $methods = isset($config['methods']) ? $config['methods'] : null; 149 150 $this->setCurrentDir(dirname($path)); 151 152 $subCollection = $this->import($config['resource'], $type, false, $file); 153 /* @var $subCollection RouteCollection */ 154 $subCollection->addPrefix($prefix); 155 if (null !== $host) { 156 $subCollection->setHost($host); 157 } 158 if (null !== $schemes) { 159 $subCollection->setSchemes($schemes); 160 } 161 if (null !== $methods) { 162 $subCollection->setMethods($methods); 163 } 164 $subCollection->addDefaults($defaults); 165 $subCollection->addRequirements($requirements); 166 $subCollection->addOptions($options); 167 168 $collection->addCollection($subCollection); 169 } 170 171 /** 172 * Validates the route configuration. 173 * 174 * @param array $config A resource config 175 * @param string $name The config key 176 * @param string $path The loaded file path 177 * 178 * @throws \InvalidArgumentException If one of the provided config keys is not supported, 179 * something is missing or the combination is nonsense 180 */ 181 protected function validate($config, $name, $path) 182 { 183 if (!is_array($config)) { 184 throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path)); 185 } 186 if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) { 187 throw new \InvalidArgumentException(sprintf( 188 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', 189 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys) 190 )); 191 } 192 if (isset($config['resource']) && isset($config['path'])) { 193 throw new \InvalidArgumentException(sprintf( 194 '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.', 195 $path, $name 196 )); 197 } 198 if (!isset($config['resource']) && isset($config['type'])) { 199 throw new \InvalidArgumentException(sprintf( 200 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', 201 $name, $path 202 )); 203 } 204 if (!isset($config['resource']) && !isset($config['path'])) { 205 throw new \InvalidArgumentException(sprintf( 206 'You must define a "path" for the route "%s" in file "%s".', 207 $name, $path 208 )); 209 } 210 } 211 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |