vendor/shopware/platform/src/Storefront/Controller/StorefrontController.php line 26

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