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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\SalesChannel;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  5. use Shopware\Core\Content\Category\Event\CategoryRouteCacheTagsEvent;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  7. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  8. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  9. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  10. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  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.     private array $states;
  35.     private EventDispatcherInterface $dispatcher;
  36.     /**
  37.      * @internal
  38.      *
  39.      * @param AbstractCacheTracer<CategoryRouteResponse> $tracer
  40.      */
  41.     public function __construct(
  42.         AbstractCategoryRoute $decorated,
  43.         CacheInterface $cache,
  44.         EntityCacheKeyGenerator $generator,
  45.         AbstractCacheTracer $tracer,
  46.         EventDispatcherInterface $dispatcher,
  47.         array $states
  48.     ) {
  49.         $this->decorated $decorated;
  50.         $this->cache $cache;
  51.         $this->generator $generator;
  52.         $this->tracer $tracer;
  53.         $this->states $states;
  54.         $this->dispatcher $dispatcher;
  55.     }
  56.     public static function buildName(string $id): string
  57.     {
  58.         return 'category-route-' $id;
  59.     }
  60.     public function getDecorated(): AbstractCategoryRoute
  61.     {
  62.         return $this->decorated;
  63.     }
  64.     /**
  65.      * @Since("6.2.0.0")
  66.      * @OA\Post(
  67.      *     path="/category/{categoryId}",
  68.      *     summary="Fetch a single category",
  69.      *     description="This endpoint returns information about the category, as well as a fully resolved (hydrated with mapping values) CMS page, if one is assigned to the category. You can pass slots which should be resolved exclusively.",
  70.      *     operationId="readCategory",
  71.      *     tags={"Store API", "Category"},
  72.      *     @OA\Parameter(
  73.      *         name="categoryId",
  74.      *         description="Identifier of the category to be fetched",
  75.      *         @OA\Schema(type="string", pattern="^[0-9a-f]{32}$"),
  76.      *         in="path",
  77.      *         required=true
  78.      *     ),
  79.      *     @OA\Parameter(
  80.      *         name="slots",
  81.      *         description="Resolves only the given slot identifiers. The identifiers have to be seperated by a '|' character",
  82.      *         @OA\Schema(type="string"),
  83.      *         in="query",
  84.      *     ),
  85.      *     @OA\Parameter(name="Api-Basic-Parameters"),
  86.      *     @OA\Response(
  87.      *          response="200",
  88.      *          description="The loaded category with cms page",
  89.      *          @OA\JsonContent(ref="#/components/schemas/Category")
  90.      *     )
  91.      * )
  92.      *
  93.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  94.      */
  95.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  96.     {
  97.         return Profiler::trace('category-route', function () use ($navigationId$request$context) {
  98.             if ($context->hasState(...$this->states)) {
  99.                 return $this->getDecorated()->load($navigationId$request$context);
  100.             }
  101.             $key $this->generateKey($navigationId$request$context);
  102.             $value $this->cache->get($key, function (ItemInterface $item) use ($navigationId$request$context) {
  103.                 $name self::buildName($navigationId);
  104.                 $response $this->tracer->trace($name, function () use ($navigationId$request$context) {
  105.                     return $this->getDecorated()->load($navigationId$request$context);
  106.                 });
  107.                 $item->tag($this->generateTags($navigationId$response$request$context));
  108.                 return CacheValueCompressor::compress($response);
  109.             });
  110.             return CacheValueCompressor::uncompress($value);
  111.         });
  112.     }
  113.     private function generateKey(string $navigationIdRequest $requestSalesChannelContext $context): string
  114.     {
  115.         $parts array_merge(
  116.             $request->query->all(),
  117.             $request->request->all(),
  118.             [$this->generator->getSalesChannelContextHash($context)]
  119.         );
  120.         $event = new CategoryRouteCacheKeyEvent($navigationId$parts$request$contextnull);
  121.         $this->dispatcher->dispatch($event);
  122.         return self::buildName($navigationId) . '-' md5(JsonFieldSerializer::encodeJson($event->getParts()));
  123.     }
  124.     private function generateTags(string $navigationIdCategoryRouteResponse $responseRequest $requestSalesChannelContext $context): array
  125.     {
  126.         $tags array_merge(
  127.             $this->tracer->get(self::buildName($navigationId)),
  128.             $this->extractProductIds($response),
  129.             [self::buildName($navigationId)]
  130.         );
  131.         $event = new CategoryRouteCacheTagsEvent($navigationId$tags$request$response$contextnull);
  132.         $this->dispatcher->dispatch($event);
  133.         return array_unique(array_filter($event->getTags()));
  134.     }
  135.     private function extractProductIds(CategoryRouteResponse $response): array
  136.     {
  137.         $page $response->getCategory()->getCmsPage();
  138.         if ($page === null) {
  139.             return [];
  140.         }
  141.         $ids = [];
  142.         $slots $page->getElementsOfType('product-slider');
  143.         /** @var CmsSlotEntity $slot */
  144.         foreach ($slots as $slot) {
  145.             $slider $slot->getData();
  146.             if (!$slider instanceof ProductSliderStruct) {
  147.                 continue;
  148.             }
  149.             if ($slider->getProducts() === null) {
  150.                 continue;
  151.             }
  152.             foreach ($slider->getProducts() as $product) {
  153.                 $ids[] = $product->getId();
  154.                 $ids[] = $product->getParentId();
  155.             }
  156.         }
  157.         $slots $page->getElementsOfType('product-box');
  158.         /** @var CmsSlotEntity $slot */
  159.         foreach ($slots as $slot) {
  160.             $box $slot->getData();
  161.             if (!$box instanceof ProductBoxStruct) {
  162.                 continue;
  163.             }
  164.             if ($box->getProduct() === null) {
  165.                 continue;
  166.             }
  167.             $ids[] = $box->getProduct()->getId();
  168.             $ids[] = $box->getProduct()->getParentId();
  169.         }
  170.         $ids array_values(array_unique(array_filter($ids)));
  171.         return array_merge(
  172.             array_map([EntityCacheKeyGenerator::class, 'buildProductTag'], $ids),
  173.             [EntityCacheKeyGenerator::buildCmsTag($page->getId())]
  174.         );
  175.     }
  176. }