vendor/shopware/core/Content/Category/SalesChannel/CachedCategoryRoute.php line 101

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Category\Event\CategoryRouteCacheTagsEvent;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  6. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  7. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  8. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  11. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  12. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  13. use Shopware\Core\Framework\Routing\Annotation\Since;
  14. use Shopware\Core\Profiling\Profiler;
  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. class CachedCategoryRoute extends AbstractCategoryRoute
  25. {
  26.     private AbstractCategoryRoute $decorated;
  27.     private CacheInterface $cache;
  28.     private EntityCacheKeyGenerator $generator;
  29.     /**
  30.      * @var AbstractCacheTracer<CategoryRouteResponse>
  31.      */
  32.     private AbstractCacheTracer $tracer;
  33.     private array $states;
  34.     private EventDispatcherInterface $dispatcher;
  35.     /**
  36.      * @internal
  37.      *
  38.      * @param AbstractCacheTracer<CategoryRouteResponse> $tracer
  39.      */
  40.     public function __construct(
  41.         AbstractCategoryRoute $decorated,
  42.         CacheInterface $cache,
  43.         EntityCacheKeyGenerator $generator,
  44.         AbstractCacheTracer $tracer,
  45.         EventDispatcherInterface $dispatcher,
  46.         array $states
  47.     ) {
  48.         $this->decorated $decorated;
  49.         $this->cache $cache;
  50.         $this->generator $generator;
  51.         $this->tracer $tracer;
  52.         $this->states $states;
  53.         $this->dispatcher $dispatcher;
  54.     }
  55.     public static function buildName(string $id): string
  56.     {
  57.         return 'category-route-' $id;
  58.     }
  59.     public function getDecorated(): AbstractCategoryRoute
  60.     {
  61.         return $this->decorated;
  62.     }
  63.     /**
  64.      * @Since("6.2.0.0")
  65.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  66.      */
  67.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  68.     {
  69.         return Profiler::trace('category-route', function () use ($navigationId$request$context) {
  70.             if ($context->hasState(...$this->states)) {
  71.                 return $this->getDecorated()->load($navigationId$request$context);
  72.             }
  73.             $key $this->generateKey($navigationId$request$context);
  74.             $value $this->cache->get($key, function (ItemInterface $item) use ($navigationId$request$context) {
  75.                 $name self::buildName($navigationId);
  76.                 $response $this->tracer->trace($name, function () use ($navigationId$request$context) {
  77.                     return $this->getDecorated()->load($navigationId$request$context);
  78.                 });
  79.                 $item->tag($this->generateTags($navigationId$response$request$context));
  80.                 return CacheValueCompressor::compress($response);
  81.             });
  82.             return CacheValueCompressor::uncompress($value);
  83.         });
  84.     }
  85.     private function generateKey(string $navigationIdRequest $requestSalesChannelContext $context): string
  86.     {
  87.         $parts array_merge(
  88.             $request->query->all(),
  89.             $request->request->all(),
  90.             [$this->generator->getSalesChannelContextHash($context)]
  91.         );
  92.         $event = new CategoryRouteCacheKeyEvent($navigationId$parts$request$contextnull);
  93.         $this->dispatcher->dispatch($event);
  94.         return self::buildName($navigationId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  95.     }
  96.     private function generateTags(string $navigationIdCategoryRouteResponse $responseRequest $requestSalesChannelContext $context): array
  97.     {
  98.         $tags array_merge(
  99.             $this->tracer->get(self::buildName($navigationId)),
  100.             $this->extractProductIds($response),
  101.             [self::buildName($navigationId)]
  102.         );
  103.         $event = new CategoryRouteCacheTagsEvent($navigationId$tags$request$response$contextnull);
  104.         $this->dispatcher->dispatch($event);
  105.         return array_unique(array_filter($event->getTags()));
  106.     }
  107.     private function extractProductIds(CategoryRouteResponse $response): array
  108.     {
  109.         $page $response->getCategory()->getCmsPage();
  110.         if ($page === null) {
  111.             return [];
  112.         }
  113.         $ids = [];
  114.         $slots $page->getElementsOfType('product-slider');
  115.         /** @var CmsSlotEntity $slot */
  116.         foreach ($slots as $slot) {
  117.             $slider $slot->getData();
  118.             if (!$slider instanceof ProductSliderStruct) {
  119.                 continue;
  120.             }
  121.             if ($slider->getProducts() === null) {
  122.                 continue;
  123.             }
  124.             foreach ($slider->getProducts() as $product) {
  125.                 $ids[] = $product->getId();
  126.                 $ids[] = $product->getParentId();
  127.             }
  128.         }
  129.         $slots $page->getElementsOfType('product-box');
  130.         /** @var CmsSlotEntity $slot */
  131.         foreach ($slots as $slot) {
  132.             $box $slot->getData();
  133.             if (!$box instanceof ProductBoxStruct) {
  134.                 continue;
  135.             }
  136.             if ($box->getProduct() === null) {
  137.                 continue;
  138.             }
  139.             $ids[] = $box->getProduct()->getId();
  140.             $ids[] = $box->getProduct()->getParentId();
  141.         }
  142.         $ids array_values(array_unique(array_filter($ids)));
  143.         return array_merge(
  144.             array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $ids),
  145.             [EntityCacheKeyGenerator::buildCmsTag($page->getId())]
  146.         );
  147.     }
  148. }