vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Router/LocaleStrippingRouter.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\Router;
  12. use Sylius\Component\Locale\Context\LocaleContextInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RequestContext;
  16. use Symfony\Component\Routing\RouteCollection;
  17. use Symfony\Component\Routing\RouterInterface;
  18. final class LocaleStrippingRouter implements RouterInterfaceWarmableInterface
  19. {
  20.     public function __construct(private RouterInterface $router, private LocaleContextInterface $localeContext)
  21.     {
  22.     }
  23.     public function match($pathinfo): array
  24.     {
  25.         return $this->router->match($pathinfo);
  26.     }
  27.     public function generate($name$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  28.     {
  29.         $url $this->router->generate($name$parameters$referenceType);
  30.         if (!str_contains($url'_locale')) {
  31.             return $url;
  32.         }
  33.         return $this->removeUnusedQueryArgument($url'_locale'$this->localeContext->getLocaleCode());
  34.     }
  35.     public function setContext(RequestContext $context): void
  36.     {
  37.         $this->router->setContext($context);
  38.     }
  39.     public function getContext(): RequestContext
  40.     {
  41.         return $this->router->getContext();
  42.     }
  43.     public function getRouteCollection(): RouteCollection
  44.     {
  45.         return $this->router->getRouteCollection();
  46.     }
  47.     public function warmUp($cacheDir): void
  48.     {
  49.         if ($this->router instanceof WarmableInterface) {
  50.             $this->router->warmUp($cacheDir);
  51.         }
  52.     }
  53.     private function removeUnusedQueryArgument(string $urlstring $keystring $value): string
  54.     {
  55.         $replace = [
  56.             sprintf('&%s=%s'$key$value) => '',
  57.             sprintf('?%s=%s&'$key$value) => '?',
  58.             sprintf('?%s=%s'$key$value) => '',
  59.         ];
  60.         return str_replace(array_keys($replace), $replace$url);
  61.     }
  62. }