vendor/pgs-soft/hashid-bundle/src/Decorator/RouterDecorator.php line 93

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Pgs\HashIdBundle\Decorator;
  4. use Pgs\HashIdBundle\ParametersProcessor\Factory\EncodeParametersProcessorFactory;
  5. use Pgs\HashIdBundle\Traits\DecoratorTrait;
  6. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  7. use Symfony\Component\Routing\RequestContext;
  8. use Symfony\Component\Routing\Route;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class RouterDecorator implements RouterInterfaceWarmableInterface
  11. {
  12.     use DecoratorTrait;
  13.     protected $parametersProcessorFactory;
  14.     public function __construct(RouterInterface $routerEncodeParametersProcessorFactory $parametersProcessorFactory)
  15.     {
  16.         $this->object $router;
  17.         $this->parametersProcessorFactory $parametersProcessorFactory;
  18.     }
  19.     public function getRouter(): RouterInterface
  20.     {
  21.         return $this->object;
  22.     }
  23.     public function generate(
  24.         string $name,
  25.         array $parameters = [],
  26.         int $referenceType RouterInterface::ABSOLUTE_PATH
  27.     ): string {
  28.         $this->processParameters($this->getRoute($name$parameters), $parameters);
  29.         return $this->getRouter()->generate($name$parameters$referenceType);
  30.     }
  31.     private function getRoute(string $name, array $parameters): ?Route
  32.     {
  33.         $routeCollection $this->getRouter()->getRouteCollection();
  34.         $route $routeCollection->get($name);
  35.         if (null === $route) {
  36.             $locale $parameters['_locale'] ?? $this->getRouter()->getContext()->getParameter('_locale');
  37.             $route $routeCollection->get(sprintf('%s.%s'$name$locale));
  38.         }
  39.         return $route;
  40.     }
  41.     private function processParameters(?Route $route, array &$parameters): void
  42.     {
  43.         if (null !== $route) {
  44.             $parametersProcessor $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route);
  45.             if ($parametersProcessor->needToProcess()) {
  46.                 $parameters $parametersProcessor->process($parameters);
  47.             }
  48.         }
  49.     }
  50.     /**
  51.      * @codeCoverageIgnore
  52.      */
  53.     public function setContext(RequestContext $context)
  54.     {
  55.         $this->getRouter()->setContext($context);
  56.     }
  57.     /**
  58.      * @codeCoverageIgnore
  59.      */
  60.     public function getContext()
  61.     {
  62.         return $this->getRouter()->getContext();
  63.     }
  64.     /**
  65.      * @codeCoverageIgnore
  66.      */
  67.     public function getRouteCollection()
  68.     {
  69.         return $this->getRouter()->getRouteCollection();
  70.     }
  71.     /**
  72.      * @codeCoverageIgnore
  73.      */
  74.     public function match(string $pathinfo)
  75.     {
  76.         return $this->getRouter()->match($pathinfo);
  77.     }
  78.     /**
  79.      * @codeCoverageIgnore
  80.      */
  81.     public function warmUp($cacheDir)
  82.     {
  83.         if ($this->getRouter() instanceof WarmableInterface) {
  84.             $this->getRouter()->warmUp($cacheDir);
  85.         }
  86.     }
  87. }