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

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\Error\Error;
  5. use Shopware\Core\Checkout\Cart\Error\ErrorRoute;
  6. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  7. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Routing\RequestTransformerInterface;
  10. use Shopware\Core\Framework\Script\Execution\Hook;
  11. use Shopware\Core\Framework\Script\Execution\ScriptExecutor;
  12. use Shopware\Core\PlatformRequest;
  13. use Shopware\Core\Profiling\Profiler;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Event\StorefrontRenderEvent;
  16. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  17. use Shopware\Storefront\Framework\Routing\Router;
  18. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  19. use Shopware\Storefront\Framework\Twig\Extension\IconCacheTwigFilter;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  24. use Twig\Environment;
  25. #[Package('storefront')]
  26. abstract class StorefrontController extends AbstractController
  27. {
  28.     public const SUCCESS 'success';
  29.     public const DANGER 'danger';
  30.     public const INFO 'info';
  31.     public const WARNING 'warning';
  32.     private Environment $twig;
  33.     public function setTwig(Environment $twig): void
  34.     {
  35.         $this->twig $twig;
  36.     }
  37.     /**
  38.      * @param array<string, mixed> $parameters
  39.      */
  40.     protected function renderStorefront(string $view, array $parameters = []): Response
  41.     {
  42.         $request $this->container->get('request_stack')->getCurrentRequest();
  43.         if ($request === null) {
  44.             $request = new Request();
  45.         }
  46.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  47.         $event = new StorefrontRenderEvent($view$parameters$request$salesChannelContext);
  48.         $this->container->get('event_dispatcher')->dispatch($event);
  49.         $iconCacheEnabled $this->getSystemConfigService()->get('core.storefrontSettings.iconCache') ?? true;
  50.         if ($iconCacheEnabled) {
  51.             IconCacheTwigFilter::enable();
  52.         }
  53.         $response Profiler::trace('twig-rendering', fn () => $this->render($view$event->getParameters(), new StorefrontResponse()));
  54.         if ($iconCacheEnabled) {
  55.             IconCacheTwigFilter::disable();
  56.         }
  57.         if (!$response instanceof StorefrontResponse) {
  58.             throw new \RuntimeException('Symfony render implementation changed. Providing a response is no longer supported');
  59.         }
  60.         $host $request->attributes->get(RequestTransformer::STOREFRONT_URL);
  61.         $seoUrlReplacer $this->container->get(SeoUrlPlaceholderHandlerInterface::class);
  62.         $content $response->getContent();
  63.         if ($content !== false) {
  64.             $response->setContent(
  65.                 $seoUrlReplacer->replace($content$host$salesChannelContext)
  66.             );
  67.         }
  68.         $response->setData($parameters);
  69.         $response->setContext($salesChannelContext);
  70.         $response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER'1');
  71.         $response->headers->set('Content-Type''text/html');
  72.         return $response;
  73.     }
  74.     /**
  75.      * @param array<string, mixed> $parameters
  76.      */
  77.     protected function trans(string $snippet, array $parameters = []): string
  78.     {
  79.         return $this->container
  80.             ->get('translator')
  81.             ->trans($snippet$parameters);
  82.     }
  83.     protected function createActionResponse(Request $request): Response
  84.     {
  85.         if ($request->get('redirectTo') || $request->get('redirectTo') === '') {
  86.             $params $this->decodeParam($request'redirectParameters');
  87.             $redirectTo $request->get('redirectTo');
  88.             if ($redirectTo) {
  89.                 return $this->redirectToRoute($redirectTo$params);
  90.             }
  91.             return $this->redirectToRoute('frontend.home.page'$params);
  92.         }
  93.         if ($request->get('forwardTo')) {
  94.             $params $this->decodeParam($request'forwardParameters');
  95.             return $this->forwardToRoute($request->get('forwardTo'), [], $params);
  96.         }
  97.         return new Response();
  98.     }
  99.     /**
  100.      * @param array<string, mixed> $attributes
  101.      * @param array<string, mixed> $routeParameters
  102.      */
  103.     protected function forwardToRoute(string $routeName, array $attributes = [], array $routeParameters = []): Response
  104.     {
  105.         $router $this->container->get('router');
  106.         $url $this->generateUrl($routeName$routeParametersRouter::PATH_INFO);
  107.         // for the route matching the request method is set to "GET" because
  108.         // this method is not ought to be used as a post passthrough
  109.         // rather it shall return templates or redirects to display results of the request ahead
  110.         $method $router->getContext()->getMethod();
  111.         $router->getContext()->setMethod(Request::METHOD_GET);
  112.         $route $router->match($url);
  113.         $router->getContext()->setMethod($method);
  114.         $request $this->container->get('request_stack')->getCurrentRequest();
  115.         if ($request === null) {
  116.             $request = new Request();
  117.         }
  118.         $attributes array_merge(
  119.             $this->container->get(RequestTransformerInterface::class)->extractInheritableAttributes($request),
  120.             $route,
  121.             $attributes,
  122.             ['_route_params' => $routeParameters]
  123.         );
  124.         return $this->forward($route['_controller'], $attributes$routeParameters);
  125.     }
  126.     /**
  127.      * @return array<string, mixed>
  128.      */
  129.     protected function decodeParam(Request $requeststring $param): array
  130.     {
  131.         $params $request->get($param);
  132.         if (\is_string($params)) {
  133.             $params json_decode($paramstrue);
  134.         }
  135.         if (empty($params)) {
  136.             $params = [];
  137.         }
  138.         return $params;
  139.     }
  140.     protected function addCartErrors(Cart $cart, ?\Closure $filter null): void
  141.     {
  142.         $errors $cart->getErrors();
  143.         if ($filter !== null) {
  144.             $errors $errors->filter($filter);
  145.         }
  146.         $groups = [
  147.             'info' => $errors->getNotices(),
  148.             'warning' => $errors->getWarnings(),
  149.             'danger' => $errors->getErrors(),
  150.         ];
  151.         $request $this->container->get('request_stack')->getMainRequest();
  152.         $exists = [];
  153.         if ($request && $request->hasSession() && method_exists($session $request->getSession(), 'getFlashBag')) {
  154.             $exists $session->getFlashBag()->peekAll();
  155.         }
  156.         $flat = [];
  157.         foreach ($exists as $messages) {
  158.             $flat array_merge($flat$messages);
  159.         }
  160.         /** @var array<string, Error[]> $groups */
  161.         foreach ($groups as $type => $errors) {
  162.             foreach ($errors as $error) {
  163.                 $parameters = [];
  164.                 foreach ($error->getParameters() as $key => $value) {
  165.                     $parameters['%' $key '%'] = $value;
  166.                 }
  167.                 if ($error->getRoute() instanceof ErrorRoute) {
  168.                     $parameters['%url%'] = $this->generateUrl(
  169.                         $error->getRoute()->getKey(),
  170.                         $error->getRoute()->getParams()
  171.                     );
  172.                 }
  173.                 $message $this->trans('checkout.' $error->getMessageKey(), $parameters);
  174.                 if (\in_array($message$flattrue)) {
  175.                     continue;
  176.                 }
  177.                 $this->addFlash($type$message);
  178.             }
  179.         }
  180.     }
  181.     /**
  182.      * @param array<string, mixed> $parameters
  183.      */
  184.     protected function renderView(string $view, array $parameters = []): string
  185.     {
  186.         $view $this->getTemplateFinder()->find($view);
  187.         if (isset($this->twig)) {
  188.             return $this->twig->render($view$parameters);
  189.         }
  190.         throw new \Exception(
  191.             sprintf('Class %s does not have twig injected. Add to your service definition a method call to setTwig with the twig instance', static::class)
  192.         );
  193.     }
  194.     protected function getTemplateFinder(): TemplateFinder
  195.     {
  196.         return $this->container->get(TemplateFinder::class);
  197.     }
  198.     protected function hook(Hook $hook): void
  199.     {
  200.         $this->container->get(ScriptExecutor::class)->execute($hook);
  201.     }
  202.     protected function getSystemConfigService(): SystemConfigService
  203.     {
  204.         return $this->container->get(SystemConfigService::class);
  205.     }
  206. }