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

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.     /** @var RouterInterface */
  21.     private $router;
  22.     /** @var LocaleContextInterface */
  23.     private $localeContext;
  24.     public function __construct(RouterInterface $routerLocaleContextInterface $localeContext)
  25.     {
  26.         $this->router $router;
  27.         $this->localeContext $localeContext;
  28.     }
  29.     public function match($pathinfo): array
  30.     {
  31.         return $this->router->match($pathinfo);
  32.     }
  33.     public function generate($name$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  34.     {
  35.         $url $this->router->generate($name$parameters$referenceType);
  36.         if (false === strpos($url'_locale')) {
  37.             return $url;
  38.         }
  39.         return $this->removeUnusedQueryArgument($url'_locale'$this->localeContext->getLocaleCode());
  40.     }
  41.     public function setContext(RequestContext $context): void
  42.     {
  43.         $this->router->setContext($context);
  44.     }
  45.     public function getContext(): RequestContext
  46.     {
  47.         return $this->router->getContext();
  48.     }
  49.     public function getRouteCollection(): RouteCollection
  50.     {
  51.         return $this->router->getRouteCollection();
  52.     }
  53.     public function warmUp($cacheDir): void
  54.     {
  55.         if ($this->router instanceof WarmableInterface) {
  56.             $this->router->warmUp($cacheDir);
  57.         }
  58.     }
  59.     private function removeUnusedQueryArgument(string $urlstring $keystring $value): string
  60.     {
  61.         $replace = [
  62.             sprintf('&%s=%s'$key$value) => '',
  63.             sprintf('?%s=%s&'$key$value) => '?',
  64.             sprintf('?%s=%s'$key$value) => '',
  65.         ];
  66.         return str_replace(array_keys($replace), $replace$url);
  67.     }
  68. }