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\Export; |
13
|
|
|
|
14
|
|
|
use ONGR\TranslationsBundle\Document\Message; |
15
|
|
|
use ONGR\TranslationsBundle\Document\Translation; |
16
|
|
|
use ONGR\TranslationsBundle\Service\LoadersContainer; |
17
|
|
|
use ONGR\TranslationsBundle\Service\TranslationManager; |
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
19
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Export. |
23
|
|
|
*/ |
24
|
|
|
class ExportManager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TranslationManager |
28
|
|
|
*/ |
29
|
|
|
private $translationManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var YmlExport |
33
|
|
|
*/ |
34
|
|
|
private $exporter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ParameterBag |
38
|
|
|
*/ |
39
|
|
|
private $loadersContainer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $managedLocales = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Translation[] |
48
|
|
|
*/ |
49
|
|
|
private $refresh = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ParameterBag $loadersContainer |
53
|
|
|
* @param TranslationManager $translationManager |
54
|
|
|
* @param YmlExport $exporter |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
ParameterBag $loadersContainer, |
58
|
|
|
TranslationManager $translationManager, |
59
|
|
|
YmlExport $exporter |
60
|
|
|
) { |
61
|
|
|
$this->loadersContainer = $loadersContainer; |
62
|
|
|
$this->translationManager = $translationManager; |
63
|
|
|
$this->exporter = $exporter; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets managed locales. |
68
|
|
|
* |
69
|
|
|
* @param array $managedLocales |
70
|
|
|
*/ |
71
|
|
|
public function setManagedLocales($managedLocales) |
72
|
|
|
{ |
73
|
|
|
$this->managedLocales = $managedLocales; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getManagedLocales() |
80
|
|
|
{ |
81
|
|
|
return $this->managedLocales; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Exports translations from ES to files. |
86
|
|
|
* |
87
|
|
|
* @param array $domains To export. |
88
|
|
|
* @param bool $force |
89
|
|
|
*/ |
90
|
|
|
public function export($domains = [], $force = null) |
91
|
|
|
{ |
92
|
|
|
foreach ($this->formExportList($domains, $force) as $file => $translations) { |
|
|
|
|
93
|
|
|
if (!file_exists($file)) { |
94
|
|
|
(new Filesystem())->touch($file); |
95
|
|
|
} |
96
|
|
|
list($domain, $locale, $extension) = explode('.', $file); |
97
|
|
|
if ($this->loadersContainer && $this->loadersContainer->has($extension)) { |
98
|
|
|
$messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain); |
99
|
|
|
$translations = array_merge($messageCatalogue->all($domain), $translations); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->exporter->export($file, $translations); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->translationManager->saveTranslations($this->refresh); |
106
|
|
|
$this->refresh = []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get translations for export. |
111
|
|
|
* |
112
|
|
|
* @param array $domains To read from storage. |
113
|
|
|
* @param bool $force Determines if the message status is relevant. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
private function formExportList($domains, $force) |
118
|
|
|
{ |
119
|
|
|
$data = []; |
120
|
|
|
$filters = array_filter([ |
121
|
|
|
'messages.locale' => $this->getManagedLocales(), |
122
|
|
|
'domain' => $domains |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
$translations = $this->translationManager->getTranslations($filters); |
126
|
|
|
|
127
|
|
|
/** @var Translation $translation */ |
128
|
|
|
foreach ($translations as $translation) { |
129
|
|
|
$messages = $translation->getMessages(); |
130
|
|
|
|
131
|
|
|
foreach ($messages as $key => $message) { |
132
|
|
|
if ($message->getStatus() === Message::DIRTY || $force) { |
133
|
|
|
$path = sprintf( |
134
|
|
|
'%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
135
|
|
|
$translation->getPath(), |
136
|
|
|
$translation->getDomain(), |
137
|
|
|
$message->getLocale(), |
138
|
|
|
$translation->getFormat() |
139
|
|
|
); |
140
|
|
|
$data[$path][$translation->getKey()] = $message->getMessage(); |
141
|
|
|
|
142
|
|
|
$message->setStatus(Message::FRESH); |
143
|
|
|
$this->refresh[] = $translation; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $data; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.