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

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\Field\Flag\RuleAreas;
  12. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  13. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  14. use Shopware\Core\Framework\Routing\Annotation\Since;
  15. use Shopware\Core\Profiling\Profiler;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  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. class CachedCategoryRoute extends AbstractCategoryRoute
  26. {
  27.     private AbstractCategoryRoute $decorated;
  28.     private CacheInterface $cache;
  29.     private EntityCacheKeyGenerator $generator;
  30.     /**
  31.      * @var AbstractCacheTracer<CategoryRouteResponse>
  32.      */
  33.     private AbstractCacheTracer $tracer;
  34.     /**
  35.      * @var array<string>
  36.      */
  37.     private array $states;
  38.     private EventDispatcherInterface $dispatcher;
  39.     /**
  40.      * @internal
  41.      *
  42.      * @param AbstractCacheTracer<CategoryRouteResponse> $tracer
  43.      * @param array<string> $states
  44.      */
  45.     public function __construct(
  46.         AbstractCategoryRoute $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 static function buildName(string $id): string
  61.     {
  62.         return 'category-route-' $id;
  63.     }
  64.     public function getDecorated(): AbstractCategoryRoute
  65.     {
  66.         return $this->decorated;
  67.     }
  68.     /**
  69.      * @Since("6.2.0.0")
  70.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  71.      */
  72.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  73.     {
  74.         return Profiler::trace('category-route', function () use ($navigationId$request$context) {
  75.             if ($context->hasState(...$this->states)) {
  76.                 return $this->getDecorated()->load($navigationId$request$context);
  77.             }
  78.             $key $this->generateKey($navigationId$request$context);
  79.             if ($key === null) {
  80.                 return $this->getDecorated()->load($navigationId$request$context);
  81.             }
  82.             $value $this->cache->get($key, function (ItemInterface $item) use ($navigationId$request$context) {
  83.                 $name self::buildName($navigationId);
  84.                 $response $this->tracer->trace($name, function () use ($navigationId$request$context) {
  85.                     return $this->getDecorated()->load($navigationId$request$context);
  86.                 });
  87.                 $item->tag($this->generateTags($navigationId$response$request$context));
  88.                 return CacheValueCompressor::compress($response);
  89.             });
  90.             return CacheValueCompressor::uncompress($value);
  91.         });
  92.     }
  93.     private function generateKey(string $navigationIdRequest $requestSalesChannelContext $context): ?string
  94.     {
  95.         $parts array_merge(
  96.             $request->query->all(),
  97.             $request->request->all(),
  98.             [$this->generator->getSalesChannelContextHash($context, [RuleAreas::CATEGORY_AREARuleAreas::PRODUCT_AREA])]
  99.         );
  100.         $event = new CategoryRouteCacheKeyEvent($navigationId$parts$request$contextnull);
  101.         $this->dispatcher->dispatch($event);
  102.         if (!$event->shouldCache()) {
  103.             return null;
  104.         }
  105.         return self::buildName($navigationId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  106.     }
  107.     /**
  108.      * @return array<string>
  109.      */
  110.     private function generateTags(string $navigationIdCategoryRouteResponse $responseRequest $requestSalesChannelContext $context): array
  111.     {
  112.         $tags array_merge(
  113.             $this->tracer->get(self::buildName($navigationId)),
  114.             $this->extractProductIds($response),
  115.             [self::buildName($navigationId)]
  116.         );
  117.         $event = new CategoryRouteCacheTagsEvent($navigationId$tags$request$response$contextnull);
  118.         $this->dispatcher->dispatch($event);
  119.         return array_unique(array_filter($event->getTags()));
  120.     }
  121.     /**
  122.      * @return array<string>
  123.      */
  124.     private function extractProductIds(CategoryRouteResponse $response): array
  125.     {
  126.         $page $response->getCategory()->getCmsPage();
  127.         if ($page === null) {
  128.             return [];
  129.         }
  130.         $ids = [];
  131.         $slots $page->getElementsOfType('product-slider');
  132.         /** @var CmsSlotEntity $slot */
  133.         foreach ($slots as $slot) {
  134.             $slider $slot->getData();
  135.             if (!$slider instanceof ProductSliderStruct) {
  136.                 continue;
  137.             }
  138.             if ($slider->getProducts() === null) {
  139.                 continue;
  140.             }
  141.             foreach ($slider->getProducts() as $product) {
  142.                 $ids[] = $product->getId();
  143.                 $ids[] = $product->getParentId();
  144.             }
  145.         }
  146.         $slots $page->getElementsOfType('product-box');
  147.         /** @var CmsSlotEntity $slot */
  148.         foreach ($slots as $slot) {
  149.             $box $slot->getData();
  150.             if (!$box instanceof ProductBoxStruct) {
  151.                 continue;
  152.             }
  153.             if ($box->getProduct() === null) {
  154.                 continue;
  155.             }
  156.             $ids[] = $box->getProduct()->getId();
  157.             $ids[] = $box->getProduct()->getParentId();
  158.         }
  159.         $ids array_values(array_unique(array_filter($ids)));
  160.         return array_merge(
  161.             array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $ids),
  162.             [EntityCacheKeyGenerator::buildCmsTag($page->getId())]
  163.         );
  164.     }
  165. }