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

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