vendor/shopware/core/Content/Product/SalesChannel/Suggest/CachedProductSuggestRoute.php line 97

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Suggest;
  3. use Shopware\Core\Content\Product\Events\ProductSuggestRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\ProductSuggestRouteCacheTagsEvent;
  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 Shopware\Core\System\SalesChannel\StoreApiResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Cache\CacheInterface;
  20. use Symfony\Contracts\Cache\ItemInterface;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * @Route(defaults={"_routeScope"={"store-api"}})
  24.  */
  25. #[Package('system-settings')]
  26. class CachedProductSuggestRoute extends AbstractProductSuggestRoute
  27. {
  28.     private const NAME 'product-suggest-route';
  29.     private AbstractProductSuggestRoute $decorated;
  30.     private CacheInterface $cache;
  31.     private EntityCacheKeyGenerator $generator;
  32.     /**
  33.      * @var AbstractCacheTracer<ProductSuggestRouteResponse>
  34.      */
  35.     private AbstractCacheTracer $tracer;
  36.     /**
  37.      * @var array<string>
  38.      */
  39.     private array $states;
  40.     private EventDispatcherInterface $dispatcher;
  41.     /**
  42.      * @internal
  43.      *
  44.      * @param AbstractCacheTracer<ProductSuggestRouteResponse> $tracer
  45.      * @param array<string> $states
  46.      */
  47.     public function __construct(
  48.         AbstractProductSuggestRoute $decorated,
  49.         CacheInterface $cache,
  50.         EntityCacheKeyGenerator $generator,
  51.         AbstractCacheTracer $tracer,
  52.         EventDispatcherInterface $dispatcher,
  53.         array $states
  54.     ) {
  55.         $this->decorated $decorated;
  56.         $this->cache $cache;
  57.         $this->generator $generator;
  58.         $this->tracer $tracer;
  59.         $this->states $states;
  60.         $this->dispatcher $dispatcher;
  61.     }
  62.     public function getDecorated(): AbstractProductSuggestRoute
  63.     {
  64.         return $this->decorated;
  65.     }
  66.     /**
  67.      * @Since("6.2.0.0")
  68.      * @Entity("product")
  69.      * @Route("/store-api/search-suggest", name="store-api.search.suggest", methods={"POST"})
  70.      */
  71.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ProductSuggestRouteResponse
  72.     {
  73.         if ($context->hasState(...$this->states)) {
  74.             return $this->getDecorated()->load($request$context$criteria);
  75.         }
  76.         $key $this->generateKey($request$context$criteria);
  77.         if ($key === null) {
  78.             return $this->getDecorated()->load($request$context$criteria);
  79.         }
  80.         $value $this->cache->get($key, function (ItemInterface $item) use ($request$context$criteria) {
  81.             $response $this->tracer->trace(self::NAME, function () use ($request$context$criteria) {
  82.                 return $this->getDecorated()->load($request$context$criteria);
  83.             });
  84.             $item->tag($this->generateTags($request$response$context$criteria));
  85.             return CacheValueCompressor::compress($response);
  86.         });
  87.         return CacheValueCompressor::uncompress($value);
  88.     }
  89.     private function generateKey(Request $requestSalesChannelContext $contextCriteria $criteria): ?string
  90.     {
  91.         $parts = [
  92.             $this->generator->getCriteriaHash($criteria),
  93.             $this->generator->getSalesChannelContextHash($context, [RuleAreas::PRODUCT_AREA]),
  94.             $request->get('search'),
  95.         ];
  96.         $event = new ProductSuggestRouteCacheKeyEvent($parts$request$context$criteria);
  97.         $this->dispatcher->dispatch($event);
  98.         if (!$event->shouldCache()) {
  99.             return null;
  100.         }
  101.         return self::NAME '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  102.     }
  103.     /**
  104.      * @return array<string>
  105.      */
  106.     private function generateTags(Request $requestStoreApiResponse $responseSalesChannelContext $contextCriteria $criteria): array
  107.     {
  108.         $tags array_merge(
  109.             $this->tracer->get(self::NAME),
  110.             [self::NAME],
  111.         );
  112.         $event = new ProductSuggestRouteCacheTagsEvent($tags$request$response$context$criteria);
  113.         $this->dispatcher->dispatch($event);
  114.         return array_unique(array_filter($event->getTags()));
  115.     }
  116. }