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

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 Enterprise License (PEL)
  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 PEL
  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\RouteCollection;
  22. class DynamicRouteProvider implements RouteProviderInterface
  23. {
  24.     /**
  25.      * @var SiteResolver
  26.      */
  27.     protected $siteResolver;
  28.     /**
  29.      * @var DynamicRouteHandlerInterface[]
  30.      */
  31.     protected $handlers = [];
  32.     /**
  33.      * @param SiteResolver $siteResolver
  34.      * @param DynamicRouteHandlerInterface[] $handlers
  35.      */
  36.     public function __construct(SiteResolver $siteResolver, array $handlers = [])
  37.     {
  38.         $this->siteResolver $siteResolver;
  39.         foreach ($handlers as $handler) {
  40.             $this->addHandler($handler);
  41.         }
  42.     }
  43.     /**
  44.      * @param DynamicRouteHandlerInterface $handler
  45.      */
  46.     public function addHandler(DynamicRouteHandlerInterface $handler)
  47.     {
  48.         if (!in_array($handler$this->handlerstrue)) {
  49.             $this->handlers[] = $handler;
  50.         }
  51.     }
  52.     /**
  53.      * @inheritdoc
  54.      */
  55.     public function getRouteCollectionForRequest(Request $request)
  56.     {
  57.         $collection = new RouteCollection();
  58.         $path $originalPath urldecode($request->getPathInfo());
  59.         // site path handled by FrontendRoutingListener which runs before routing is started
  60.         if (null !== $sitePath $this->siteResolver->getSitePath($request)) {
  61.             $path $sitePath;
  62.         }
  63.         foreach ($this->handlers as $handler) {
  64.             $handler->matchRequest($collection, new DynamicRequestContext($request$path$originalPath));
  65.         }
  66.         return $collection;
  67.     }
  68.     /**
  69.      * @inheritdoc
  70.      */
  71.     public function getRouteByName($name)
  72.     {
  73.         foreach ($this->handlers as $handler) {
  74.             try {
  75.                 return $handler->getRouteByName($name);
  76.             } catch (RouteNotFoundException $e) {
  77.                 // noop
  78.             }
  79.         }
  80.         throw new RouteNotFoundException(sprintf("Route for name '%s' was not found"$name));
  81.     }
  82.     /**
  83.      * @inheritdoc
  84.      */
  85.     public function getRoutesByNames($names)
  86.     {
  87.         // TODO needs performance optimizations
  88.         // TODO really return all routes here as documentation states? where is this used?
  89.         $routes = [];
  90.         if (is_array($names)) {
  91.             foreach ($names as $name) {
  92.                 try {
  93.                     $route $this->getRouteByName($name);
  94.                     if ($route) {
  95.                         $routes[] = $route;
  96.                     }
  97.                 } catch (RouteNotFoundException $e) {
  98.                     // noop
  99.                 }
  100.             }
  101.         }
  102.         return $routes;
  103.     }
  104. }