vendor/shopware/core/Content/LandingPage/SalesChannel/CachedLandingPageRoute.php line 83

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\LandingPage\SalesChannel;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  4. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  5. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  6. use Shopware\Core\Content\LandingPage\Event\LandingPageRouteCacheKeyEvent;
  7. use Shopware\Core\Content\LandingPage\Event\LandingPageRouteCacheTagsEvent;
  8. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\RuleAreas;
  12. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Shopware\Core\Framework\Routing\Annotation\Since;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. use Symfony\Contracts\Cache\ItemInterface;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. /**
  22.  * @Route(defaults={"_routeScope"={"store-api"}})
  23.  */
  24. class CachedLandingPageRoute extends AbstractLandingPageRoute
  25. {
  26.     private AbstractLandingPageRoute $decorated;
  27.     private CacheInterface $cache;
  28.     private EntityCacheKeyGenerator $generator;
  29.     /**
  30.      * @var AbstractCacheTracer<LandingPageRouteResponse>
  31.      */
  32.     private AbstractCacheTracer $tracer;
  33.     /**
  34.      * @var array<string>
  35.      */
  36.     private array $states;
  37.     private EventDispatcherInterface $dispatcher;
  38.     /**
  39.      * @internal
  40.      *
  41.      * @param AbstractCacheTracer<LandingPageRouteResponse> $tracer
  42.      * @param array<string> $states
  43.      */
  44.     public function __construct(
  45.         AbstractLandingPageRoute $decorated,
  46.         CacheInterface $cache,
  47.         EntityCacheKeyGenerator $generator,
  48.         AbstractCacheTracer $tracer,
  49.         EventDispatcherInterface $dispatcher,
  50.         array $states
  51.     ) {
  52.         $this->decorated $decorated;
  53.         $this->cache $cache;
  54.         $this->generator $generator;
  55.         $this->tracer $tracer;
  56.         $this->states $states;
  57.         $this->dispatcher $dispatcher;
  58.     }
  59.     public static function buildName(string $id): string
  60.     {
  61.         return 'landing-page-route-' $id;
  62.     }
  63.     public function getDecorated(): AbstractLandingPageRoute
  64.     {
  65.         return $this->decorated;
  66.     }
  67.     /**
  68.      * @Since("6.4.0.0")
  69.      * @Route("/store-api/landing-page/{landingPageId}", name="store-api.landing-page.detail", methods={"POST"})
  70.      */
  71.     public function load(string $landingPageIdRequest $requestSalesChannelContext $context): LandingPageRouteResponse
  72.     {
  73.         if ($context->hasState(...$this->states)) {
  74.             return $this->getDecorated()->load($landingPageId$request$context);
  75.         }
  76.         $key $this->generateKey($landingPageId$request$context);
  77.         if ($key === null) {
  78.             return $this->getDecorated()->load($landingPageId$request$context);
  79.         }
  80.         $value $this->cache->get($key, function (ItemInterface $item) use ($request$context$landingPageId) {
  81.             $name self::buildName($landingPageId);
  82.             $response $this->tracer->trace($name, function () use ($landingPageId$request$context) {
  83.                 return $this->getDecorated()->load($landingPageId$request$context);
  84.             });
  85.             $item->tag($this->generateTags($landingPageId$response$request$context));
  86.             return CacheValueCompressor::compress($response);
  87.         });
  88.         return CacheValueCompressor::uncompress($value);
  89.     }
  90.     private function generateKey(string $landingPageIdRequest $requestSalesChannelContext $context): ?string
  91.     {
  92.         $parts array_merge(
  93.             $request->query->all(),
  94.             $request->request->all(),
  95.             [$this->generator->getSalesChannelContextHash($context, [RuleAreas::LANDING_PAGE_AREARuleAreas::PRODUCT_AREARuleAreas::CATEGORY_AREA])]
  96.         );
  97.         $event = new LandingPageRouteCacheKeyEvent($landingPageId$parts$request$contextnull);
  98.         $this->dispatcher->dispatch($event);
  99.         if (!$event->shouldCache()) {
  100.             return null;
  101.         }
  102.         return self::buildName($landingPageId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  103.     }
  104.     /**
  105.      * @return array<string>
  106.      */
  107.     private function generateTags(string $landingPageIdLandingPageRouteResponse $responseRequest $requestSalesChannelContext $context): array
  108.     {
  109.         $tags array_merge(
  110.             $this->tracer->get(self::buildName($landingPageId)),
  111.             $this->extractIds($response),
  112.             [self::buildName($landingPageId)]
  113.         );
  114.         $event = new LandingPageRouteCacheTagsEvent($landingPageId$tags$request$response$contextnull);
  115.         $this->dispatcher->dispatch($event);
  116.         return array_unique(array_filter($event->getTags()));
  117.     }
  118.     /**
  119.      * @return array<string>
  120.      */
  121.     private function extractIds(LandingPageRouteResponse $response): array
  122.     {
  123.         $page $response->getLandingPage()->getCmsPage();
  124.         if ($page === null) {
  125.             return [];
  126.         }
  127.         $ids = [];
  128.         $slots $page->getElementsOfType('product-slider');
  129.         /** @var CmsSlotEntity $slot */
  130.         foreach ($slots as $slot) {
  131.             $slider $slot->getData();
  132.             if (!$slider instanceof ProductSliderStruct) {
  133.                 continue;
  134.             }
  135.             if ($slider->getProducts() === null) {
  136.                 continue;
  137.             }
  138.             foreach ($slider->getProducts() as $product) {
  139.                 $ids[] = $product->getId();
  140.                 $ids[] = $product->getParentId();
  141.             }
  142.         }
  143.         $slots $page->getElementsOfType('product-box');
  144.         /** @var CmsSlotEntity $slot */
  145.         foreach ($slots as $slot) {
  146.             $box $slot->getData();
  147.             if (!$box instanceof ProductBoxStruct) {
  148.                 continue;
  149.             }
  150.             if ($box->getProduct() === null) {
  151.                 continue;
  152.             }
  153.             $ids[] = $box->getProduct()->getId();
  154.             $ids[] = $box->getProduct()->getParentId();
  155.         }
  156.         $ids array_values(array_unique(array_filter($ids)));
  157.         return array_merge(
  158.             array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $ids),
  159.             [EntityCacheKeyGenerator::buildCmsTag($page->getId())]
  160.         );
  161.     }
  162. }