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