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

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