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

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