vendor/shopware/core/Content/Sitemap/SalesChannel/SitemapRoute.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Sitemap\SalesChannel;
  3. use Shopware\Core\Content\Sitemap\Exception\AlreadyLockedException;
  4. use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
  5. use Shopware\Core\Content\Sitemap\Service\SitemapListerInterface;
  6. use Shopware\Core\Content\Sitemap\Struct\SitemapCollection;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  9. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  10. use Shopware\Core\Framework\Routing\Annotation\Since;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route(defaults={"_routeScope"={"store-api"}})
  17.  */
  18. #[Package('sales-channel')]
  19. class SitemapRoute extends AbstractSitemapRoute
  20. {
  21.     /**
  22.      * @var SitemapListerInterface
  23.      */
  24.     private $sitemapLister;
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     /**
  30.      * @var SitemapExporterInterface
  31.      */
  32.     private $sitemapExporter;
  33.     /**
  34.      * @internal
  35.      */
  36.     public function __construct(SitemapListerInterface $sitemapListerSystemConfigService $systemConfigServiceSitemapExporterInterface $sitemapExporter)
  37.     {
  38.         $this->sitemapLister $sitemapLister;
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->sitemapExporter $sitemapExporter;
  41.     }
  42.     /**
  43.      * @Since("6.3.2.0")
  44.      * @Route(path="/store-api/sitemap", name="store-api.sitemap", methods={"GET", "POST"})
  45.      */
  46.     public function load(Request $requestSalesChannelContext $context): SitemapRouteResponse
  47.     {
  48.         $sitemaps $this->sitemapLister->getSitemaps($context);
  49.         if ($this->systemConfigService->getInt('core.sitemap.sitemapRefreshStrategy') !== SitemapExporterInterface::STRATEGY_LIVE) {
  50.             return new SitemapRouteResponse(new SitemapCollection($sitemaps));
  51.         }
  52.         // Close session to prevent session locking from waiting in case there is another request coming in
  53.         if ($request->hasSession() && session_status() === \PHP_SESSION_ACTIVE) {
  54.             $request->getSession()->save();
  55.         }
  56.         try {
  57.             $this->generateSitemap($contexttrue);
  58.         } catch (AlreadyLockedException $exception) {
  59.             // Silent catch, lock couldn't be acquired. Some other process already generates the sitemap.
  60.         }
  61.         $sitemaps $this->sitemapLister->getSitemaps($context);
  62.         return new SitemapRouteResponse(new SitemapCollection($sitemaps));
  63.     }
  64.     public function getDecorated(): AbstractSitemapRoute
  65.     {
  66.         throw new DecorationPatternException(self::class);
  67.     }
  68.     private function generateSitemap(SalesChannelContext $salesChannelContextbool $force, ?string $lastProvider null, ?int $offset null): void
  69.     {
  70.         $result $this->sitemapExporter->generate($salesChannelContext$force$lastProvider$offset);
  71.         if ($result->isFinish() === false) {
  72.             $this->generateSitemap($salesChannelContext$force$result->getProvider(), $result->getOffset());
  73.         }
  74.     }
  75. }