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\Message; |
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 Message[] |
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 Message($this, $locale, $domain, $key, $text); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $messages; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $domain |
86
|
|
|
* @param string $key |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getTranslations($domain, $key) |
91
|
|
|
{ |
92
|
|
|
$translations = []; |
93
|
|
|
foreach ($this->catalogues as $locale => $catalogue) { |
94
|
|
|
if ($catalogue->has($key, $domain)) { |
95
|
|
|
$translations[$locale] = $catalogue->get($key, $domain); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $translations; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $domain |
104
|
|
|
* @param string $key |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getSourceLocations($domain, $key) |
109
|
|
|
{ |
110
|
|
|
$notes = $this->getNotes($domain, $key); |
111
|
|
|
$sources = []; |
112
|
|
|
foreach ($notes as $note) { |
113
|
|
|
if ($note['content'] === 'file-source') { |
114
|
|
|
list($path, $line) = explode(':', $note['from'], 2); |
115
|
|
|
$sources[] = ['full_path' => $this->projectRoot.$path, 'path' => $path, 'line' => $line]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $sources; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $domain |
124
|
|
|
* @param string $key |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function isNew($domain, $key) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$notes = $this->getNotes($domain, $key); |
131
|
|
|
foreach ($notes as $note) { |
132
|
|
|
if ($note['content'] === 'status:new') { |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $domain |
142
|
|
|
* @param string $key |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function isObsolete($domain, $key) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$notes = $this->getNotes($domain, $key); |
149
|
|
|
foreach ($notes as $note) { |
150
|
|
|
if ($note['content'] === 'status:obsolete') { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $domain |
160
|
|
|
* @param $key |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
private function getNotes($domain, $key) |
165
|
|
|
{ |
166
|
|
|
/** @var MessageCatalogue $c */ |
167
|
|
|
$c = reset($this->catalogues); |
168
|
|
|
$meta = $c->getMetadata($key, $domain); |
169
|
|
|
|
170
|
|
|
if (!isset($meta['notes'])) { |
171
|
|
|
return []; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $meta['notes']; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.