src/Routing/DynamicRouter.php line 46

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\Matcher\RequestMatcherInterface;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Symfony\Component\Routing\Router;
  17. use Symfony\Component\Routing\RouterInterface;
  18. class DynamicRouter implements RouterInterfaceRequestMatcherInterfaceWarmableInterface
  19. {
  20.     /**
  21.      * Префикс названий роутов, которые были переопределены, но старые адреса при этом оставлены
  22.      */
  23.     const OVERRIDDEN_ROUTE_PREFIX '_overridden.';
  24.     /**
  25.      * Суффикс названий роутов для пагинации
  26.      */
  27.     const PAGINATION_ROUTE_POSTFIX '._pagination';
  28.     /**
  29.      * Параметр в настройках переопределенного роута, указывающий на новый роут
  30.      */
  31.     const OVERRIDDEN_ROUTE_PARAMETER '_overridden_route_name';
  32.     public function __construct(
  33.         protected Router $router,
  34.         protected ?EventDispatcherInterface $eventDispatcher null
  35.     ) {}
  36.     /**
  37.      * @inheritDoc
  38.      */
  39.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH): string
  40.     {
  41.         if ($this->eventDispatcher) {
  42.             $event = new RoutingEvent($name$parameters$referenceType);
  43.             $this->eventDispatcher->dispatch($eventRoutingEvents::PRE_GENERATE);
  44.             $name $event->getName();
  45.             $parameters $event->getParameters();
  46.             $referenceType $event->getReferenceType();
  47.         }
  48.         $url $this->router->generate($name$parameters$referenceType);
  49.         if ($this->eventDispatcher) {
  50.             $event = new FilterGeneratedUrlEvent($url$name$parameters$referenceType);
  51.             $this->eventDispatcher->dispatch($eventRoutingEvents::POST_GENERATE);
  52.             $url $event->getUrl();
  53.         }
  54.         return $url;
  55.     }
  56.     /**
  57.      * @inheritDoc
  58.      */
  59.     public function setContext(RequestContext $context): void
  60.     {
  61.         $this->router->setContext($context);
  62.     }
  63.     /**
  64.      * @inheritDoc
  65.      */
  66.     public function getContext(): RequestContext
  67.     {
  68.         return $this->router->getContext();
  69.     }
  70.     /**
  71.      * @inheritDoc
  72.      */
  73.     public function matchRequest(Request $request): array
  74.     {
  75.         return $this->router->matchRequest($request);
  76.     }
  77.     /**
  78.      * @inheritDoc
  79.      */
  80.     public function getRouteCollection()
  81.     {
  82.         return $this->router->getRouteCollection();
  83.     }
  84.     public function warmUp(string $cacheDir): void
  85.     {
  86.     }
  87.     /**
  88.      * @inheritDoc
  89.      */
  90.     public function match($pathinfo): array
  91.     {
  92.         return $this->router->match($pathinfo);
  93.     }
  94. }