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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Detail;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheKeyEvent;
  5. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheTagsEvent;
  6. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  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.     private array $states;
  33.     private EventDispatcherInterface $dispatcher;
  34.     /**
  35.      * @internal
  36.      *
  37.      * @param AbstractCacheTracer<ProductDetailRouteResponse> $tracer
  38.      */
  39.     public function __construct(
  40.         AbstractProductDetailRoute $decorated,
  41.         CacheInterface $cache,
  42.         EntityCacheKeyGenerator $generator,
  43.         AbstractCacheTracer $tracer,
  44.         EventDispatcherInterface $dispatcher,
  45.         array $states
  46.     ) {
  47.         $this->decorated $decorated;
  48.         $this->cache $cache;
  49.         $this->generator $generator;
  50.         $this->tracer $tracer;
  51.         $this->states $states;
  52.         $this->dispatcher $dispatcher;
  53.     }
  54.     public function getDecorated(): AbstractProductDetailRoute
  55.     {
  56.         return $this->decorated;
  57.     }
  58.     /**
  59.      * @Since("6.3.2.0")
  60.      * @Entity("product")
  61.      * @OA\Post(
  62.      *      path="/product/{productId}",
  63.      *      summary="Fetch a single product",
  64.      *      description="This route is used to load a single product with the corresponding details. In addition to loading the data, the best variant of the product is determined when a parent id is passed.",
  65.      *      operationId="readProductDetail",
  66.      *      tags={"Store API","Product"},
  67.      *      @OA\Parameter(
  68.      *          name="productId",
  69.      *          description="Product ID",
  70.      *          @OA\Schema(type="string"),
  71.      *          in="path",
  72.      *          required=true
  73.      *      ),
  74.      *      @OA\Response(
  75.      *          response="200",
  76.      *          description="Product information along with variant groups and options",
  77.      *          @OA\JsonContent(ref="#/components/schemas/ProductDetailResponse")
  78.      *     )
  79.      * )
  80.      * @Route("/store-api/product/{productId}", name="store-api.product.detail", methods={"POST"})
  81.      */
  82.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductDetailRouteResponse
  83.     {
  84.         if ($context->hasState(...$this->states)) {
  85.             return $this->getDecorated()->load($productId$request$context$criteria);
  86.         }
  87.         $key $this->generateKey($productId$request$context$criteria);
  88.         $value $this->cache->get($key, function (ItemInterface $item) use ($productId$request$context$criteria) {
  89.             $name self::buildName($productId);
  90.             $response $this->tracer->trace($name, function () use ($productId$request$context$criteria) {
  91.                 return $this->getDecorated()->load($productId$request$context$criteria);
  92.             });
  93.             $item->tag($this->generateTags($productId$request$response$context$criteria));
  94.             return CacheValueCompressor::compress($response);
  95.         });
  96.         return CacheValueCompressor::uncompress($value);
  97.     }
  98.     public static function buildName(string $parentId): string
  99.     {
  100.         return 'product-detail-route-' $parentId;
  101.     }
  102.     private function generateKey(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): string
  103.     {
  104.         $parts = [
  105.             $this->generator->getCriteriaHash($criteria),
  106.             $this->generator->getSalesChannelContextHash($context),
  107.         ];
  108.         $event = new ProductDetailRouteCacheKeyEvent($parts$request$context$criteria);
  109.         $this->dispatcher->dispatch($event);
  110.         return self::buildName($productId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  111.     }
  112.     private function generateTags(string $productIdRequest $requestProductDetailRouteResponse $responseSalesChannelContext $contextCriteria $criteria): array
  113.     {
  114.         $parentId $response->getProduct()->getParentId() ?? $response->getProduct()->getId();
  115.         $pageId $response->getProduct()->getCmsPageId();
  116.         $tags array_merge(
  117.             $this->tracer->get(self::buildName($productId)),
  118.             [$pageId !== null EntityCacheKeyGenerator::buildCmsTag($pageId) : null],
  119.             [self::buildName($parentId)]
  120.         );
  121.         $event = new ProductDetailRouteCacheTagsEvent($tags$request$response$context$criteria);
  122.         $this->dispatcher->dispatch($event);
  123.         return array_unique(array_filter($event->getTags()));
  124.     }
  125. }