vendor/shopware/storefront/Controller/ScriptController.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\Framework\Routing\Annotation\Since;
  5. use Shopware\Core\Framework\Script\Api\ScriptResponseEncoder;
  6. use Shopware\Core\System\SalesChannel\Api\ResponseFields;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  9. use Shopware\Storefront\Framework\Cache\CacheStore;
  10. use Shopware\Storefront\Framework\Script\Api\StorefrontHook;
  11. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @internal
  17.  * @Route(defaults={"_routeScope"={"storefront"}})
  18.  *
  19.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  20.  */
  21. class ScriptController extends StorefrontController
  22. {
  23.     private GenericPageLoaderInterface $pageLoader;
  24.     private ScriptResponseEncoder $scriptResponseEncoder;
  25.     public function __construct(GenericPageLoaderInterface $pageLoaderScriptResponseEncoder $scriptResponseEncoder)
  26.     {
  27.         $this->pageLoader $pageLoader;
  28.         $this->scriptResponseEncoder $scriptResponseEncoder;
  29.     }
  30.     /**
  31.      * @Since("6.4.9.0")
  32.      * @Route("/storefront/script/{hook}", name="frontend.script_endpoint", defaults={"XmlHttpRequest"=true}, methods={"GET", "POST"}, requirements={"hook"=".+"})
  33.      */
  34.     public function execute(string $hookRequest $requestSalesChannelContext $context): Response
  35.     {
  36.         //  blog/update =>  blog-update
  37.         $hookName \str_replace('/''-'$hook);
  38.         $page $this->pageLoader->load($request$context);
  39.         $hook = new StorefrontHook($hookName$request->request->all(), $request->query->all(), $page$context);
  40.         // hook: storefront-{hook}
  41.         $this->hook($hook);
  42.         $fields = new ResponseFields(
  43.             $request->get('includes', [])
  44.         );
  45.         $response $hook->getScriptResponse();
  46.         $symfonyResponse $this->scriptResponseEncoder->encodeToSymfonyResponse(
  47.             $response,
  48.             $fields,
  49.             \str_replace('-''_''storefront_' $hookName '_response')
  50.         );
  51.         if ($response->getCache()->isEnabled()) {
  52.             $request->attributes->set('_' HttpCache::ALIAS, [HttpCache::fromScriptResponseCacheConfig($response->getCache())]);
  53.             $symfonyResponse->headers->set(CacheStore::TAG_HEADER\json_encode($response->getCache()->getCacheTags(), \JSON_THROW_ON_ERROR));
  54.         }
  55.         return $symfonyResponse;
  56.     }
  57.     public function renderStorefront(string $view, array $parameters = []): Response
  58.     {
  59.         return parent::renderStorefront($view$parameters);
  60.     }
  61. }