src/AtechNet/CoreBundle/Translation/DatabaseTranslator.php line 66

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: DancZer
  5.  * Date: 2015.09.29.
  6.  * Time: 10:06
  7.  */
  8. namespace App\AtechNet\CoreBundle\Translation;
  9. use App\AtechNet\CoreBundle\Entity\Domain;
  10. use App\AtechNet\CoreBundle\Entity\Language;
  11. use App\AtechNet\CoreBundle\Entity\Translation;
  12. use Doctrine\ORM\EntityManager;
  13. class DatabaseTranslator implements TranslatorInterface
  14. {
  15.     /**
  16.      * Translator name
  17.      *
  18.      * @var string
  19.      */
  20.     private $name;
  21.     /**
  22.      * @var EntityManager $entityManager
  23.      */
  24.     private $entityManager;
  25.     /**
  26.      * @var Domain $domain
  27.      */
  28.     private $domain;
  29.     /**
  30.      * @var Language $language
  31.      */
  32.     private $language;
  33.     private $memoryCache;
  34.     /**
  35.      * @param EntityManager $entityManager
  36.      * @param int $domain_id
  37.      * @param bool $warmUpCache
  38.      * @throws \Exception
  39.      * @internal param bool $warmupCache
  40.      */
  41.     public function __construct($entityManager, int $domain_id, bool $warmUpCache = true)
  42.     {
  43.         $this->entityManager = $entityManager;
  44.         $this->init($domain_id);
  45.         if ($warmUpCache) {
  46.             $this->initDomainCache();
  47.         }
  48.     }
  49.     private function init($domain_id)
  50.     {
  51.         $this->memoryCache = [];
  52.         $result = $this->entityManager->createQuery('SELECT d, l, tr FROM AtechNetCoreBundle:Domain d LEFT JOIN d.language l LEFT JOIN l.translations tr WHERE d.id = :id')
  53.             ->setParameter('id', $domain_id)
  54.             ->getResult();
  55.         if (count($result) !== 1) {
  56.             throw new \Exception('Domain not found!');
  57.         }
  58.         $this->domain = $result[0];
  59.         $this->language = $this->domain->getLanguage();
  60.         $this->name = $this->domain->getName() . '_' . $this->domain->getId();
  61.     }
  62.     private function initDomainCache()
  63.     {
  64.         /**
  65.          * @var Translation $trans
  66.          */
  67.         foreach ($this->domain->getLanguage()->getTranslations() as $trans) {
  68.             $this->memoryCache[$trans->getKey()] = $trans->getValue();
  69.         }
  70.     }
  71.     /**
  72.      * @inheritdoc
  73.      */
  74.     public function trans($text)
  75.     {
  76.         $result = $this->findInCache($text);
  77.         if (is_null($result)) {
  78.             return $text;
  79.         } elseif (!$result) {
  80.             $translations = $this->findInDb($text);
  81.             //not found
  82.             if (!$translations) {
  83.                 $this->memoryCache[$text] = null;
  84.                 return $text;
  85.             } else {
  86.                 /**
  87.                  * @var Translation $translation
  88.                  */
  89.                 $translation = $translations[0];
  90.                 $result = $translation->getValue();
  91.                 $this->memoryCache[$text] = $result;
  92.                 return $result;
  93.             }
  94.         } else {
  95.             return $result;
  96.         }
  97.     }
  98.     /**
  99.      * @param $text
  100.      *
  101.      * @return string as the translated text from memory cache or false if not found
  102.      */
  103.     private function findInCache($text)
  104.     {
  105.         if (array_key_exists($text, $this->memoryCache)) {
  106.             return $this->memoryCache[$text];
  107.         } else {
  108.             return false;
  109.         }
  110.     }
  111.     /**
  112.      * @param string $text
  113.      *
  114.      * @return Translation $translation or false if not found
  115.      */
  116.     private function findInDb($text)
  117.     {
  118.         return $this->entityManager->getRepository(Translation::class)->findBy([
  119.             'language' => $this->language,
  120.             'key' => $text,
  121.         ]);
  122.     }
  123.     public function getName()
  124.     {
  125.         return $this->name;
  126.     }
  127. }