vendor/willdurand/js-translation-bundle/Controller/Controller.php line 107

Open in your IDE?
  1. <?php
  2. namespace Bazinga\Bundle\JsTranslationBundle\Controller;
  3. use Bazinga\Bundle\JsTranslationBundle\Finder\TranslationFinder;
  4. use Symfony\Component\Translation\TranslatorInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Config\ConfigCache;
  8. use Symfony\Component\Config\Resource\FileResource;
  9. use Symfony\Component\Filesystem\Exception\IOException;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Twig_Environment;
  12. /**
  13.  * @author William DURAND <william.durand1@gmail.com>
  14.  */
  15. class Controller
  16. {
  17.     /**
  18.      * @var TranslatorInterface
  19.      */
  20.     private $translator;
  21.     /**
  22.      * @var Twig_Environment
  23.      */
  24.     private $twig;
  25.     /**
  26.      * @var TranslationFinder
  27.      */
  28.     private $translationFinder;
  29.     /**
  30.      * @var array
  31.      */
  32.     private $loaders = array();
  33.     /**
  34.      * @var string
  35.      */
  36.     private $cacheDir;
  37.     /**
  38.      * @var boolean
  39.      */
  40.     private $debug;
  41.     /**
  42.      * @var string
  43.      */
  44.     private $localeFallback;
  45.     /**
  46.      * @var string
  47.      */
  48.     private $defaultDomain;
  49.     /**
  50.      * @var int
  51.      */
  52.     private $httpCacheTime;
  53.     /**
  54.      * @param TranslatorInterface $translator        The translator.
  55.      * @param Twig_Environment    $twig              The twig environment.
  56.      * @param TranslationFinder   $translationFinder The translation finder.
  57.      * @param string              $cacheDir
  58.      * @param boolean             $debug
  59.      * @param string              $localeFallback
  60.      * @param string              $defaultDomain
  61.      * @param int                 $httpCacheTime
  62.      */
  63.     public function __construct(
  64.         TranslatorInterface $translator,
  65.         Twig_Environment $twig,
  66.         TranslationFinder $translationFinder,
  67.         $cacheDir,
  68.         $debug          false,
  69.         $localeFallback '',
  70.         $defaultDomain  '',
  71.         $httpCacheTime  86400
  72.     ) {
  73.         $this->translator        $translator;
  74.         $this->twig              $twig;
  75.         $this->translationFinder $translationFinder;
  76.         $this->cacheDir          $cacheDir;
  77.         $this->debug             $debug;
  78.         $this->localeFallback    $localeFallback;
  79.         $this->defaultDomain     $defaultDomain;
  80.         $this->httpCacheTime     $httpCacheTime;
  81.     }
  82.     /**
  83.      * Add a translation loader if it does not exist.
  84.      *
  85.      * @param string          $id     The loader id.
  86.      * @param LoaderInterface $loader A translation loader.
  87.      */
  88.     public function addLoader($id$loader)
  89.     {
  90.         if (!array_key_exists($id$this->loaders)) {
  91.             $this->loaders[$id] = $loader;
  92.         }
  93.     }
  94.     public function getTranslationsAction(Request $request$domain$_format)
  95.     {
  96.         $locales $this->getLocales($request);
  97.         if (=== count($locales)) {
  98.             throw new NotFoundHttpException();
  99.         }
  100.         $cache = new ConfigCache(sprintf('%s/%s.%s.%s',
  101.             $this->cacheDir,
  102.             $domain,
  103.             implode('-'$locales),
  104.             $_format
  105.         ), $this->debug);
  106.         if (!$cache->isFresh()) {
  107.             $resources    = array();
  108.             $translations = array();
  109.             foreach ($locales as $locale) {
  110.                 $translations[$locale] = array();
  111.                 $files $this->translationFinder->get($domain$locale);
  112.                 if (count($files)) {
  113.                     continue;
  114.                 }
  115.                 $translations[$locale][$domain] = array();
  116.                 foreach ($files as $filename) {
  117.                     $extension pathinfo($filename, \PATHINFO_EXTENSION);
  118.                     if (isset($this->loaders[$extension])) {
  119.                         $resources[] = new FileResource($filename);
  120.                         $catalogue   $this->loaders[$extension]
  121.                             ->load($filename$locale$domain);
  122.                         $translations[$locale][$domain] = array_replace_recursive(
  123.                             $translations[$locale][$domain],
  124.                             $catalogue->all($domain)
  125.                         );
  126.                     }
  127.                 }
  128.             }
  129.             $content $this->twig->render('@BazingaJsTranslation/getTranslations.' $_format '.twig', array(
  130.                 'fallback'       => $this->localeFallback,
  131.                 'defaultDomain'  => $this->defaultDomain,
  132.                 'translations'   => $translations,
  133.                 'include_config' => true,
  134.             ));
  135.             try {
  136.                 $cache->write($content$resources);
  137.             } catch (IOException $e) {
  138.                 throw new NotFoundHttpException();
  139.             }
  140.         }
  141.         if (method_exists($cache'getPath')) {
  142.             $cachePath $cache->getPath();
  143.         } else {
  144.             $cachePath = (string) $cache;
  145.         }
  146.         $expirationTime = new \DateTime();
  147.         $expirationTime->modify('+' $this->httpCacheTime ' seconds');
  148.         $response = new Response(
  149.             file_get_contents($cachePath),
  150.             200,
  151.             array('Content-Type' => $request->getMimeType($_format))
  152.         );
  153.         $response->prepare($request);
  154.         $response->setPublic();
  155.         $response->setETag(md5($response->getContent()));
  156.         $response->isNotModified($request);
  157.         $response->setExpires($expirationTime);
  158.         return $response;
  159.     }
  160.     private function getLocales(Request $request)
  161.     {
  162.         if (null !== $locales $request->query->get('locales')) {
  163.             $locales explode(','$locales);
  164.         } else {
  165.             $locales = array($request->getLocale());
  166.         }
  167.         $locales array_filter($locales, function ($locale) {
  168.             return === preg_match('/^[a-z]{2,3}([-_]{1}[a-zA-Z]{2})?$/'$locale);
  169.         });
  170.         $locales array_unique(array_map(function ($locale) {
  171.             return trim($locale);
  172.         }, $locales));
  173.         return $locales;
  174.     }
  175. }