These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the ONGR package. |
||
5 | * |
||
6 | * (c) NFQ Technologies UAB <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace ONGR\TranslationsBundle\Service; |
||
13 | |||
14 | use Elasticsearch\Common\Exceptions\BadRequest400Exception; |
||
15 | use ONGR\TranslationsBundle\Storage\StorageInterface; |
||
16 | use ONGR\TranslationsBundle\Translation\Import\FileImport; |
||
17 | use Symfony\Component\Finder\Finder; |
||
18 | |||
19 | /** |
||
20 | * Collects translations. |
||
21 | */ |
||
22 | class Import |
||
23 | { |
||
24 | /** |
||
25 | * @var FileImport |
||
26 | */ |
||
27 | private $fileImport; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $locales; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $domains; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $kernelDir; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $bundles; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $formats; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $translations = []; |
||
58 | |||
59 | /** |
||
60 | * @var StorageInterface |
||
61 | */ |
||
62 | private $storage; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $configBundles; |
||
68 | |||
69 | /** |
||
70 | * @param FileImport $fileImport |
||
71 | * @param StorageInterface $storage |
||
72 | * @param string $kernelDir |
||
73 | * @param array $kernelBundles |
||
74 | * @param array $configBundles |
||
75 | */ |
||
76 | public function __construct( |
||
77 | FileImport $fileImport, |
||
78 | 5 | StorageInterface $storage, |
|
79 | $kernelDir, |
||
80 | $kernelBundles, |
||
81 | $configBundles |
||
82 | ) { |
||
83 | $this->fileImport = $fileImport; |
||
84 | $this->storage = $storage; |
||
85 | 5 | $this->kernelDir = $kernelDir; |
|
86 | 5 | $this->bundles = $kernelBundles; |
|
87 | 5 | $this->configBundles = $configBundles; |
|
88 | 5 | } |
|
89 | 5 | ||
90 | 5 | /** |
|
91 | * Collects translations. |
||
92 | */ |
||
93 | public function import() |
||
94 | { |
||
95 | 1 | $this->importAppTranslationFiles(); |
|
96 | |||
97 | 1 | $this->importBundlesTranslationFiles(array_merge($this->getConfigBundles(), $this->getBundles())); |
|
98 | |||
99 | 1 | $this->importComponentTranslationFiles(); |
|
100 | } |
||
101 | 1 | ||
102 | 1 | /** |
|
103 | * Returns all translations as array. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getTranslations() |
||
108 | { |
||
109 | 1 | if (empty($this->translations)) { |
|
110 | $this->import(); |
||
111 | 1 | } |
|
112 | 1 | ||
113 | return $this->translations; |
||
114 | } |
||
115 | 1 | ||
116 | /** |
||
117 | * Write translations to storage. |
||
118 | */ |
||
119 | public function writeToStorage() |
||
120 | { |
||
121 | 2 | try { |
|
122 | $this->storage->write($this->translations); |
||
123 | } catch (BadRequest400Exception $e) { |
||
124 | 2 | // Empty bulk commit exception. |
|
125 | } |
||
126 | } |
||
127 | |||
128 | 2 | /** |
|
129 | * Imports application translation files. |
||
130 | */ |
||
131 | public function importAppTranslationFiles() |
||
132 | { |
||
133 | 2 | $finder = $this->findTranslationsFiles( |
|
134 | $this->kernelDir, |
||
135 | 2 | $this->getLocales(), |
|
136 | 2 | $this->getDomains() |
|
137 | 2 | ); |
|
138 | 2 | $this->importTranslationFiles($finder); |
|
139 | } |
||
140 | 2 | ||
141 | 2 | /** |
|
142 | * Imports translation files form all bundles. |
||
143 | * |
||
144 | * @param array $bundles |
||
145 | * @param bool $isBundle |
||
146 | */ |
||
147 | public function importBundlesTranslationFiles($bundles, $isBundle = false) |
||
148 | 3 | { |
|
149 | foreach ($bundles as $bundle) { |
||
150 | 3 | $dir = $isBundle? |
|
151 | 3 | dir($bundle->getPath())->path : |
|
0 ignored issues
–
show
|
|||
152 | 3 | dirname((new \ReflectionClass($bundle))->getFilename()); |
|
153 | |||
154 | 3 | $this->importBundleTranslationFiles($dir); |
|
155 | } |
||
156 | 3 | } |
|
157 | |||
158 | /** |
||
159 | * Imports translation files form the specific bundles. |
||
160 | * |
||
161 | * @param string $bundle |
||
162 | */ |
||
163 | 3 | public function importBundleTranslationFiles($bundle) |
|
164 | { |
||
165 | 3 | $finder = $this->findTranslationsFiles($bundle); |
|
166 | 3 | $this->importTranslationFiles($finder); |
|
167 | 3 | } |
|
168 | |||
169 | /** |
||
170 | * Imports Symfony's components translation files. |
||
171 | */ |
||
172 | 2 | public function importComponentTranslationFiles() |
|
173 | { |
||
174 | $classes = [ |
||
175 | 2 | 'Symfony\Component\Validator\Validator' => '/Resources/translations', |
|
176 | 'Symfony\Component\Form\Form' => '/Resources/translations', |
||
177 | 'Symfony\Component\Security\Core\Exception\AuthenticationException' => '/../../Resources/translations', |
||
178 | ]; |
||
179 | |||
180 | 2 | $dirs = []; |
|
181 | 2 | foreach ($classes as $namespace => $translationDir) { |
|
182 | 2 | $reflection = new \ReflectionClass($namespace); |
|
183 | 2 | $dirs[] = dirname($reflection->getFilename()) . $translationDir; |
|
184 | } |
||
185 | |||
186 | 2 | $finder = $this->getFinder(); |
|
187 | 2 | $finder->files() |
|
188 | 2 | ->name($this->getFileNamePattern()) |
|
189 | 2 | ->in($dirs); |
|
190 | |||
191 | 2 | $this->importTranslationFiles($finder->count() > 0 ? $finder : null); |
|
192 | 2 | } |
|
193 | |||
194 | /** |
||
195 | * Return a Finder object if $path has a Resources/translations folder. |
||
196 | * |
||
197 | * @param string $path |
||
198 | * |
||
199 | * @return Finder |
||
200 | */ |
||
201 | 3 | protected function findTranslationsFiles($path) |
|
202 | { |
||
203 | 3 | $finder = null; |
|
204 | |||
205 | 3 | if (preg_match('#^win#i', PHP_OS)) { |
|
206 | $path = preg_replace('#' . preg_quote(DIRECTORY_SEPARATOR, '#') . '#', '/', $path); |
||
207 | } else { |
||
208 | 3 | $path = str_replace('\\', '/', $path); |
|
209 | } |
||
210 | |||
211 | 3 | $dir = $path . '/Resources/translations'; |
|
212 | |||
213 | 3 | if (is_dir($dir)) { |
|
214 | 3 | $finder = $this->getFinder(); |
|
215 | 3 | $finder->files() |
|
216 | 3 | ->name($this->getFileNamePattern()) |
|
217 | 3 | ->in($dir); |
|
218 | } |
||
219 | |||
220 | 3 | return (null !== $finder && $finder->count() > 0) ? $finder : null; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | 3 | protected function getFileNamePattern() |
|
227 | { |
||
228 | 3 | if (count($this->getDomains())) { |
|
229 | 1 | $regex = sprintf( |
|
230 | 1 | '/((%s)\.(%s)\.(%s))/', |
|
231 | 1 | implode('|', $this->getDomains()), |
|
232 | 1 | implode('|', $this->getLocales()), |
|
233 | 1 | implode('|', $this->getFormats()) |
|
234 | ); |
||
235 | } else { |
||
236 | 2 | $regex = sprintf( |
|
237 | 2 | '/(.*\.(%s)\.(%s))/', |
|
238 | 2 | implode('|', $this->getLocales()), |
|
239 | 2 | implode('|', $this->getFormats()) |
|
240 | ); |
||
241 | } |
||
242 | |||
243 | 3 | return $regex; |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Imports some translations files. |
||
248 | * |
||
249 | * @param Finder $finder |
||
250 | */ |
||
251 | 3 | protected function importTranslationFiles($finder) |
|
252 | { |
||
253 | 3 | if ($finder instanceof Finder) { |
|
254 | 3 | foreach ($finder as $file) { |
|
255 | 3 | $this->translations = array_replace_recursive($this->fileImport->import($file), $this->translations); |
|
256 | } |
||
257 | } |
||
258 | 3 | } |
|
259 | |||
260 | /** |
||
261 | * @return mixed |
||
262 | */ |
||
263 | 3 | public function getDomains() |
|
264 | { |
||
265 | 3 | return $this->domains; |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param mixed $domains |
||
270 | */ |
||
271 | 5 | public function setDomains($domains) |
|
272 | { |
||
273 | 5 | $this->domains = $domains; |
|
274 | 5 | } |
|
275 | |||
276 | /** |
||
277 | * @return mixed |
||
278 | */ |
||
279 | 3 | public function getLocales() |
|
280 | { |
||
281 | 3 | return $this->locales; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param mixed $locales |
||
286 | */ |
||
287 | 5 | public function setLocales($locales) |
|
288 | { |
||
289 | 5 | $this->locales = $locales; |
|
290 | 5 | } |
|
291 | |||
292 | /** |
||
293 | * @return mixed |
||
294 | */ |
||
295 | 3 | public function getFormats() |
|
296 | { |
||
297 | 3 | return $this->formats; |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param mixed $formats |
||
302 | */ |
||
303 | 5 | public function setFormats($formats) |
|
304 | { |
||
305 | 5 | $this->formats = $formats; |
|
306 | 5 | } |
|
307 | |||
308 | /** |
||
309 | * Returns Finder object. |
||
310 | * |
||
311 | * @return Finder |
||
312 | */ |
||
313 | 3 | protected function getFinder() |
|
314 | { |
||
315 | 3 | return new Finder(); |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | 2 | public function getConfigBundles() |
|
322 | { |
||
323 | 2 | return $this->configBundles; |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * @return array |
||
328 | */ |
||
329 | 2 | public function getBundles() |
|
330 | { |
||
331 | 2 | return $this->bundles; |
|
332 | } |
||
333 | } |
||
334 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.