vendor/lexik/translation-bundle/Translation/Translator.php line 21

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\TranslationBundle\Translation;
  3. use Lexik\Bundle\TranslationBundle\EventDispatcher\Event\GetDatabaseResourcesEvent;
  4. use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;
  5. use Symfony\Component\Translation\Loader\LoaderInterface;
  6. use Symfony\Component\Config\ConfigCache;
  7. use Symfony\Component\Finder\Finder;
  8. /**
  9.  * Translator service class.
  10.  *
  11.  * @author Cédric Girard <c.girard@lexik.fr>
  12.  */
  13. class Translator extends BaseTranslator
  14. {
  15.     /**
  16.      * Add all resources available in database.
  17.      */
  18.     public function addDatabaseResources()
  19.     {
  20.         $file sprintf('%s/database.resources.php'$this->options['cache_dir']);
  21.         $cache = new ConfigCache($file$this->options['debug']);
  22.         if (!$cache->isFresh()) {
  23.             $event = new GetDatabaseResourcesEvent();
  24.             $this->container->get('event_dispatcher')->dispatch($event);
  25.             $resources $event->getResources();
  26.             $metadata = array();
  27.             foreach ($resources as $resource) {
  28.                 $metadata[] = new DatabaseFreshResource($resource['locale'], $resource['domain']);
  29.             }
  30.             $content sprintf("<?php return %s;"var_export($resourcestrue));
  31.             $cache->write($content$metadata);
  32.         } else {
  33.             $resources = include $file;
  34.         }
  35.         foreach ($resources as $resource) {
  36.             $this->addResource('database''DB'$resource['locale'], $resource['domain']);
  37.         }
  38.     }
  39.     /**
  40.      * Remove the cache file corresponding to the given locale.
  41.      *
  42.      * @param string $locale
  43.      * @return boolean
  44.      */
  45.     public function removeCacheFile($locale)
  46.     {
  47.         $localeExploded explode('_'$locale);
  48.         $finder = new Finder();
  49.         $finder->files()->in($this->options['cache_dir'])->name(sprintf'/catalogue\.%s.*\.php$/'$localeExploded[0]));
  50.         $deleted true;
  51.         foreach ($finder as $file) {
  52.             $path $file->getRealPath();
  53.             $this->invalidateSystemCacheForFile($path);
  54.             $deleted unlink($path);
  55.             $metadata $path.'.meta';
  56.             if (file_exists($metadata)) {
  57.                 $this->invalidateSystemCacheForFile($metadata);
  58.                 unlink($metadata);
  59.             }
  60.         }
  61.         return $deleted;
  62.     }
  63.     /**
  64.      * Remove the cache file corresponding to each given locale.
  65.      *
  66.      * @param array $locales
  67.      */
  68.     public function removeLocalesCacheFiles(array $locales)
  69.     {
  70.         foreach ($locales as $locale) {
  71.             $this->removeCacheFile($locale);
  72.         }
  73.         // also remove database.resources.php cache file
  74.         $file sprintf('%s/database.resources.php'$this->options['cache_dir']);
  75.         if (file_exists($file)) {
  76.             $this->invalidateSystemCacheForFile($file);
  77.             unlink($file);
  78.         }
  79.         $metadata $file.'.meta';
  80.         if (file_exists($metadata)) {
  81.             $this->invalidateSystemCacheForFile($metadata);
  82.             unlink($metadata);
  83.         }
  84.     }
  85.     /**
  86.      * @param string $path
  87.      *
  88.      * @throws \RuntimeException
  89.      */
  90.     protected function invalidateSystemCacheForFile($path)
  91.     {
  92.         if (ini_get('apc.enabled') && function_exists('apc_delete_file')) {
  93.             if (apc_exists($path) && !apc_delete_file($path)) {
  94.                 throw new \RuntimeException(sprintf('Failed to clear APC Cache for file %s'$path));
  95.             }
  96.         } elseif ('cli' === php_sapi_name() ? ini_get('opcache.enable_cli') : ini_get('opcache.enable')) {
  97.             if (function_exists("opcache_invalidate") && !opcache_invalidate($pathtrue)) {
  98.                 throw new \RuntimeException(sprintf('Failed to clear OPCache for file %s'$path));
  99.             }
  100.         }
  101.     }
  102.     /**
  103.      * Returns all translations file formats.
  104.      *
  105.      * @return array
  106.      */
  107.     public function getFormats()
  108.     {
  109.         $allFormats = array();
  110.         foreach ($this->loaderIds as $id => $formats) {
  111.             foreach ($formats as $format) {
  112.                 if ('database' !== $format) {
  113.                     $allFormats[] = $format;
  114.                 }
  115.             }
  116.         }
  117.         return $allFormats;
  118.     }
  119.     /**
  120.      * Returns a loader according to the given format.
  121.      *
  122.      * @param string $format
  123.      * @throws \RuntimeException
  124.      * @return LoaderInterface
  125.      */
  126.     public function getLoader($format)
  127.     {
  128.         $loader null;
  129.         $i 0;
  130.         $ids array_keys($this->loaderIds);
  131.         while ($i count($ids) && null === $loader) {
  132.             if (in_array($format$this->loaderIds[$ids[$i]])) {
  133.                 $loader $this->container->get($ids[$i]);
  134.             }
  135.             $i++;
  136.         }
  137.         if (!($loader instanceof LoaderInterface)) {
  138.             throw new \RuntimeException(sprintf('No loader found for "%s" format.'$format));
  139.         }
  140.         return $loader;
  141.     }
  142. }