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