src/Routing/DynamicRouter.php line 112

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-07-18
  5.  * Time: 14:15
  6.  */
  7. namespace App\Routing;
  8. use App\Event\Routing\FilterGeneratedUrlEvent;
  9. use App\Event\Routing\RoutingEvent;
  10. use App\Event\Routing\RoutingEvents;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  15. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  16. use Symfony\Component\Routing\RequestContext;
  17. use Symfony\Component\Routing\Router;
  18. use Symfony\Component\Routing\RouterInterface;
  19. class DynamicRouter implements RouterInterfaceRequestMatcherInterfaceWarmableInterface
  20. {
  21.     /**
  22.      * Префикс названий роутов, которые были переопределены, но старые адреса при этом оставлены
  23.      */
  24.     const OVERRIDDEN_ROUTE_PREFIX '_overridden.';
  25.     /**
  26.      * Суффикс названий роутов для пагинации
  27.      */
  28.     const PAGINATION_ROUTE_POSTFIX '._pagination';
  29.     /**
  30.      * Параметр в настройках переопределенного роута, указывающий на новый роут
  31.      */
  32.     const OVERRIDDEN_ROUTE_PARAMETER '_overridden_route_name';
  33.     const DEFAULT_CITY_ROUTE_PREFIX '_default_city.';
  34.     /**
  35.      * Префиксы и суффиксы для модификации названий роутов
  36.      *
  37.      * Для получения оригинального имени роута (canonical route name)
  38.      * из имени текущего роута все модификаторы нужно удалить.
  39.      */
  40.     public static array $routeNameModifiers = [
  41.         self::OVERRIDDEN_ROUTE_PREFIX,
  42.         self::DEFAULT_CITY_ROUTE_PREFIX,
  43.         self::PAGINATION_ROUTE_POSTFIX,
  44.     ];
  45.     public function __construct(
  46.         protected Router $router,
  47.         protected ?EventDispatcherInterface $eventDispatcher null
  48.     ) {}
  49.     /**
  50.      * @inheritDoc
  51.      */
  52.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH): string
  53.     {
  54.         $originalName $name;
  55.         if ($this->eventDispatcher) {
  56.             $event = new RoutingEvent($name$parameters$referenceType);
  57.             $this->eventDispatcher->dispatch($eventRoutingEvents::PRE_GENERATE);
  58.             $name $event->getName();
  59.             $parameters $event->getParameters();
  60.             $referenceType $event->getReferenceType();
  61.         }
  62.         try {
  63.             $url $this->router->generate($name$parameters$referenceType);
  64.         } catch (RouteNotFoundException $e) {
  65.             if (!str_starts_with($nameself::DEFAULT_CITY_ROUTE_PREFIX)) {
  66.                 throw $e;
  67.             }
  68.             $url $this->router->generate($originalName$parameters$referenceType);
  69.         }
  70.         if ($this->eventDispatcher) {
  71.             $event = new FilterGeneratedUrlEvent($url$name$parameters$referenceType);
  72.             $this->eventDispatcher->dispatch($eventRoutingEvents::POST_GENERATE);
  73.             $url $event->getUrl();
  74.         }
  75.         return $url;
  76.     }
  77.     /**
  78.      * @inheritDoc
  79.      */
  80.     public function setContext(RequestContext $context): void
  81.     {
  82.         $this->router->setContext($context);
  83.     }
  84.     /**
  85.      * @inheritDoc
  86.      */
  87.     public function getContext(): RequestContext
  88.     {
  89.         return $this->router->getContext();
  90.     }
  91.     /**
  92.      * @inheritDoc
  93.      */
  94.     public function matchRequest(Request $request): array
  95.     {
  96.         return $this->router->matchRequest($request);
  97.     }
  98.     /**
  99.      * @inheritDoc
  100.      */
  101.     public function getRouteCollection()
  102.     {
  103.         return $this->router->getRouteCollection();
  104.     }
  105.     public function warmUp(string $cacheDir): void
  106.     {
  107.     }
  108.     /**
  109.      * @inheritDoc
  110.      */
  111.     public function match($pathinfo): array
  112.     {
  113.         return $this->router->match($pathinfo);
  114.     }
  115. }