[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/symfony/routing/ -> RouteCollectionBuilder.php (source)

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


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1