lib/composer/RoutingBundle/src/VisualMedia/RoutingBundle/Service/Router.php line 119

Open in your IDE?
  1. <?php
  2. /**
  3.  * Router
  4.  *
  5.  * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6.  */
  7. namespace VisualMedia\RoutingBundle\Service;
  8. use Exception;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Symfony\Component\Routing\Router as SymfonyRouter;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\Routing\RouteCollection;
  17. use VisualMedia\LisaBundle\Component\ManagerData;
  18. use VisualMedia\RoutingBundle\Controller\RedirectController;
  19. use VisualMedia\RoutingBundle\Entity\BaseRedirect;
  20. use VisualMedia\RoutingBundle\Entity\BaseRoute;
  21. use VisualMedia\TranslationBundle\Service\ChannelProvider;
  22. /**
  23.  * Router
  24.  */
  25. class Router implements RouterInterfaceWarmableInterface
  26. {
  27.     /**
  28.      * @var RequestStack
  29.      */
  30.     protected $requestStack;
  31.     /**
  32.      * @var RouterInterface
  33.      */
  34.     protected $router;
  35.     /**
  36.      * @var EntityManagerInterface
  37.      */
  38.     protected $manager;
  39.     /**
  40.      * @var ChannelProvider
  41.      */
  42.     protected $channelProvider;
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function __construct(RequestStack $requestStackRouterInterface $routerEntityManagerInterface $manager)
  47.     {
  48.         $this->requestStack $requestStack;
  49.         $this->router $router;
  50.         $this->manager $manager;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function initialize(ChannelProvider $channelProvider)
  56.     {
  57.         $this->channelProvider $channelProvider;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function warmup(string $cacheDir)
  63.     {
  64.         // ...
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function getRouteCollection()
  70.     {
  71.         return $this->router->getRouteCollection();
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      */
  76.     public function getContext()
  77.     {
  78.         return $this->router->getContext();
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function setContext(RequestContext $context)
  84.     {
  85.         $this->router->setContext($context);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function generate(string $name, array $parameters = [], int $referenceType self::ABSOLUTE_PATH)
  91.     {
  92.         try {
  93.             return $this->router->generate($name$parameters$referenceType);
  94.         }
  95.         catch (Exception $exception) {
  96.             return '#';
  97.         }
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function match($pathinfo)
  103.     {
  104.         // Match non-fallback system routes.
  105.         try {
  106.             if (is_array($match $this->router->match($pathinfo))) {
  107.                 $collection $this->router->getRouteCollection();
  108.                 if (null !== $route $collection->get($match['_route'])) {
  109.                     if (!$route->getOption('fallback')) {
  110.                         return $match;
  111.                     }
  112.                 }
  113.             }
  114.         } catch (Exception $exception) {}
  115.         // Match database routes.
  116.         $locales = [];
  117.         if (null !== $domain $this->channelProvider->getCurrentDomain()) {
  118.             foreach ($domain->locales as $locale) {
  119.                 $locales[] = $locale->code;
  120.             }
  121.             if (!empty($locales)) {
  122.                 $request $this->requestStack->getMasterRequest();
  123.                 $manager $this->manager->getRepository(BaseRoute::class);
  124.                 $route $manager->getFirst(new ManagerData([
  125.                     'url' => $pathinfo,
  126.                     'locales' => $locales,
  127.                     'active' => true,
  128.                 ]));
  129.                 if ($route) {
  130.                     if ($request->getLocale() !== $locale $route->locale->code) {
  131.                         $request->setLocale($locale);
  132.                     }
  133.                     $data array_merge([
  134.                         '_route' => $route->name,
  135.                         '_controller' => $route->getController(),
  136.                     ], $route->getParameters());
  137.                     return $data;
  138.                 }
  139.             }
  140.         }
  141.         // Match redirects.
  142.         $manager $this->manager->getRepository(BaseRedirect::class);
  143.         $redirect $manager->getFirst(new ManagerData([
  144.             'urlFrom' => $pathinfo,
  145.             'active' => true,
  146.         ]));
  147.         if ($redirect) {
  148.             return [
  149.                 '_route' => 'redirect',
  150.                 '_controller' => sprintf('%s::%s'RedirectController::class, 'redirectAction'),
  151.                 'url' => $redirect->urlTo,
  152.                 'code' => $redirect->type,
  153.             ];
  154.         }
  155.         // Match fallback system routes.
  156.         return $this->router->match($pathinfo);
  157.     }
  158.     /**
  159.      * {@inheritdoc}
  160.      */
  161.     public function getOption($option)
  162.     {
  163.         return $this->router->getOption($option);
  164.     }
  165. }