vendor/shopware/core/Content/Product/SalesChannel/Search/CachedProductSearchRoute.php line 82

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Search;
  3. use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheTagsEvent;
  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\Since;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SalesChannel\StoreApiResponse;
  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. class CachedProductSearchRoute extends AbstractProductSearchRoute
  20. {
  21.     private const NAME 'product-search-route';
  22.     private AbstractProductSearchRoute $decorated;
  23.     private CacheInterface $cache;
  24.     private EntityCacheKeyGenerator $generator;
  25.     /**
  26.      * @var AbstractCacheTracer<ProductSearchRouteResponse>
  27.      */
  28.     private AbstractCacheTracer $tracer;
  29.     private array $states;
  30.     private EventDispatcherInterface $dispatcher;
  31.     /**
  32.      * @internal
  33.      *
  34.      * @param AbstractCacheTracer<ProductSearchRouteResponse> $tracer
  35.      */
  36.     public function __construct(
  37.         AbstractProductSearchRoute $decorated,
  38.         CacheInterface $cache,
  39.         EntityCacheKeyGenerator $generator,
  40.         AbstractCacheTracer $tracer,
  41.         EventDispatcherInterface $dispatcher,
  42.         array $states
  43.     ) {
  44.         $this->decorated $decorated;
  45.         $this->cache $cache;
  46.         $this->generator $generator;
  47.         $this->tracer $tracer;
  48.         $this->states $states;
  49.         $this->dispatcher $dispatcher;
  50.     }
  51.     public function getDecorated(): AbstractProductSearchRoute
  52.     {
  53.         return $this->decorated;
  54.     }
  55.     /**
  56.      * @Since("6.2.0.0")
  57.      * @Entity("product")
  58.      * @Route("/store-api/search", name="store-api.search", methods={"POST"})
  59.      */
  60.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ProductSearchRouteResponse
  61.     {
  62.         if ($context->hasState(...$this->states)) {
  63.             return $this->getDecorated()->load($request$context$criteria);
  64.         }
  65.         $key $this->generateKey($request$context$criteria);
  66.         $value $this->cache->get($key, function (ItemInterface $item) use ($request$context$criteria) {
  67.             $response $this->tracer->trace(self::NAME, function () use ($request$context$criteria) {
  68.                 return $this->getDecorated()->load($request$context$criteria);
  69.             });
  70.             $item->tag($this->generateTags($request$response$context$criteria));
  71.             return CacheValueCompressor::compress($response);
  72.         });
  73.         return CacheValueCompressor::uncompress($value);
  74.     }
  75.     private function generateKey(Request $requestSalesChannelContext $contextCriteria $criteria): string
  76.     {
  77.         $parts = [
  78.             $this->generator->getCriteriaHash($criteria),
  79.             $this->generator->getSalesChannelContextHash($context),
  80.             $request->get('search'),
  81.         ];
  82.         $event = new ProductSearchRouteCacheKeyEvent($parts$request$context$criteria);
  83.         $this->dispatcher->dispatch($event);
  84.         return self::NAME '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  85.     }
  86.     private function generateTags(Request $requestStoreApiResponse $responseSalesChannelContext $contextCriteria $criteria): array
  87.     {
  88.         $tags array_merge(
  89.             $this->tracer->get(self::NAME),
  90.             [self::NAME]
  91.         );
  92.         $event = new ProductSearchRouteCacheTagsEvent($tags$request$response$context$criteria);
  93.         $this->dispatcher->dispatch($event);
  94.         return array_unique(array_filter($event->getTags()));
  95.     }
  96. }