Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

TranslatorBundle/Service/Translator/Translator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Service\Translator;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Kunstmaan\TranslatorBundle\Entity\Translation;
7
use Psr\Container\ContainerInterface;
8
use Symfony\Bundle\FrameworkBundle\Translation\Translator as SymfonyTranslator;
9
10
/**
11
 * Translator
12
 *
13
 * NEXT_MAJOR remove the $profilerEnable constructor parameter en remove the profileTranslation method.
14
 */
15
class Translator extends SymfonyTranslator
16
{
17
    /** @var bool */
18
    private $profilerEnabled;
19
20
    private $translationRepository;
21
22
    /**
23
     * Resource Cacher
24
     *
25
     * @var Kunstmaan\TranslatorBundle\Service\Translator\ResourceCacher
26
     */
27
    private $resourceCacher;
28
29
    /**
30
     * @var \Symfony\Component\HttpFoundation\Request
31
     */
32
    protected $request;
33
34 3
    public function __construct(ContainerInterface $container, $formatter, $defaultLocale = null, array $loaderIds = array(), array $options = array(), $profilerEnable = false)
35
    {
36 3
        parent::__construct($container, $formatter, $defaultLocale, $loaderIds, $options);
37
38 3
        $this->profilerEnabled = $profilerEnable;
39 3
    }
40
41
    /**
42
     * Add resources from the database
43
     * So the translator knows where to look (first) for specific translations
44
     * This function will also look if these resources are loaded from the stash or from the cache
45
     */
46 3
    public function addDatabaseResources()
47
    {
48 3
        if ($this->addResourcesFromCacher() === false) {
49 3
            $this->addResourcesFromDatabaseAndCacheThem();
50
        }
51 3
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function warmUp($cacheDir)
57
    {
58
        return;
59
    }
60
61
    /**
62
     * Add resources to the Translator from the cache
63
     */
64 3
    public function addResourcesFromCacher()
65
    {
66 3
        $resources = $this->resourceCacher->getCachedResources(false);
67
68 3
        if ($resources !== false) {
69
            $this->addResources($resources);
70
71
            return true;
72
        }
73
74 3
        return false;
75
    }
76
77
    /**
78
     * Add resources from the stash and cache them
79
     *
80
     * @param bool $cacheResources cache resources after retrieving them from the stasher
81
     */
82 3
    public function addResourcesFromDatabaseAndCacheThem($cacheResources = true)
83
    {
84
        try {
85 3
            $resources = $this->translationRepository->getAllDomainsByLocale();
86
            $this->addResources($resources);
87
88
            if ($cacheResources === true) {
89
                $this->resourceCacher->cacheResources($resources);
90
            }
91 3
        } catch (\Exception $ex) {
92
            // don't load if the database doesn't work
93
        }
94 3
    }
95
96
    /**
97
     * Add resources to the Translator
98
     * Resources is an array[0] => array('name' => 'messages', 'locale' => 'en')
99
     * Where name is the domain of the domain
100
     *
101
     * @param array $resources
102
     */
103
    public function addResources($resources)
104
    {
105
        foreach ($resources as $resource) {
106
            $this->addResource('database', 'DB', $resource['locale'], $resource['name']);
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function loadCatalogue($locale)
114
    {
115
        if ($this->options['debug'] === true) {
116
            $this->options['cache_dir'] = null; // disable caching for debug
117
        }
118
119
        return parent::loadCatalogue($locale);
120
    }
121
122
    public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
123
    {
124
        if (!$this->request = $this->container->get('request_stack')->getCurrentRequest()) {
125
            return parent::trans($id, $parameters, $domain, $locale);
126
        }
127
128
        $showTranslationsSource = $this->request->get('transSource');
129
        if ($showTranslationsSource !== null) {
130
            $trans = sprintf('%s (%s)', $id, $domain);
131
        } else {
132
            $trans = parent::trans($id, $parameters, $domain, $locale);
133
        }
134
135
        $this->profileTranslation($id, $parameters, $domain, $locale, $trans);
136
137
        return $trans;
138
    }
139
140
    /**
141
     * @deprecated This method is deprecated since KunstmaanTranslatorBundle version 5.1 and will be removed in KunstmaanTranslatorBundle version 6.0
142
     */
143
    public function profileTranslation($id, $parameters, $domain, $locale, $trans)
144
    {
145
        if (!$this->request || $this->profilerEnabled === false) {
146
            return;
147
        }
148
149
        if ($locale === null) {
150
            $locale = $this->request->get('_locale');
151
        }
152
153
        $translation = new Translation();
154
        $translation->setKeyword($id);
155
        $translation->setDomain($domain);
156
        $translation->setLocale($locale);
157
        $translation->setText($trans);
158
159
        $translationCollection = $this->request->request->get('usedTranslations');
160
161
        if (!$translationCollection instanceof \Doctrine\Common\Collections\ArrayCollection) {
162
            $translationCollection = new ArrayCollection();
163
        }
164
165
        $translationCollection->set($domain . $id . $locale, $translation);
166
167
        $this->request->request->set('usedTranslations', $translationCollection);
168
    }
169
170
    public function getTranslationRepository()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
171
    {
172
        return $this->translationRepository;
173
    }
174
175 3
    public function setTranslationRepository($translationRepository)
176
    {
177 3
        $this->translationRepository = $translationRepository;
178 3
    }
179
180 3
    public function setResourceCacher($resourceCacher)
181
    {
182 3
        $this->resourceCacher = $resourceCacher;
183 3
    }
184
}
185