vendor/shopware/core/System/Language/CachedLanguageLoader.php line 47

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Language;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Contracts\Cache\CacheInterface;
  6. /**
  7.  * @internal
  8.  *
  9.  * @phpstan-import-type LanguageData from LanguageLoaderInterface
  10.  */
  11. #[Package('core')]
  12. class CachedLanguageLoader implements LanguageLoaderInterfaceEventSubscriberInterface
  13. {
  14.     private const CACHE_KEY 'shopware.languages';
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(
  19.         private readonly LanguageLoaderInterface $loader,
  20.         private readonly CacheInterface $cache
  21.     ) {
  22.     }
  23.     /**
  24.      * @return array<string, string>
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             LanguageEvents::LANGUAGE_DELETED_EVENT => 'invalidateCache',
  30.             LanguageEvents::LANGUAGE_WRITTEN_EVENT => 'invalidateCache',
  31.         ];
  32.     }
  33.     /**
  34.      * @return LanguageData
  35.      */
  36.     public function loadLanguages(): array
  37.     {
  38.         return $this->cache->get(self::CACHE_KEY, fn () => $this->loader->loadLanguages());
  39.     }
  40.     public function invalidateCache(): void
  41.     {
  42.         $this->cache->delete(self::CACHE_KEY);
  43.     }
  44. }