src/Core/System/Language/CachedLanguageLoader.php line 44

  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.  * @phpstan-import-type LanguageData from LanguageLoaderInterface
  9.  */
  10. #[Package('core')]
  11. class CachedLanguageLoader implements LanguageLoaderInterfaceEventSubscriberInterface
  12. {
  13.     private const CACHE_KEY 'shopware.languages';
  14.     /**
  15.      * @internal
  16.      */
  17.     public function __construct(private readonly LanguageLoaderInterface $loader, private readonly CacheInterface $cache)
  18.     {
  19.     }
  20.     /**
  21.      * @return array<string, string>
  22.      */
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             LanguageEvents::LANGUAGE_DELETED_EVENT => 'invalidateCache',
  27.             LanguageEvents::LANGUAGE_WRITTEN_EVENT => 'invalidateCache',
  28.         ];
  29.     }
  30.     /**
  31.      * @return LanguageData
  32.      */
  33.     public function loadLanguages(): array
  34.     {
  35.         return $this->cache->get(self::CACHE_KEY, fn () => $this->loader->loadLanguages());
  36.     }
  37.     public function invalidateCache(): void
  38.     {
  39.         $this->cache->delete(self::CACHE_KEY);
  40.     }
  41. }