annotate vendor/symfony/routing/Loader/YamlFileLoader.php @ 0:4c8ae668cc8c

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