vendor/shopware/core/Content/Product/SalesChannel/Detail/CachedProductDetailRoute.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Detail;
  3. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheTagsEvent;
  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\FieldSerializer\JsonFieldSerializer;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Routing\Annotation\Entity;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Routing\Annotation\Since;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Cache\CacheInterface;
  17. use Symfony\Contracts\Cache\ItemInterface;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"store-api"}})
  21.  */
  22. class CachedProductDetailRoute extends AbstractProductDetailRoute
  23. {
  24.     private AbstractProductDetailRoute $decorated;
  25.     private CacheInterface $cache;
  26.     private EntityCacheKeyGenerator $generator;
  27.     /**
  28.      * @var AbstractCacheTracer<ProductDetailRouteResponse>
  29.      */
  30.     private AbstractCacheTracer $tracer;
  31.     private array $states;
  32.     private EventDispatcherInterface $dispatcher;
  33.     /**
  34.      * @internal
  35.      *
  36.      * @param AbstractCacheTracer<ProductDetailRouteResponse> $tracer
  37.      */
  38.     public function __construct(
  39.         AbstractProductDetailRoute $decorated,
  40.         CacheInterface $cache,
  41.         EntityCacheKeyGenerator $generator,
  42.         AbstractCacheTracer $tracer,
  43.         EventDispatcherInterface $dispatcher,
  44.         array $states
  45.     ) {
  46.         $this->decorated $decorated;
  47.         $this->cache $cache;
  48.         $this->generator $generator;
  49.         $this->tracer $tracer;
  50.         $this->states $states;
  51.         $this->dispatcher $dispatcher;
  52.     }
  53.     public function getDecorated(): AbstractProductDetailRoute
  54.     {
  55.         return $this->decorated;
  56.     }
  57.     /**
  58.      * @Since("6.3.2.0")
  59.      * @Entity("product")
  60.      * @Route("/store-api/product/{productId}", name="store-api.product.detail", methods={"POST"})
  61.      */
  62.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductDetailRouteResponse
  63.     {
  64.         if ($context->hasState(...$this->states)) {
  65.             return $this->getDecorated()->load($productId$request$context$criteria);
  66.         }
  67.         $key $this->generateKey($productId$request$context$criteria);
  68.         $value $this->cache->get($key, function (ItemInterface $item) use ($productId$request$context$criteria) {
  69.             $name self::buildName($productId);
  70.             $response $this->tracer->trace($name, function () use ($productId$request$context$criteria) {
  71.                 return $this->getDecorated()->load($productId$request$context$criteria);
  72.             });
  73.             $item->tag($this->generateTags($productId$request$response$context$criteria));
  74.             return CacheValueCompressor::compress($response);
  75.         });
  76.         return CacheValueCompressor::uncompress($value);
  77.     }
  78.     public static function buildName(string $parentId): string
  79.     {
  80.         return 'product-detail-route-' $parentId;
  81.     }
  82.     private function generateKey(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): string
  83.     {
  84.         $parts = [
  85.             $this->generator->getCriteriaHash($criteria),
  86.             $this->generator->getSalesChannelContextHash($context),
  87.         ];
  88.         $event = new ProductDetailRouteCacheKeyEvent($parts$request$context$criteria);
  89.         $this->dispatcher->dispatch($event);
  90.         return self::buildName($productId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  91.     }
  92.     private function generateTags(string $productIdRequest $requestProductDetailRouteResponse $responseSalesChannelContext $contextCriteria $criteria): array
  93.     {
  94.         $parentId $response->getProduct()->getParentId() ?? $response->getProduct()->getId();
  95.         $pageId $response->getProduct()->getCmsPageId();
  96.         $tags array_merge(
  97.             $this->tracer->get(self::buildName($productId)),
  98.             [$pageId !== null EntityCacheKeyGenerator::buildCmsTag($pageId) : null],
  99.             [self::buildName($parentId)]
  100.         );
  101.         $event = new ProductDetailRouteCacheTagsEvent($tags$request$response$context$criteria);
  102.         $this->dispatcher->dispatch($event);
  103.         return array_unique(array_filter($event->getTags()));
  104.     }
  105. }