vendor/pimcore/pimcore/lib/Routing/DynamicRouteProvider.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Routing;
  15. use Pimcore\Http\Request\Resolver\SiteResolver;
  16. use Pimcore\Routing\Dynamic\DynamicRequestContext;
  17. use Pimcore\Routing\Dynamic\DynamicRouteHandlerInterface;
  18. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  21. use Symfony\Component\Routing\Route;
  22. use Symfony\Component\Routing\RouteCollection;
  23. /**
  24.  * @internal
  25.  */
  26. final class DynamicRouteProvider implements RouteProviderInterface
  27. {
  28.     /**
  29.      * @var SiteResolver
  30.      */
  31.     protected $siteResolver;
  32.     /**
  33.      * @var DynamicRouteHandlerInterface[]
  34.      */
  35.     protected $handlers = [];
  36.     /**
  37.      * @param SiteResolver $siteResolver
  38.      * @param DynamicRouteHandlerInterface[] $handlers
  39.      */
  40.     public function __construct(SiteResolver $siteResolver, array $handlers = [])
  41.     {
  42.         $this->siteResolver $siteResolver;
  43.         foreach ($handlers as $handler) {
  44.             $this->addHandler($handler);
  45.         }
  46.     }
  47.     /**
  48.      * @param DynamicRouteHandlerInterface $handler
  49.      */
  50.     public function addHandler(DynamicRouteHandlerInterface $handler)
  51.     {
  52.         if (!in_array($handler$this->handlerstrue)) {
  53.             $this->handlers[] = $handler;
  54.         }
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getRouteCollectionForRequest(Request $request): RouteCollection
  60.     {
  61.         $collection = new RouteCollection();
  62.         $path $originalPath urldecode($request->getPathInfo());
  63.         // site path handled by FrontendRoutingListener which runs before routing is started
  64.         if (null !== $sitePath $this->siteResolver->getSitePath($request)) {
  65.             $path $sitePath;
  66.         }
  67.         foreach ($this->handlers as $handler) {
  68.             $handler->matchRequest($collection, new DynamicRequestContext($request$path$originalPath));
  69.         }
  70.         return $collection;
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function getRouteByName($name): Route
  76.     {
  77.         foreach ($this->handlers as $handler) {
  78.             try {
  79.                 return $handler->getRouteByName($name);
  80.             } catch (RouteNotFoundException $e) {
  81.                 // noop
  82.             }
  83.         }
  84.         throw new RouteNotFoundException(sprintf("Route for name '%s' was not found"$name));
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function getRoutesByNames($names): array
  90.     {
  91.         // TODO needs performance optimizations
  92.         // TODO really return all routes here as documentation states? where is this used?
  93.         $routes = [];
  94.         if (is_array($names)) {
  95.             foreach ($names as $name) {
  96.                 try {
  97.                     $route $this->getRouteByName($name);
  98.                     $routes[] = $route;
  99.                 } catch (RouteNotFoundException $e) {
  100.                     // noop
  101.                 }
  102.             }
  103.         }
  104.         return $routes;
  105.     }
  106. }