src/Routing/DynamicRouter.php line 100

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.     public function __construct(
  35.         protected Router $router,
  36.         protected ?EventDispatcherInterface $eventDispatcher null
  37.     ) {}
  38.     /**
  39.      * @inheritDoc
  40.      */
  41.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH): string
  42.     {
  43.         $originalName $name;
  44.         if ($this->eventDispatcher) {
  45.             $event = new RoutingEvent($name$parameters$referenceType);
  46.             $this->eventDispatcher->dispatch($eventRoutingEvents::PRE_GENERATE);
  47.             $name $event->getName();
  48.             $parameters $event->getParameters();
  49.             $referenceType $event->getReferenceType();
  50.         }
  51.         try {
  52.             $url $this->router->generate($name$parameters$referenceType);
  53.         } catch (RouteNotFoundException $e) {
  54.             if (!str_starts_with($nameself::DEFAULT_CITY_ROUTE_PREFIX)) {
  55.                 throw $e;
  56.             }
  57.             $url $this->router->generate($originalName$parameters$referenceType);
  58.         }
  59.         if ($this->eventDispatcher) {
  60.             $event = new FilterGeneratedUrlEvent($url$name$parameters$referenceType);
  61.             $this->eventDispatcher->dispatch($eventRoutingEvents::POST_GENERATE);
  62.             $url $event->getUrl();
  63.         }
  64.         return $url;
  65.     }
  66.     /**
  67.      * @inheritDoc
  68.      */
  69.     public function setContext(RequestContext $context): void
  70.     {
  71.         $this->router->setContext($context);
  72.     }
  73.     /**
  74.      * @inheritDoc
  75.      */
  76.     public function getContext(): RequestContext
  77.     {
  78.         return $this->router->getContext();
  79.     }
  80.     /**
  81.      * @inheritDoc
  82.      */
  83.     public function matchRequest(Request $request): array
  84.     {
  85.         return $this->router->matchRequest($request);
  86.     }
  87.     /**
  88.      * @inheritDoc
  89.      */
  90.     public function getRouteCollection()
  91.     {
  92.         return $this->router->getRouteCollection();
  93.     }
  94.     public function warmUp(string $cacheDir): void
  95.     {
  96.     }
  97.     /**
  98.      * @inheritDoc
  99.      */
  100.     public function match($pathinfo): array
  101.     {
  102.         return $this->router->match($pathinfo);
  103.     }
  104. }