src/Hitso/Bundle/RoutingBundle/Provider/RouteProvider.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\RoutingBundle\Provider;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Persistence\ManagerRegistry;
  6. use Doctrine\Common\Util\ClassUtils;
  7. use Doctrine\Common\Util\Debug;
  8. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection;
  9. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
  10. use Hitso\Bundle\RoutingBundle\Entity\Route;
  11. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Route as SymfonyRoute;
  14. use Symfony\Component\Routing\RouteCollection;
  15. class RouteProvider implements RouteProviderInterface
  16. {
  17.     const DYNAMIC_PREFIX        'dynamic_';
  18.     const PATH_PARAMS_SEPARATOR ',';
  19.     /**
  20.      * @var ManagerRegistry
  21.      */
  22.     private $managerRegistry;
  23.     /**
  24.      * @var \Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection
  25.      */
  26.     private $sites;
  27.     /**
  28.      * @var array
  29.      */
  30.     private $routingGeneratorMap;
  31.     /**
  32.      * @var SiteContext
  33.      */
  34.     private $context;
  35.     public function __construct(
  36.         ManagerRegistry $managerRegistry,
  37.         array $routingGeneratorMap = [],
  38.         SiteCollection $sites,
  39.         SiteContext $context
  40.     ) {
  41.         $this->managerRegistry     $managerRegistry;
  42.         $this->routingGeneratorMap $routingGeneratorMap;
  43.         $this->sites               $sites;
  44.         $this->context             $context;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function getRouteCollectionForRequest(Request $request)
  50.     {
  51.         $collection = new RouteCollection();
  52.         $repository $this->getRouteRepository();
  53.         $path       $this->getNormalizedPath($request);
  54.         $siteId     $this->context->getContentSite()->getId();
  55.         $resource   $repository->findOneBy(['path' => $path'siteId' => $siteId]);
  56.         if ($resource instanceof Route) {
  57.             $route $this->createRoute($resource);
  58.             $collection->add(
  59.                 self::DYNAMIC_PREFIX $resource->getId(),
  60.                 $route
  61.             );
  62.         }
  63.         return $collection;
  64.     }
  65.     private function createRoute(Route $resource): SymfonyRoute
  66.     {
  67.         $settings                        $this->getRouteGenerationSettings($resource);
  68.         $settings['defaults']['id']      = $resource->getIdentifier()->getId();
  69.         $site                            $this->sites->get($resource->getSiteId());
  70.         $settings['defaults']['_locale'] = $site->getLocale();
  71.         return new SymfonyRoute(
  72.             $this->getPath($resource$settings['pattern']),
  73.             $settings['defaults'],
  74.             $settings['requirements'],
  75.             $settings['options'],
  76.             $site->getHost()
  77.         );
  78.     }
  79.     public function getRouteByName($identifier)
  80.     {
  81.         $id       str_replace(self::DYNAMIC_PREFIX''$identifier);
  82.         $resource $this->getRouteRepository()->find($id);
  83.         if ($resource instanceof Route) {
  84.             return $this->createRoute($resource);
  85.         }
  86.         return null;
  87.     }
  88.     public function getRoutesByNames($names$parameters = [])
  89.     {
  90.         return new ArrayCollection();
  91.     }
  92.     protected function getRouteRepository()
  93.     {
  94.         return $this->managerRegistry->getRepository(Route::class);
  95.     }
  96.     private function getRouteGenerationSettings(Route $resource): array
  97.     {
  98.         $class ClassUtils::getRealClass(get_class($resource));
  99.         if (!isset($this->routingGeneratorMap[$class])) {
  100.             throw new \InvalidArgumentException(
  101.                 sprintf('Route resource of type "%s" has invalid/missing configuration.'$class)
  102.             );
  103.         }
  104.         return $this->routingGeneratorMap[$class];
  105.     }
  106.     private function getNormalizedPath(Request $request)
  107.     {
  108.         $path  ltrim($request->getPathInfo(), '/');
  109.         $paths explode(self::PATH_PARAMS_SEPARATOR$path);
  110.         return current($paths);
  111.     }
  112.     private function getPath(Route $resourcestring $pattern): string
  113.     {
  114.         if (strlen($pattern)) {
  115.             return $resource->getPath() . self::PATH_PARAMS_SEPARATOR $pattern;
  116.         }
  117.         return $resource->getPath();
  118.     }
  119. }