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

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\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 CachedProductDetailRoute extends AbstractProductDetailRoute
  24. {
  25.     private AbstractProductDetailRoute $decorated;
  26.     private CacheInterface $cache;
  27.     private EntityCacheKeyGenerator $generator;
  28.     /**
  29.      * @var AbstractCacheTracer<ProductDetailRouteResponse>
  30.      */
  31.     private AbstractCacheTracer $tracer;
  32.     /**
  33.      * @var array<string, string>
  34.      */
  35.     private array $states;
  36.     private EventDispatcherInterface $dispatcher;
  37.     /**
  38.      * @internal
  39.      *
  40.      * @param AbstractCacheTracer<ProductDetailRouteResponse> $tracer
  41.      * @param array<string> $states
  42.      */
  43.     public function __construct(
  44.         AbstractProductDetailRoute $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(): AbstractProductDetailRoute
  59.     {
  60.         return $this->decorated;
  61.     }
  62.     /**
  63.      * @Since("6.3.2.0")
  64.      * @Entity("product")
  65.      * @Route("/store-api/product/{productId}", name="store-api.product.detail", methods={"POST"})
  66.      */
  67.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductDetailRouteResponse
  68.     {
  69.         if ($context->hasState(...$this->states)) {
  70.             return $this->getDecorated()->load($productId$request$context$criteria);
  71.         }
  72.         $key $this->generateKey($productId$request$context$criteria);
  73.         if ($key === null) {
  74.             return $this->getDecorated()->load($productId$request$context$criteria);
  75.         }
  76.         $value $this->cache->get($key, function (ItemInterface $item) use ($productId$request$context$criteria) {
  77.             $name self::buildName($productId);
  78.             $response $this->tracer->trace($name, function () use ($productId$request$context$criteria) {
  79.                 return $this->getDecorated()->load($productId$request$context$criteria);
  80.             });
  81.             $item->tag($this->generateTags($productId$request$response$context$criteria));
  82.             return CacheValueCompressor::compress($response);
  83.         });
  84.         return CacheValueCompressor::uncompress($value);
  85.     }
  86.     public static function buildName(string $parentId): string
  87.     {
  88.         return 'product-detail-route-' $parentId;
  89.     }
  90.     private function generateKey(string $productIdRequest $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 ProductDetailRouteCacheKeyEvent($parts$request$context$criteria);
  97.         $this->dispatcher->dispatch($event);
  98.         if (!$event->shouldCache()) {
  99.             return null;
  100.         }
  101.         return self::buildName($productId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  102.     }
  103.     /**
  104.      * @return array<string>
  105.      */
  106.     private function generateTags(string $productIdRequest $requestProductDetailRouteResponse $responseSalesChannelContext $contextCriteria $criteria): array
  107.     {
  108.         $parentId $response->getProduct()->getParentId() ?? $response->getProduct()->getId();
  109.         $pageId $response->getProduct()->getCmsPageId();
  110.         $tags array_merge(
  111.             $this->tracer->get(self::buildName($productId)),
  112.             [$pageId !== null EntityCacheKeyGenerator::buildCmsTag($pageId) : null],
  113.             [self::buildName($parentId)]
  114.         );
  115.         $event = new ProductDetailRouteCacheTagsEvent($tags$request$response$context$criteria);
  116.         $this->dispatcher->dispatch($event);
  117.         return array_unique(array_filter($event->getTags()));
  118.     }
  119. }