platform/src/Core/System/Language/CachedLanguageLoader.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Language;
  3. use Psr\Cache\CacheItemPoolInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class CachedLanguageLoader implements LanguageLoaderInterfaceEventSubscriberInterface
  6. {
  7.     private const CACHE_KEY 'shopware.languages';
  8.     private CacheItemPoolInterface $cache;
  9.     private LanguageLoaderInterface $loader;
  10.     public function __construct(LanguageLoaderInterface $loaderCacheItemPoolInterface $cache)
  11.     {
  12.         $this->cache $cache;
  13.         $this->loader $loader;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             LanguageEvents::LANGUAGE_DELETED_EVENT => 'invalidateCache',
  19.             LanguageEvents::LANGUAGE_WRITTEN_EVENT => 'invalidateCache',
  20.         ];
  21.     }
  22.     public function loadLanguages(): array
  23.     {
  24.         $cacheItem $this->cache->getItem(self::CACHE_KEY);
  25.         if ($cacheItem->isHit()) {
  26.             return $cacheItem->get();
  27.         }
  28.         $languages $this->loader->loadLanguages();
  29.         $cacheItem->set($languages);
  30.         $this->cache->save($cacheItem);
  31.         return $languages;
  32.     }
  33.     public function invalidateCache(): void
  34.     {
  35.         $cacheItem $this->cache->getItem(self::CACHE_KEY);
  36.         if (!$cacheItem->isHit()) {
  37.             return;
  38.         }
  39.         $this->cache->deleteItem($cacheItem->getKey());
  40.     }
  41. }