src/Core/Content/Seo/CachedSeoResolver.php line 36

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Seo;
  3. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Symfony\Contracts\Cache\CacheInterface;
  6. use Symfony\Contracts\Cache\ItemInterface;
  7. /**
  8.  * @phpstan-import-type ResolvedSeoUrl from AbstractSeoResolver
  9.  */
  10. #[Package('sales-channel')]
  11. class CachedSeoResolver extends AbstractSeoResolver
  12. {
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(private readonly AbstractSeoResolver $decorated, private readonly CacheInterface $cache)
  17.     {
  18.     }
  19.     public function getDecorated(): AbstractSeoResolver
  20.     {
  21.         return $this->decorated;
  22.     }
  23.     /**
  24.      * @return ResolvedSeoUrl
  25.      */
  26.     public function resolve(string $languageIdstring $salesChannelIdstring $pathInfo): array
  27.     {
  28.         $key 'seo-resolver-' md5(implode('-', [$languageId$salesChannelId$pathInfo]));
  29.         $value $this->cache->get($key, function (ItemInterface $item) use ($languageId$salesChannelId$pathInfo) {
  30.             $resolved $this->getDecorated()->resolve($languageId$salesChannelId$pathInfo);
  31.             $item->tag([self::buildName($pathInfo)]);
  32.             return CacheValueCompressor::compress($resolved);
  33.         });
  34.         /** @var ResolvedSeoUrl $value */
  35.         $value CacheValueCompressor::uncompress($value);
  36.         return $value;
  37.     }
  38.     public static function buildName(string $pathInfo): string
  39.     {
  40.         return 'path-info-' md5($pathInfo);
  41.     }
  42. }