vendor/shopware/storefront/Controller/StorefrontController.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  6. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  7. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Event\StorefrontRenderEvent;
  11. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  12. use Shopware\Storefront\Framework\Routing\Router;
  13. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  19. abstract class StorefrontController extends AbstractController
  20. {
  21.     protected function renderStorefront(string $view, array $parameters = []): Response
  22.     {
  23.         $request $this->get('request_stack')->getCurrentRequest();
  24.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  25.         $view $this->get(TemplateFinder::class)->find($viewfalsenull);
  26.         $event = new StorefrontRenderEvent($view$parameters$request$salesChannelContext);
  27.         $this->get('event_dispatcher')->dispatch($event);
  28.         $response $this->render($view$event->getParameters(), new StorefrontResponse());
  29.         if (!$response instanceof StorefrontResponse) {
  30.             throw new \RuntimeException('Symfony render implementation changed. Providing a response is no longer supported');
  31.         }
  32.         $host $request->attributes->get(RequestTransformer::STOREFRONT_URL);
  33.         /** @var SeoUrlPlaceholderHandlerInterface $seoUrlReplacer */
  34.         $seoUrlReplacer $this->container->get(SeoUrlPlaceholderHandlerInterface::class);
  35.         $response->setContent(
  36.             $seoUrlReplacer->replace($response->getContent(), $host$salesChannelContext)
  37.         );
  38.         /* @var StorefrontResponse $response */
  39.         $response->setData($parameters);
  40.         $response->setContext($salesChannelContext);
  41.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'1');
  42.         $response->headers->set('Content-Type''text/html');
  43.         return $response;
  44.     }
  45.     protected function trans(string $snippet, array $parameters = []): string
  46.     {
  47.         return $this->container
  48.             ->get('translator')
  49.             ->trans($snippet$parameters);
  50.     }
  51.     protected function createActionResponse(Request $request): Response
  52.     {
  53.         if ($request->get('redirectTo')) {
  54.             $params $this->decodeParam($request'redirectParameters');
  55.             return $this->redirectToRoute($request->get('redirectTo'), $params);
  56.         }
  57.         if ($request->get('forwardTo')) {
  58.             $params $this->decodeParam($request'forwardParameters');
  59.             return $this->forwardToRoute($request->get('forwardTo'), [], $params);
  60.         }
  61.         return new Response();
  62.     }
  63.     protected function forwardToRoute(string $routeName, array $attributes = [], array $routeParameters = []): Response
  64.     {
  65.         $router $this->container->get('router');
  66.         $url $this->generateUrl($routeName$routeParametersRouter::PATH_INFO);
  67.         // for the route matching the request method is set to "GET" because
  68.         // this method is not ought to be used as a post passthrough
  69.         // rather it shall return templates or redirects to display results of the request ahead
  70.         $method $router->getContext()->getMethod();
  71.         $router->getContext()->setMethod(Request::METHOD_GET);
  72.         $route $router->match($url);
  73.         $router->getContext()->setMethod($method);
  74.         $request $this->container->get('request_stack')->getCurrentRequest();
  75.         $attributes array_merge(
  76.             $this->get(RequestTransformerInterface::class)->extractInheritableAttributes($request),
  77.             $route,
  78.             $attributes,
  79.             ['_route_params' => $routeParameters]
  80.         );
  81.         return $this->forward($route['_controller'], $attributes$routeParameters);
  82.     }
  83.     /**
  84.      * @deprecated tag:v6.4.0 - use annotation `LoginRequired` instead
  85.      *
  86.      * @throws CustomerNotLoggedInException
  87.      */
  88.     protected function denyAccessUnlessLoggedIn(bool $allowGuest false): void
  89.     {
  90.         /** @var RequestStack $requestStack */
  91.         $requestStack $this->get('request_stack');
  92.         $request $requestStack->getCurrentRequest();
  93.         if (!$request) {
  94.             throw new CustomerNotLoggedInException();
  95.         }
  96.         /** @var SalesChannelContext|null $context */
  97.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  98.         if (
  99.             $context
  100.             && $context->getCustomer()
  101.             && (
  102.                 $allowGuest === true
  103.                 || $context->getCustomer()->getGuest() === false
  104.             )
  105.         ) {
  106.             return;
  107.         }
  108.         throw new CustomerNotLoggedInException();
  109.     }
  110.     protected function decodeParam(Request $requeststring $param): array
  111.     {
  112.         $params $request->get($param);
  113.         if (\is_string($params)) {
  114.             $params json_decode($paramstrue);
  115.         }
  116.         if (empty($params)) {
  117.             $params = [];
  118.         }
  119.         return $params;
  120.     }
  121.     protected function addCartErrors(Cart $cart): void
  122.     {
  123.         $groups = [
  124.             'info' => $cart->getErrors()->getNotices(),
  125.             'warning' => $cart->getErrors()->getWarnings(),
  126.             'danger' => $cart->getErrors()->getErrors(),
  127.         ];
  128.         foreach ($groups as $type => $errors) {
  129.             foreach ($errors as $error) {
  130.                 $parameters = [];
  131.                 foreach ($error->getParameters() as $key => $value) {
  132.                     $parameters['%' $key '%'] = $value;
  133.                 }
  134.                 $message $this->trans('checkout.' $error->getMessageKey(), $parameters);
  135.                 $this->addFlash($type$message);
  136.             }
  137.         }
  138.     }
  139. }