annotate vendor/symfony/routing/RouteCollection.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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;
Chris@0 13
Chris@0 14 use Symfony\Component\Config\Resource\ResourceInterface;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * A RouteCollection represents a set of Route instances.
Chris@0 18 *
Chris@0 19 * When adding a route at the end of the collection, an existing route
Chris@0 20 * with the same name is removed first. So there can only be one route
Chris@0 21 * with a given name.
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 * @author Tobias Schultze <http://tobion.de>
Chris@0 25 */
Chris@0 26 class RouteCollection implements \IteratorAggregate, \Countable
Chris@0 27 {
Chris@0 28 /**
Chris@0 29 * @var Route[]
Chris@0 30 */
Chris@17 31 private $routes = [];
Chris@0 32
Chris@0 33 /**
Chris@0 34 * @var array
Chris@0 35 */
Chris@17 36 private $resources = [];
Chris@0 37
Chris@0 38 public function __clone()
Chris@0 39 {
Chris@0 40 foreach ($this->routes as $name => $route) {
Chris@0 41 $this->routes[$name] = clone $route;
Chris@0 42 }
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Gets the current RouteCollection as an Iterator that includes all routes.
Chris@0 47 *
Chris@0 48 * It implements \IteratorAggregate.
Chris@0 49 *
Chris@0 50 * @see all()
Chris@0 51 *
Chris@0 52 * @return \ArrayIterator|Route[] An \ArrayIterator object for iterating over routes
Chris@0 53 */
Chris@0 54 public function getIterator()
Chris@0 55 {
Chris@0 56 return new \ArrayIterator($this->routes);
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * Gets the number of Routes in this collection.
Chris@0 61 *
Chris@0 62 * @return int The number of routes
Chris@0 63 */
Chris@0 64 public function count()
Chris@0 65 {
Chris@17 66 return \count($this->routes);
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Adds a route.
Chris@0 71 *
Chris@0 72 * @param string $name The route name
Chris@0 73 * @param Route $route A Route instance
Chris@0 74 */
Chris@0 75 public function add($name, Route $route)
Chris@0 76 {
Chris@0 77 unset($this->routes[$name]);
Chris@0 78
Chris@0 79 $this->routes[$name] = $route;
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Returns all routes in this collection.
Chris@0 84 *
Chris@0 85 * @return Route[] An array of routes
Chris@0 86 */
Chris@0 87 public function all()
Chris@0 88 {
Chris@0 89 return $this->routes;
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Gets a route by name.
Chris@0 94 *
Chris@0 95 * @param string $name The route name
Chris@0 96 *
Chris@0 97 * @return Route|null A Route instance or null when not found
Chris@0 98 */
Chris@0 99 public function get($name)
Chris@0 100 {
Chris@0 101 return isset($this->routes[$name]) ? $this->routes[$name] : null;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Removes a route or an array of routes by name from the collection.
Chris@0 106 *
Chris@14 107 * @param string|string[] $name The route name or an array of route names
Chris@0 108 */
Chris@0 109 public function remove($name)
Chris@0 110 {
Chris@0 111 foreach ((array) $name as $n) {
Chris@0 112 unset($this->routes[$n]);
Chris@0 113 }
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Adds a route collection at the end of the current set by appending all
Chris@0 118 * routes of the added collection.
Chris@0 119 */
Chris@14 120 public function addCollection(self $collection)
Chris@0 121 {
Chris@0 122 // we need to remove all routes with the same names first because just replacing them
Chris@0 123 // would not place the new route at the end of the merged array
Chris@0 124 foreach ($collection->all() as $name => $route) {
Chris@0 125 unset($this->routes[$name]);
Chris@0 126 $this->routes[$name] = $route;
Chris@0 127 }
Chris@0 128
Chris@14 129 foreach ($collection->getResources() as $resource) {
Chris@14 130 $this->addResource($resource);
Chris@14 131 }
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Adds a prefix to the path of all child routes.
Chris@0 136 *
Chris@0 137 * @param string $prefix An optional prefix to add before each pattern of the route collection
Chris@0 138 * @param array $defaults An array of default values
Chris@0 139 * @param array $requirements An array of requirements
Chris@0 140 */
Chris@17 141 public function addPrefix($prefix, array $defaults = [], array $requirements = [])
Chris@0 142 {
Chris@0 143 $prefix = trim(trim($prefix), '/');
Chris@0 144
Chris@0 145 if ('' === $prefix) {
Chris@0 146 return;
Chris@0 147 }
Chris@0 148
Chris@0 149 foreach ($this->routes as $route) {
Chris@0 150 $route->setPath('/'.$prefix.$route->getPath());
Chris@0 151 $route->addDefaults($defaults);
Chris@0 152 $route->addRequirements($requirements);
Chris@0 153 }
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Sets the host pattern on all routes.
Chris@0 158 *
Chris@0 159 * @param string $pattern The pattern
Chris@0 160 * @param array $defaults An array of default values
Chris@0 161 * @param array $requirements An array of requirements
Chris@0 162 */
Chris@17 163 public function setHost($pattern, array $defaults = [], array $requirements = [])
Chris@0 164 {
Chris@0 165 foreach ($this->routes as $route) {
Chris@0 166 $route->setHost($pattern);
Chris@0 167 $route->addDefaults($defaults);
Chris@0 168 $route->addRequirements($requirements);
Chris@0 169 }
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * Sets a condition on all routes.
Chris@0 174 *
Chris@0 175 * Existing conditions will be overridden.
Chris@0 176 *
Chris@0 177 * @param string $condition The condition
Chris@0 178 */
Chris@0 179 public function setCondition($condition)
Chris@0 180 {
Chris@0 181 foreach ($this->routes as $route) {
Chris@0 182 $route->setCondition($condition);
Chris@0 183 }
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * Adds defaults to all routes.
Chris@0 188 *
Chris@0 189 * An existing default value under the same name in a route will be overridden.
Chris@0 190 *
Chris@0 191 * @param array $defaults An array of default values
Chris@0 192 */
Chris@0 193 public function addDefaults(array $defaults)
Chris@0 194 {
Chris@0 195 if ($defaults) {
Chris@0 196 foreach ($this->routes as $route) {
Chris@0 197 $route->addDefaults($defaults);
Chris@0 198 }
Chris@0 199 }
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Adds requirements to all routes.
Chris@0 204 *
Chris@0 205 * An existing requirement under the same name in a route will be overridden.
Chris@0 206 *
Chris@0 207 * @param array $requirements An array of requirements
Chris@0 208 */
Chris@0 209 public function addRequirements(array $requirements)
Chris@0 210 {
Chris@0 211 if ($requirements) {
Chris@0 212 foreach ($this->routes as $route) {
Chris@0 213 $route->addRequirements($requirements);
Chris@0 214 }
Chris@0 215 }
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Adds options to all routes.
Chris@0 220 *
Chris@0 221 * An existing option value under the same name in a route will be overridden.
Chris@0 222 *
Chris@0 223 * @param array $options An array of options
Chris@0 224 */
Chris@0 225 public function addOptions(array $options)
Chris@0 226 {
Chris@0 227 if ($options) {
Chris@0 228 foreach ($this->routes as $route) {
Chris@0 229 $route->addOptions($options);
Chris@0 230 }
Chris@0 231 }
Chris@0 232 }
Chris@0 233
Chris@0 234 /**
Chris@0 235 * Sets the schemes (e.g. 'https') all child routes are restricted to.
Chris@0 236 *
Chris@14 237 * @param string|string[] $schemes The scheme or an array of schemes
Chris@0 238 */
Chris@0 239 public function setSchemes($schemes)
Chris@0 240 {
Chris@0 241 foreach ($this->routes as $route) {
Chris@0 242 $route->setSchemes($schemes);
Chris@0 243 }
Chris@0 244 }
Chris@0 245
Chris@0 246 /**
Chris@0 247 * Sets the HTTP methods (e.g. 'POST') all child routes are restricted to.
Chris@0 248 *
Chris@14 249 * @param string|string[] $methods The method or an array of methods
Chris@0 250 */
Chris@0 251 public function setMethods($methods)
Chris@0 252 {
Chris@0 253 foreach ($this->routes as $route) {
Chris@0 254 $route->setMethods($methods);
Chris@0 255 }
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Returns an array of resources loaded to build this collection.
Chris@0 260 *
Chris@0 261 * @return ResourceInterface[] An array of resources
Chris@0 262 */
Chris@0 263 public function getResources()
Chris@0 264 {
Chris@14 265 return array_values($this->resources);
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@14 269 * Adds a resource for this collection. If the resource already exists
Chris@14 270 * it is not added.
Chris@0 271 */
Chris@0 272 public function addResource(ResourceInterface $resource)
Chris@0 273 {
Chris@14 274 $key = (string) $resource;
Chris@14 275
Chris@14 276 if (!isset($this->resources[$key])) {
Chris@14 277 $this->resources[$key] = $resource;
Chris@14 278 }
Chris@0 279 }
Chris@0 280 }