vendor/shopware/storefront/Controller/ContextController.php line 73

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeLanguageRoute;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Shopware\Core\Framework\Routing\Annotation\Since;
  6. use Shopware\Core\Framework\Routing\Exception\LanguageNotFoundException;
  7. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  10. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  11. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  14. use Shopware\Storefront\Framework\Routing\Router;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Routing\RouterInterface;
  21. /**
  22.  * @Route(defaults={"_routeScope"={"storefront"}})
  23.  *
  24.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  25.  */
  26. #[Package('storefront')]
  27. class ContextController extends StorefrontController
  28. {
  29.     private AbstractContextSwitchRoute $contextSwitchRoute;
  30.     private RequestStack $requestStack;
  31.     private RouterInterface $router;
  32.     /**
  33.      * @deprecated tag:v6.5.0 - $changeLanguageRoute will be removed
  34.      */
  35.     private AbstractChangeLanguageRoute $changeLanguageRoute;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         AbstractContextSwitchRoute $contextSwitchRoute,
  41.         RequestStack $requestStack,
  42.         RouterInterface $router,
  43.         AbstractChangeLanguageRoute $changeLanguageRoute
  44.     ) {
  45.         $this->contextSwitchRoute $contextSwitchRoute;
  46.         $this->requestStack $requestStack;
  47.         $this->router $router;
  48.         $this->changeLanguageRoute $changeLanguageRoute;
  49.     }
  50.     /**
  51.      * @Since("6.0.0.0")
  52.      * @Route("/checkout/configure", name="frontend.checkout.configure", methods={"POST"}, options={"seo"="false"}, defaults={"XmlHttpRequest": true})
  53.      */
  54.     public function configure(Request $requestRequestDataBag $dataSalesChannelContext $context): Response
  55.     {
  56.         $this->contextSwitchRoute->switchContext($data$context);
  57.         return $this->createActionResponse($request);
  58.     }
  59.     /**
  60.      * @Since("6.0.0.0")
  61.      * @Route("/checkout/language", name="frontend.checkout.switch-language", methods={"POST"})
  62.      */
  63.     public function switchLanguage(Request $requestSalesChannelContext $context): RedirectResponse
  64.     {
  65.         if (!$request->request->has('languageId')) {
  66.             throw new MissingRequestParameterException('languageId');
  67.         }
  68.         $languageId $request->request->get('languageId');
  69.         try {
  70.             $newTokenResponse $this->contextSwitchRoute->switchContext(
  71.                 new RequestDataBag([SalesChannelContextService::LANGUAGE_ID => $languageId]),
  72.                 $context
  73.             );
  74.         } catch (ConstraintViolationException $e) {
  75.             throw new LanguageNotFoundException($languageId);
  76.         }
  77.         /** @deprecated tag:v6.5.0 - The automatic change of the customer language will be removed - NEXT-22283 */
  78.         if ($context->getCustomer()) {
  79.             $this->changeLanguageRoute->change(
  80.                 new RequestDataBag(
  81.                     [
  82.                         'id' => $context->getCustomer()->getId(),
  83.                         'languageId' => $languageId,
  84.                     ]
  85.                 ),
  86.                 $context,
  87.                 $context->getCustomer()
  88.             );
  89.         }
  90.         $route = (string) $request->request->get('redirectTo''frontend.home.page');
  91.         if (empty($route)) {
  92.             $route 'frontend.home.page';
  93.         }
  94.         $params $request->request->get('redirectParameters''[]');
  95.         if (\is_string($params)) {
  96.             $params json_decode($paramstrue);
  97.         }
  98.         if ($newTokenResponse->getRedirectUrl() === null) {
  99.             return $this->redirectToRoute($route$params);
  100.         }
  101.         /*
  102.          * possible domains
  103.          *
  104.          * http://shopware.de/de
  105.          * http://shopware.de/en
  106.          * http://shopware.de/fr
  107.          *
  108.          * http://shopware.fr
  109.          * http://shopware.com
  110.          * http://shopware.de
  111.          *
  112.          * http://color.com
  113.          * http://farben.de
  114.          * http://couleurs.fr
  115.          *
  116.          * http://localhost/development/public/de
  117.          * http://localhost/development/public/en
  118.          * http://localhost/development/public/fr
  119.          * http://localhost/development/public/de-DE
  120.          *
  121.          * http://localhost:8080
  122.          * http://localhost:8080/en
  123.          * http://localhost:8080/fr
  124.          * http://localhost:8080/de-DE
  125.          */
  126.         $parsedUrl parse_url($newTokenResponse->getRedirectUrl());
  127.         if (!$parsedUrl) {
  128.             throw new LanguageNotFoundException($languageId);
  129.         }
  130.         $routerContext $this->router->getContext();
  131.         $routerContext->setHttpPort($parsedUrl['port'] ?? 80);
  132.         $routerContext->setMethod('GET');
  133.         $routerContext->setHost($parsedUrl['host']);
  134.         $routerContext->setBaseUrl(rtrim($parsedUrl['path'] ?? '''/'));
  135.         if ($this->requestStack->getMainRequest()) {
  136.             $this->requestStack->getMainRequest()
  137.                 ->attributes->set(RequestTransformer::SALES_CHANNEL_BASE_URL'');
  138.         }
  139.         $url $this->router->generate($route$paramsRouter::ABSOLUTE_URL);
  140.         return new RedirectResponse($url);
  141.     }
  142. }