|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP Translation package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) PHP Translation team <[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 Translation\Bundle\Catalogue; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
|
15
|
|
|
use Translation\Bundle\Model\CatalogueMessage; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A manager that handle loaded catalogues. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class CatalogueManager |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var MessageCatalogue[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $catalogues; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $projectRoot; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $projectRoot |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($projectRoot) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->projectRoot = $projectRoot; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param MessageCatalogue[] $catalogues |
|
44
|
|
|
*/ |
|
45
|
|
|
public function load(array $catalogues) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->catalogues = []; |
|
48
|
|
|
foreach ($catalogues as $c) { |
|
49
|
|
|
$this->catalogues[$c->getLocale()] = $c; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getDomains() |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var MessageCatalogue $c */ |
|
59
|
|
|
$c = reset($this->catalogues); |
|
60
|
|
|
|
|
61
|
|
|
return $c->getDomains(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $locale |
|
66
|
|
|
* @param string $domain |
|
67
|
|
|
* |
|
68
|
|
|
* @return CatalogueMessage[] |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getMessages($locale, $domain) |
|
71
|
|
|
{ |
|
72
|
|
|
$messages = []; |
|
73
|
|
|
if (!isset($this->catalogues[$locale])) { |
|
74
|
|
|
return $messages; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
foreach ($this->catalogues[$locale]->all($domain) as $key => $text) { |
|
78
|
|
|
$messages[] = new CatalogueMessage($this, $locale, $domain, $key, $text); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $messages; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $config { |
|
86
|
|
|
* |
|
87
|
|
|
* @var string $domain |
|
88
|
|
|
* @var string $locale |
|
89
|
|
|
* @var bool $isNew |
|
90
|
|
|
* @var bool $isObsolete |
|
91
|
|
|
* } |
|
92
|
|
|
* |
|
93
|
|
|
* @return CatalogueMessage[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function findMessages(array $config = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$inputDomain = isset($config['domain']) ? $config['domain'] : null; |
|
98
|
|
|
$isNew = isset($config['isNew']) ? $config['isNew'] : null; |
|
99
|
|
|
$isObsolete = isset($config['isObsolete']) ? $config['isObsolete'] : null; |
|
100
|
|
|
|
|
101
|
|
|
$messages = []; |
|
102
|
|
|
$catalogues = []; |
|
103
|
|
|
if (isset($config['locale'])) { |
|
104
|
|
|
$locale = $config['locale']; |
|
105
|
|
|
if (isset($this->catalogues[$locale])) { |
|
106
|
|
|
$catalogues = [$locale => $this->catalogues[$locale]]; |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
|
|
$catalogues = $this->catalogues; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
foreach ($catalogues as $locale => $catalogue) { |
|
113
|
|
|
$domains = $catalogue->getDomains(); |
|
114
|
|
|
if (null !== $inputDomain) { |
|
115
|
|
|
$domains = [$inputDomain]; |
|
116
|
|
|
} |
|
117
|
|
|
foreach ($domains as $domain) { |
|
118
|
|
|
foreach ($catalogue->all($domain) as $key => $text) { |
|
119
|
|
|
// Filter on new and obsolete |
|
120
|
|
|
if (null !== $isNew || null !== $isObsolete) { |
|
121
|
|
|
$notes = $this->getNotes($domain, $key, $catalogue); |
|
122
|
|
|
|
|
123
|
|
|
if (null !== $isNew) { |
|
124
|
|
|
if ($isNew !== $this->hasNoteNew($notes)) { |
|
125
|
|
|
continue; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
if (null !== $isObsolete) { |
|
129
|
|
|
if ($isObsolete !== $this->hasNoteObsolete($notes)) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$messages[] = new CatalogueMessage($this, $locale, $domain, $key, $text); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $messages; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $domain |
|
145
|
|
|
* @param string $key |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getTranslations($domain, $key) |
|
150
|
|
|
{ |
|
151
|
|
|
$translations = []; |
|
152
|
|
|
foreach ($this->catalogues as $locale => $catalogue) { |
|
153
|
|
|
if ($catalogue->has($key, $domain)) { |
|
154
|
|
|
$translations[$locale] = $catalogue->get($key, $domain); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $translations; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $domain |
|
163
|
|
|
* @param string $key |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getSourceLocations($domain, $key) |
|
168
|
|
|
{ |
|
169
|
|
|
$notes = $this->getNotes($domain, $key); |
|
170
|
|
|
$sources = []; |
|
171
|
|
|
foreach ($notes as $note) { |
|
172
|
|
|
if ($note['content'] === 'file-source') { |
|
173
|
|
|
list($path, $line) = explode(':', $note['from'], 2); |
|
174
|
|
|
$sources[] = ['full_path' => $this->projectRoot.$path, 'path' => $path, 'line' => $line]; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $sources; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $domain |
|
183
|
|
|
* @param string $key |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function isNew($domain, $key) |
|
188
|
|
|
{ |
|
189
|
|
|
$notes = $this->getNotes($domain, $key); |
|
190
|
|
|
|
|
191
|
|
|
return $this->hasNoteNew($notes); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $domain |
|
196
|
|
|
* @param string $key |
|
197
|
|
|
* |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public function isObsolete($domain, $key) |
|
201
|
|
|
{ |
|
202
|
|
|
$notes = $this->getNotes($domain, $key); |
|
203
|
|
|
|
|
204
|
|
|
return $this->hasNoteObsolete($notes); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param $domain |
|
209
|
|
|
* @param $key |
|
210
|
|
|
* |
|
211
|
|
|
* @return array |
|
212
|
|
|
*/ |
|
213
|
|
|
private function getNotes($domain, $key, MessageCatalogue $catalogue = null) |
|
214
|
|
|
{ |
|
215
|
|
|
if (null === $catalogue) { |
|
216
|
|
|
/** @var MessageCatalogue $c */ |
|
217
|
|
|
$catalogue = reset($this->catalogues); |
|
218
|
|
|
} |
|
219
|
|
|
$meta = $catalogue->getMetadata($key, $domain); |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
if (!isset($meta['notes'])) { |
|
222
|
|
|
return []; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $meta['notes']; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param array $notes |
|
230
|
|
|
* |
|
231
|
|
|
* @return bool |
|
232
|
|
|
*/ |
|
233
|
|
|
private function hasNoteObsolete(array $notes) |
|
234
|
|
|
{ |
|
235
|
|
|
foreach ($notes as $note) { |
|
236
|
|
|
if ($note['content'] === 'status:obsolete') { |
|
237
|
|
|
return true; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param array $notes |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool |
|
248
|
|
|
*/ |
|
249
|
|
|
private function hasNoteNew(array $notes) |
|
250
|
|
|
{ |
|
251
|
|
|
foreach ($notes as $note) { |
|
252
|
|
|
if ($note['content'] === 'status:new') { |
|
253
|
|
|
return true; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return false; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: