vendor/shopware/core/Content/Product/SalesChannel/Listing/CachedProductListingRoute.php line 94

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Listing;
  3. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheTagsEvent;
  5. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  6. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\RuleAreas;
  9. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Routing\Annotation\Entity;
  12. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  13. use Shopware\Core\Framework\Routing\Annotation\Since;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\Cache\CacheInterface;
  18. use Symfony\Contracts\Cache\ItemInterface;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. /**
  21.  * @Route(defaults={"_routeScope"={"store-api"}})
  22.  */
  23. class CachedProductListingRoute extends AbstractProductListingRoute
  24. {
  25.     private AbstractProductListingRoute $decorated;
  26.     private CacheInterface $cache;
  27.     private EntityCacheKeyGenerator $generator;
  28.     /**
  29.      * @var AbstractCacheTracer<ProductListingRouteResponse>
  30.      */
  31.     private AbstractCacheTracer $tracer;
  32.     /**
  33.      * @var array<string>
  34.      */
  35.     private array $states;
  36.     private EventDispatcherInterface $dispatcher;
  37.     /**
  38.      * @internal
  39.      *
  40.      * @param AbstractCacheTracer<ProductListingRouteResponse> $tracer
  41.      * @param array<string> $states
  42.      */
  43.     public function __construct(
  44.         AbstractProductListingRoute $decorated,
  45.         CacheInterface $cache,
  46.         EntityCacheKeyGenerator $generator,
  47.         AbstractCacheTracer $tracer,
  48.         EventDispatcherInterface $dispatcher,
  49.         array $states
  50.     ) {
  51.         $this->decorated $decorated;
  52.         $this->cache $cache;
  53.         $this->generator $generator;
  54.         $this->tracer $tracer;
  55.         $this->states $states;
  56.         $this->dispatcher $dispatcher;
  57.     }
  58.     public function getDecorated(): AbstractProductListingRoute
  59.     {
  60.         return $this->decorated;
  61.     }
  62.     /**
  63.      * @Since("6.2.0.0")
  64.      * @Entity("product")
  65.      * @Route("/store-api/product-listing/{categoryId}", name="store-api.product.listing", methods={"POST"})
  66.      */
  67.     public function load(string $categoryIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductListingRouteResponse
  68.     {
  69.         if ($context->hasState(...$this->states)) {
  70.             return $this->getDecorated()->load($categoryId$request$context$criteria);
  71.         }
  72.         $key $this->generateKey($categoryId$request$context$criteria);
  73.         if ($key === null) {
  74.             return $this->getDecorated()->load($categoryId$request$context$criteria);
  75.         }
  76.         $value $this->cache->get($key, function (ItemInterface $item) use ($categoryId$request$context$criteria) {
  77.             $name self::buildName($categoryId);
  78.             $response $this->tracer->trace($name, function () use ($categoryId$request$context$criteria) {
  79.                 return $this->getDecorated()->load($categoryId$request$context$criteria);
  80.             });
  81.             $item->tag($this->generateTags($categoryId$request$response$context$criteria));
  82.             return CacheValueCompressor::compress($response);
  83.         });
  84.         return CacheValueCompressor::uncompress($value);
  85.     }
  86.     public static function buildName(string $categoryId): string
  87.     {
  88.         return 'product-listing-route-' $categoryId;
  89.     }
  90.     private function generateKey(string $categoryIdRequest $requestSalesChannelContext $contextCriteria $criteria): ?string
  91.     {
  92.         $parts = [
  93.             $this->generator->getCriteriaHash($criteria),
  94.             $this->generator->getSalesChannelContextHash($context, [RuleAreas::PRODUCT_AREARuleAreas::CATEGORY_AREA]),
  95.         ];
  96.         $event = new ProductListingRouteCacheKeyEvent($parts$categoryId$request$context$criteria);
  97.         $this->dispatcher->dispatch($event);
  98.         if (!$event->shouldCache()) {
  99.             return null;
  100.         }
  101.         return self::buildName($categoryId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  102.     }
  103.     /**
  104.      * @return array<string>
  105.      */
  106.     private function generateTags(string $categoryIdRequest $requestProductListingRouteResponse $responseSalesChannelContext $contextCriteria $criteria): array
  107.     {
  108.         $streamId $response->getResult()->getStreamId();
  109.         $tags array_merge(
  110.             $this->tracer->get(self::buildName($categoryId)),
  111.             [$streamId EntityCacheKeyGenerator::buildStreamTag($streamId) : null],
  112.             [self::buildName($categoryId)]
  113.         );
  114.         $event = new ProductListingRouteCacheTagsEvent($tags$categoryId$request$response$context$criteria);
  115.         $this->dispatcher->dispatch($event);
  116.         return array_unique(array_filter($event->getTags()));
  117.     }
  118. }