[ Index ] |
PHP Cross Reference of phpBB-3.2.11-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; 13 14 use Symfony\Component\Config\Exception\FileLoaderLoadException; 15 use Symfony\Component\Config\Loader\LoaderInterface; 16 use Symfony\Component\Config\Resource\ResourceInterface; 17 18 /** 19 * Helps add and import routes into a RouteCollection. 20 * 21 * @author Ryan Weaver <ryan@knpuniversity.com> 22 */ 23 class RouteCollectionBuilder 24 { 25 /** 26 * @var Route[]|RouteCollectionBuilder[] 27 */ 28 private $routes = array(); 29 30 private $loader; 31 private $defaults = array(); 32 private $prefix; 33 private $host; 34 private $condition; 35 private $requirements = array(); 36 private $options = array(); 37 private $schemes; 38 private $methods; 39 private $resources = array(); 40 41 public function __construct(LoaderInterface $loader = null) 42 { 43 $this->loader = $loader; 44 } 45 46 /** 47 * Import an external routing resource and returns the RouteCollectionBuilder. 48 * 49 * $routes->import('blog.yml', '/blog'); 50 * 51 * @param mixed $resource 52 * @param string|null $prefix 53 * @param string $type 54 * 55 * @return self 56 * 57 * @throws FileLoaderLoadException 58 */ 59 public function import($resource, $prefix = '/', $type = null) 60 { 61 /** @var RouteCollection $collection */ 62 $collection = $this->load($resource, $type); 63 64 // create a builder from the RouteCollection 65 $builder = $this->createBuilder(); 66 foreach ($collection->all() as $name => $route) { 67 $builder->addRoute($route, $name); 68 } 69 70 foreach ($collection->getResources() as $resource) { 71 $builder->addResource($resource); 72 } 73 74 // mount into this builder 75 $this->mount($prefix, $builder); 76 77 return $builder; 78 } 79 80 /** 81 * Adds a route and returns it for future modification. 82 * 83 * @param string $path The route path 84 * @param string $controller The route's controller 85 * @param string|null $name The name to give this route 86 * 87 * @return Route 88 */ 89 public function add($path, $controller, $name = null) 90 { 91 $route = new Route($path); 92 $route->setDefault('_controller', $controller); 93 $this->addRoute($route, $name); 94 95 return $route; 96 } 97 98 /** 99 * Returns a RouteCollectionBuilder that can be configured and then added with mount(). 100 * 101 * @return self 102 */ 103 public function createBuilder() 104 { 105 return new self($this->loader); 106 } 107 108 /** 109 * Add a RouteCollectionBuilder. 110 * 111 * @param string $prefix 112 * @param RouteCollectionBuilder $builder 113 */ 114 public function mount($prefix, RouteCollectionBuilder $builder) 115 { 116 $builder->prefix = trim(trim($prefix), '/'); 117 $this->routes[] = $builder; 118 } 119 120 /** 121 * Adds a Route object to the builder. 122 * 123 * @param Route $route 124 * @param string|null $name 125 * 126 * @return $this 127 */ 128 public function addRoute(Route $route, $name = null) 129 { 130 if (null === $name) { 131 // used as a flag to know which routes will need a name later 132 $name = '_unnamed_route_'.spl_object_hash($route); 133 } 134 135 $this->routes[$name] = $route; 136 137 return $this; 138 } 139 140 /** 141 * Sets the host on all embedded routes (unless already set). 142 * 143 * @param string $pattern 144 * 145 * @return $this 146 */ 147 public function setHost($pattern) 148 { 149 $this->host = $pattern; 150 151 return $this; 152 } 153 154 /** 155 * Sets a condition on all embedded routes (unless already set). 156 * 157 * @param string $condition 158 * 159 * @return $this 160 */ 161 public function setCondition($condition) 162 { 163 $this->condition = $condition; 164 165 return $this; 166 } 167 168 /** 169 * Sets a default value that will be added to all embedded routes (unless that 170 * default value is already set). 171 * 172 * @param string $key 173 * @param mixed $value 174 * 175 * @return $this 176 */ 177 public function setDefault($key, $value) 178 { 179 $this->defaults[$key] = $value; 180 181 return $this; 182 } 183 184 /** 185 * Sets a requirement that will be added to all embedded routes (unless that 186 * requirement is already set). 187 * 188 * @param string $key 189 * @param mixed $regex 190 * 191 * @return $this 192 */ 193 public function setRequirement($key, $regex) 194 { 195 $this->requirements[$key] = $regex; 196 197 return $this; 198 } 199 200 /** 201 * Sets an opiton that will be added to all embedded routes (unless that 202 * option is already set). 203 * 204 * @param string $key 205 * @param mixed $value 206 * 207 * @return $this 208 */ 209 public function setOption($key, $value) 210 { 211 $this->options[$key] = $value; 212 213 return $this; 214 } 215 216 /** 217 * Sets the schemes on all embedded routes (unless already set). 218 * 219 * @param array|string $schemes 220 * 221 * @return $this 222 */ 223 public function setSchemes($schemes) 224 { 225 $this->schemes = $schemes; 226 227 return $this; 228 } 229 230 /** 231 * Sets the methods on all embedded routes (unless already set). 232 * 233 * @param array|string $methods 234 * 235 * @return $this 236 */ 237 public function setMethods($methods) 238 { 239 $this->methods = $methods; 240 241 return $this; 242 } 243 244 /** 245 * Adds a resource for this collection. 246 * 247 * @param ResourceInterface $resource 248 * 249 * @return $this 250 */ 251 private function addResource(ResourceInterface $resource) 252 { 253 $this->resources[] = $resource; 254 255 return $this; 256 } 257 258 /** 259 * Creates the final RouteCollection and returns it. 260 * 261 * @return RouteCollection 262 */ 263 public function build() 264 { 265 $routeCollection = new RouteCollection(); 266 267 foreach ($this->routes as $name => $route) { 268 if ($route instanceof Route) { 269 $route->setDefaults(array_merge($this->defaults, $route->getDefaults())); 270 $route->setOptions(array_merge($this->options, $route->getOptions())); 271 272 // we're extra careful here to avoid re-setting deprecated _method and _scheme 273 foreach ($this->requirements as $key => $val) { 274 if (!$route->hasRequirement($key)) { 275 $route->setRequirement($key, $val); 276 } 277 } 278 279 if (null !== $this->prefix) { 280 $route->setPath('/'.$this->prefix.$route->getPath()); 281 } 282 283 if (!$route->getHost()) { 284 $route->setHost($this->host); 285 } 286 287 if (!$route->getCondition()) { 288 $route->setCondition($this->condition); 289 } 290 291 if (!$route->getSchemes()) { 292 $route->setSchemes($this->schemes); 293 } 294 295 if (!$route->getMethods()) { 296 $route->setMethods($this->methods); 297 } 298 299 // auto-generate the route name if it's been marked 300 if ('_unnamed_route_' === substr($name, 0, 15)) { 301 $name = $this->generateRouteName($route); 302 } 303 304 $routeCollection->add($name, $route); 305 } else { 306 /* @var self $route */ 307 $subCollection = $route->build(); 308 $subCollection->addPrefix($this->prefix); 309 310 $routeCollection->addCollection($subCollection); 311 } 312 } 313 314 foreach ($this->resources as $resource) { 315 $routeCollection->addResource($resource); 316 } 317 318 return $routeCollection; 319 } 320 321 /** 322 * Generates a route name based on details of this route. 323 * 324 * @return string 325 */ 326 private function generateRouteName(Route $route) 327 { 328 $methods = implode('_', $route->getMethods()).'_'; 329 330 $routeName = $methods.$route->getPath(); 331 $routeName = str_replace(array('/', ':', '|', '-'), '_', $routeName); 332 $routeName = preg_replace('/[^a-z0-9A-Z_.]+/', '', $routeName); 333 334 // Collapse consecutive underscores down into a single underscore. 335 $routeName = preg_replace('/_+/', '_', $routeName); 336 337 return $routeName; 338 } 339 340 /** 341 * Finds a loader able to load an imported resource and loads it. 342 * 343 * @param mixed $resource A resource 344 * @param string|null $type The resource type or null if unknown 345 * 346 * @return RouteCollection 347 * 348 * @throws FileLoaderLoadException If no loader is found 349 */ 350 private function load($resource, $type = null) 351 { 352 if (null === $this->loader) { 353 throw new \BadMethodCallException('Cannot import other routing resources: you must pass a LoaderInterface when constructing RouteCollectionBuilder.'); 354 } 355 356 if ($this->loader->supports($resource, $type)) { 357 return $this->loader->load($resource, $type); 358 } 359 360 if (null === $resolver = $this->loader->getResolver()) { 361 throw new FileLoaderLoadException($resource); 362 } 363 364 if (false === $loader = $resolver->resolve($resource, $type)) { 365 throw new FileLoaderLoadException($resource); 366 } 367 368 return $loader->load($resource, $type); 369 } 370 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |