<?php declare(strict_types=1);
namespace Shopware\Core\Content\Sitemap\SalesChannel;
use Shopware\Core\Content\Sitemap\Event\SitemapRouteCacheKeyEvent;
use Shopware\Core\Content\Sitemap\Event\SitemapRouteCacheTagsEvent;
use Shopware\Core\Content\Sitemap\Service\SitemapExporterInterface;
use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
#[Package('sales-channel')]
class CachedSitemapRoute extends AbstractSitemapRoute
{
public const ALL_TAG = 'sitemap-route';
private AbstractSitemapRoute $decorated;
private CacheInterface $cache;
private EntityCacheKeyGenerator $generator;
/**
* @var AbstractCacheTracer<SitemapRouteResponse>
*/
private AbstractCacheTracer $tracer;
/**
* @var array<string>
*/
private array $states;
private EventDispatcherInterface $dispatcher;
private SystemConfigService $config;
/**
* @internal
*
* @param AbstractCacheTracer<SitemapRouteResponse> $tracer
* @param array<string> $states
*/
public function __construct(
AbstractSitemapRoute $decorated,
CacheInterface $cache,
EntityCacheKeyGenerator $generator,
AbstractCacheTracer $tracer,
EventDispatcherInterface $dispatcher,
array $states,
SystemConfigService $config
) {
$this->decorated = $decorated;
$this->cache = $cache;
$this->generator = $generator;
$this->tracer = $tracer;
$this->states = $states;
$this->dispatcher = $dispatcher;
$this->config = $config;
}
public static function buildName(string $id): string
{
return 'sitemap-route-' . $id;
}
public function getDecorated(): AbstractSitemapRoute
{
return $this->decorated;
}
/**
* @Since("6.3.2.0")
* @Route(path="/store-api/sitemap", name="store-api.sitemap", methods={"GET", "POST"})
*/
public function load(Request $request, SalesChannelContext $context): SitemapRouteResponse
{
if ($context->hasState(...$this->states)) {
return $this->getDecorated()->load($request, $context);
}
$strategy = $this->config->getInt('core.sitemap.sitemapRefreshStrategy');
if ($strategy === SitemapExporterInterface::STRATEGY_LIVE) {
return $this->getDecorated()->load($request, $context);
}
$key = $this->generateKey($request, $context);
if ($key === null) {
return $this->getDecorated()->load($request, $context);
}
$value = $this->cache->get($key, function (ItemInterface $item) use ($request, $context) {
$name = self::buildName($context->getSalesChannelId());
$response = $this->tracer->trace($name, function () use ($request, $context) {
return $this->getDecorated()->load($request, $context);
});
$item->tag($this->generateTags($response, $request, $context));
return CacheValueCompressor::compress($response);
});
return CacheValueCompressor::uncompress($value);
}
private function generateKey(Request $request, SalesChannelContext $context): ?string
{
$parts = [$this->generator->getSalesChannelContextHash($context)];
$event = new SitemapRouteCacheKeyEvent($parts, $request, $context, null);
$this->dispatcher->dispatch($event);
if (!$event->shouldCache()) {
return null;
}
return self::buildName($context->getSalesChannelId()) . '-' . md5(JsonFieldSerializer::encodeJson($event->getParts()));
}
/**
* @return array<string>
*/
private function generateTags(SitemapRouteResponse $response, Request $request, SalesChannelContext $context): array
{
$tags = array_merge(
$this->tracer->get(self::buildName($context->getSalesChannelId())),
[self::buildName($context->getSalesChannelId()), self::ALL_TAG]
);
$event = new SitemapRouteCacheTagsEvent($tags, $request, $response, $context, null);
$this->dispatcher->dispatch($event);
return array_unique(array_filter($event->getTags()));
}
}