|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace nlxNeosContent\Storefront\Controller; |
| 6 | + |
| 7 | +use nlxNeosContent\Service\NeosPageTreeService; |
| 8 | +use Shopware\Core\Framework\Routing\RoutingException; |
| 9 | +use Shopware\Core\Framework\Uuid\Uuid; |
| 10 | +use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; |
| 11 | +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; |
| 12 | +use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService; |
| 13 | +use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface; |
| 14 | +use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters; |
| 15 | +use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute; |
| 16 | +use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute; |
| 17 | +use Shopware\Core\System\SalesChannel\SalesChannelContext; |
| 18 | +use Shopware\Storefront\Controller\ContextController; |
| 19 | +use Shopware\Storefront\Framework\Routing\RequestTransformer; |
| 20 | +use Shopware\Storefront\Framework\Routing\Router; |
| 21 | +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; |
| 22 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 23 | +use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated; |
| 24 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 25 | +use Symfony\Component\HttpFoundation\Request; |
| 26 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 27 | +use Symfony\Component\HttpFoundation\Response; |
| 28 | +use Symfony\Component\Routing\RouterInterface; |
| 29 | + |
| 30 | +#[AsDecorator(ContextController::class)] |
| 31 | +class ContextControllerDecorator extends ContextController |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + #[Autowire(service: ContextSwitchRoute::class)] |
| 35 | + private readonly AbstractContextSwitchRoute $contextSwitchRoute, |
| 36 | + private readonly RequestStack $requestStack, |
| 37 | + private readonly RouterInterface $router, |
| 38 | + #[AutowireDecorated] |
| 39 | + private readonly ContextController $inner, |
| 40 | + private readonly NeosPageTreeService $neosPageTreeService, |
| 41 | + #[Autowire(service: SalesChannelContextService::class)] |
| 42 | + private readonly SalesChannelContextServiceInterface $salesChannelContextService, |
| 43 | + ) { |
| 44 | + parent::__construct( |
| 45 | + $this->contextSwitchRoute, |
| 46 | + $this->requestStack, |
| 47 | + $this->router, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function switchLanguage(Request $request, SalesChannelContext $context): RedirectResponse |
| 52 | + { |
| 53 | + |
| 54 | + if(array_key_exists('neos', $request->request->all('redirectParameters')) && $request->request->all('redirectParameters')['neos'] === "1") |
| 55 | + { |
| 56 | + if (!array_key_exists('navigationId', $request->request->all('redirectParameters'))) { |
| 57 | + throw new \InvalidArgumentException('navigationId is required for neos redirects'); |
| 58 | + } |
| 59 | + |
| 60 | + $languageId = $request->request->get('languageId'); |
| 61 | + if (!$languageId) { |
| 62 | + throw RoutingException::missingRequestParameter('languageId'); |
| 63 | + } |
| 64 | + |
| 65 | + if (!\is_string($languageId) || !Uuid::isValid($languageId)) { |
| 66 | + throw RoutingException::invalidRequestParameter('languageId'); |
| 67 | + } |
| 68 | + |
| 69 | + $newContext = $this->salesChannelContextService->get( |
| 70 | + new SalesChannelContextServiceParameters( |
| 71 | + salesChannelId: $context->getSalesChannelId(), |
| 72 | + token: $context->getToken(), |
| 73 | + languageId: $languageId, |
| 74 | + currencyId: $context->getCurrencyId(), |
| 75 | + domainId: $context->getDomainId(), |
| 76 | + originalContext: $context->getContext(), |
| 77 | + customerId: $context->getCustomer()?->getId(), |
| 78 | + imitatingUserId: $context->getImitatingUserId(), |
| 79 | + ) |
| 80 | + ); |
| 81 | + |
| 82 | + $nodeIdentifier = $request->request->all('redirectParameters')['navigationId']; |
| 83 | + $pathInfo = $this->neosPageTreeService->findPathInfoForIdentifierAndContext($nodeIdentifier, $newContext); |
| 84 | + |
| 85 | + try { |
| 86 | + $newTokenResponse = $this->contextSwitchRoute->switchContext( |
| 87 | + new RequestDataBag([SalesChannelContextService::LANGUAGE_ID => $languageId]), |
| 88 | + $context |
| 89 | + ); |
| 90 | + } catch (ConstraintViolationException) { |
| 91 | + throw RoutingException::languageNotFound($languageId); |
| 92 | + } |
| 93 | + |
| 94 | + $parsedUrl = parse_url($newTokenResponse->getRedirectUrl()); |
| 95 | + |
| 96 | + if (!$parsedUrl) { |
| 97 | + throw RoutingException::languageNotFound($languageId); |
| 98 | + } |
| 99 | + |
| 100 | + $redirectRequest = Request::create($newTokenResponse->getRedirectUrl()); |
| 101 | + |
| 102 | + return new RedirectResponse(rtrim($redirectRequest->getUri(), '/') . '/' . ltrim($pathInfo, '/'), Response::HTTP_FOUND); |
| 103 | + |
| 104 | + } |
| 105 | + return $this->inner->switchLanguage($request, $context); |
| 106 | + } |
| 107 | +} |
0 commit comments