vendor/shopware/storefront/Framework/Routing/Router.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Routing;
  3. use Shopware\Core\PlatformRequest;
  4. use Symfony\Bundle\FrameworkBundle\Routing\Router as SymfonyRouter;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  8. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  9. use Symfony\Component\Routing\RequestContext;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  12. class Router implements RouterInterfaceRequestMatcherInterfaceWarmableInterfaceServiceSubscriberInterface
  13. {
  14.     /** @var int Used to indicate the router that we only need the path info without the sales channel prefix */
  15.     public const PATH_INFO 10;
  16.     /**
  17.      * @var SymfonyRouter
  18.      */
  19.     private $decorated;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     private $requestStack;
  24.     public function __construct(SymfonyRouter $decoratedRequestStack $requestStack)
  25.     {
  26.         $this->decorated $decorated;
  27.         $this->requestStack $requestStack;
  28.     }
  29.     public static function getSubscribedServices()
  30.     {
  31.         return SymfonyRouter::getSubscribedServices();
  32.     }
  33.     public function warmUp($cacheDir)
  34.     {
  35.         return $this->decorated->warmUp($cacheDir);
  36.     }
  37.     public function matchRequest(Request $request)
  38.     {
  39.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID)) {
  40.             return $this->decorated->matchRequest($request);
  41.         }
  42.         $server array_merge(
  43.             $request->server->all(),
  44.             ['REQUEST_URI' => $request->attributes->get(RequestTransformer::SALES_CHANNEL_RESOLVED_URI)]
  45.         );
  46.         $localClone $request->duplicate(nullnullnullnullnull$server);
  47.         return $this->decorated->matchRequest($localClone);
  48.     }
  49.     public function setContext(RequestContext $context)
  50.     {
  51.         return $this->decorated->setContext($context);
  52.     }
  53.     public function getContext()
  54.     {
  55.         return $this->decorated->getContext();
  56.     }
  57.     public function getRouteCollection()
  58.     {
  59.         return $this->decorated->getRouteCollection();
  60.     }
  61.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH)
  62.     {
  63.         $basePath $this->getBasePath();
  64.         if ($referenceType === self::PATH_INFO) {
  65.             $route $this->decorated->generate($name$parametersself::ABSOLUTE_PATH);
  66.             return $this->removePrefix($route$basePath);
  67.         }
  68.         if (!$this->isStorefrontRoute($name)) {
  69.             return $this->decorated->generate($name$parameters$referenceType);
  70.         }
  71.         $salesChannelBaseUrl $this->getSalesChannelBaseUrl();
  72.         // we need to insert the sales channel base url between the baseUrl and the infoPath
  73.         switch ($referenceType) {
  74.             case self::NETWORK_PATH:
  75.             case self::ABSOLUTE_URL:
  76.                 $schema '';
  77.                 if ($referenceType === self::ABSOLUTE_URL) {
  78.                     $schema $this->getContext()->getScheme() . ':';
  79.                 }
  80.                 $schemaAuthority $schema '//' $this->getContext()->getHost();
  81.                 if ($this->getContext()->getHttpPort() !== 80) {
  82.                     $schemaAuthority .= ':' $this->getContext()->getHttpPort();
  83.                 } elseif ($this->getContext()->getHttpsPort() !== 443) {
  84.                     $schemaAuthority .= ':' $this->getContext()->getHttpsPort();
  85.                 }
  86.                 $generated $this->decorated->generate($name$parameters);
  87.                 $pathInfo $this->removePrefix($generated$basePath);
  88.                 $rewrite $schemaAuthority rtrim($basePath'/') . rtrim($salesChannelBaseUrl'/') . $pathInfo;
  89.                 break;
  90.             case self::RELATIVE_PATH:
  91.                 // remove base path from generated url (/shopware/public or /)
  92.                 $generated $this->removePrefix(
  93.                     $this->decorated->generate($name$parametersself::RELATIVE_PATH),
  94.                     $basePath
  95.                 );
  96.                 // url contains the base path and the base url
  97.                     // base url /shopware/public/de
  98.                 $rewrite ltrim($salesChannelBaseUrl'/') . $generated;
  99.                 break;
  100.             case self::ABSOLUTE_PATH:
  101.             default:
  102.                 $generated $this->removePrefix(
  103.                     $this->decorated->generate($name$parameters),
  104.                     $basePath
  105.                 );
  106.                 $rewrite $basePath rtrim($salesChannelBaseUrl'/') . $generated;
  107.                 break;
  108.         }
  109.         return $rewrite;
  110.     }
  111.     public function match($pathinfo)
  112.     {
  113.         return $this->decorated->match($pathinfo);
  114.     }
  115.     private function removePrefix(string $subjectstring $prefix): string
  116.     {
  117.         if (!$prefix || mb_strpos($subject$prefix) !== 0) {
  118.             return $subject;
  119.         }
  120.         return mb_substr($subjectmb_strlen($prefix));
  121.     }
  122.     private function getSalesChannelBaseUrl(): string
  123.     {
  124.         $request $this->requestStack->getMasterRequest();
  125.         if (!$request) {
  126.             return '';
  127.         }
  128.         $url = (string) $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  129.         if (empty($url)) {
  130.             return $url;
  131.         }
  132.         return '/' trim($url'/') . '/';
  133.     }
  134.     private function getBasePath(): string
  135.     {
  136.         $request $this->requestStack->getMasterRequest();
  137.         if (!$request) {
  138.             return '';
  139.         }
  140.         return $request->getBasePath();
  141.     }
  142.     private function isStorefrontRoute(string $name): bool
  143.     {
  144.         return strncmp($name'frontend.'9) === 0
  145.             || strncmp($name'widgets.'8) === 0
  146.             || strncmp($name'payment.'8) === 0;
  147.     }
  148. }