vendor/lexik/translation-bundle/EventDispatcher/CleanTranslationCacheListener.php line 62

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\TranslationBundle\EventDispatcher;
  3. use Lexik\Bundle\TranslationBundle\Manager\LocaleManager;
  4. use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
  5. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  6. use Symfony\Component\Translation\TranslatorInterface;
  7. use Symfony\Component\Finder\Finder;
  8. /**
  9.  * @author Cédric Girard <c.girard@lexik.fr>
  10.  */
  11. class CleanTranslationCacheListener
  12. {
  13.     /**
  14.      * @var StorageInterface
  15.      */
  16.     private $storage;
  17.     /**
  18.      * @var TranslatorInterface
  19.      */
  20.     private $translator;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $cacheDirectory;
  25.     /**
  26.      * @var array
  27.      */
  28.     private $localeManager;
  29.     /**
  30.      * @var int
  31.      */
  32.     private $cacheInterval;
  33.     /**
  34.      * Constructor
  35.      *
  36.      * @param StorageInterface    $storage
  37.      * @param TranslatorInterface $translator
  38.      * @param string              $cacheDirectory
  39.      * @param LocaleManager       $localeManager
  40.      * @param int                 $cacheInterval
  41.      */
  42.     public function __construct(StorageInterface $storageTranslatorInterface $translator$cacheDirectoryLocaleManager $localeManager$cacheInterval)
  43.     {
  44.         $this->storage $storage;
  45.         $this->cacheDirectory $cacheDirectory;
  46.         $this->translator $translator;
  47.         $this->localeManager $localeManager;
  48.         $this->cacheInterval $cacheInterval;
  49.     }
  50.     /**
  51.      * @param GetResponseEvent $event
  52.      */
  53.     public function onKernelRequest(GetResponseEvent $event)
  54.     {
  55.         if ($event->isMasterRequest() && $this->isCacheExpired()) {
  56.             $lastUpdateTime $this->storage->getLatestUpdatedAt();
  57.             if ($lastUpdateTime instanceof \DateTime) {
  58.                 $this->checkCacheFolder();
  59.                 $finder = new Finder();
  60.                 $finder->files()
  61.                     ->in($this->cacheDirectory.'/translations')
  62.                     ->date('< '.$lastUpdateTime->format('Y-m-d H:i:s'));
  63.                 if ($finder->count() > 0) {
  64.                     $this->translator->removeLocalesCacheFiles($this->localeManager->getLocales());
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     /**
  70.     * Checks if cache has expired
  71.     *
  72.     * @return boolean
  73.     */
  74.     private function isCacheExpired()
  75.     {
  76.         if (empty($this->cacheInterval)) {
  77.             return true;
  78.         }
  79.         $cache_file $this->cacheDirectory.'/translations/cache_timestamp';
  80.         $cache_dir  =$this->cacheDirectory.'/translations';
  81.         if ('\\' === DIRECTORY_SEPARATOR) {
  82.             $cache_file strtr($cache_file'/''\\');
  83.             $cache_dir strtr($cache_dir'/''\\');
  84.         }
  85.         if (!\is_dir($cache_dir)) {
  86.             \mkdir($cache_dir);
  87.         }        
  88.         if (!\file_exists($cache_file)) {
  89.             \touch($cache_file);
  90.             return true;
  91.         }
  92.         $expired false;
  93.         if ((\time() - \filemtime($cache_file)) > $this->cacheInterval) {
  94.             \file_put_contents($cache_file, \time());
  95.             $expired true;
  96.         }
  97.         return $expired;
  98.     }
  99.     private function checkCacheFolder()
  100.     {
  101.         if (!is_dir($dirName $this->cacheDirectory.'/translations') && !mkdir($dirName) && !is_dir($dirName)) {
  102.             throw new \RuntimeException(sprintf('Directory "%s" was not created'$dirName));
  103.         }
  104.     }
  105. }