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

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