src/Storefront/Page/GenericPageLoader.php line 69

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page;
  3. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  4. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Profiling\Profiler;
  8. use Shopware\Core\SalesChannelRequest;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Event\RouteRequest\PaymentMethodRouteRequestEvent;
  12. use Shopware\Storefront\Event\RouteRequest\ShippingMethodRouteRequestEvent;
  13. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoaderInterface;
  14. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoaderInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. #[Package('storefront')]
  18. class GenericPageLoader implements GenericPageLoaderInterface
  19. {
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(private readonly HeaderPageletLoaderInterface $headerLoader, private readonly FooterPageletLoaderInterface $footerLoader, private readonly SystemConfigService $systemConfigService, private readonly AbstractPaymentMethodRoute $paymentMethodRoute, private readonly AbstractShippingMethodRoute $shippingMethodRoute, private readonly EventDispatcherInterface $eventDispatcher)
  24.     {
  25.     }
  26.     public function load(Request $requestSalesChannelContext $context): Page
  27.     {
  28.         return Profiler::trace('generic-page-loader', function () use ($request$context) {
  29.             $page = new Page();
  30.             if ($request->isXmlHttpRequest()) {
  31.                 $this->eventDispatcher->dispatch(
  32.                     new GenericPageLoadedEvent($page$context$request)
  33.                 );
  34.                 return $page;
  35.             }
  36.             $page->setHeader(
  37.                 $this->headerLoader->load($request$context)
  38.             );
  39.             $page->setFooter(
  40.                 $this->footerLoader->load($request$context)
  41.             );
  42.             $criteria = new Criteria();
  43.             $criteria->setTitle('generic-page::shipping-methods');
  44.             $event = new ShippingMethodRouteRequestEvent($request, new Request(), $context$criteria);
  45.             $this->eventDispatcher->dispatch($event);
  46.             $shippingMethods $this->shippingMethodRoute
  47.                 ->load($event->getStoreApiRequest(), $context$event->getCriteria())
  48.                 ->getShippingMethods();
  49.             $page->setSalesChannelShippingMethods($shippingMethods);
  50.             $criteria = new Criteria();
  51.             $criteria->setTitle('generic-page::payment-methods');
  52.             $event = new PaymentMethodRouteRequestEvent($request, new Request(), $context$criteria);
  53.             $this->eventDispatcher->dispatch($event);
  54.             $paymentMethods $this->paymentMethodRoute
  55.                 ->load($event->getStoreApiRequest(), $context$event->getCriteria())
  56.                 ->getPaymentMethods();
  57.             $page->setSalesChannelPaymentMethods($paymentMethods);
  58.             $page->setMetaInformation((new MetaInformation())->assign([
  59.                 'revisit' => '15 days',
  60.                 'robots' => 'index,follow',
  61.                 'xmlLang' => $request->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_LOCALE) ?? '',
  62.                 'metaTitle' => $this->systemConfigService->getString('core.basicInformation.shopName'$context->getSalesChannel()->getId()),
  63.             ]));
  64.             $this->eventDispatcher->dispatch(
  65.                 new GenericPageLoadedEvent($page$context$request)
  66.             );
  67.             return $page;
  68.         });
  69.     }
  70. }