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\SymfonyStorage; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
15
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
16
|
|
|
use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
17
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriterInterface; |
18
|
|
|
use Translation\Common\Model\Message; |
19
|
|
|
use Translation\Common\Storage; |
20
|
|
|
use Translation\Common\TransferableStorage; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This storage uses Symfony's writer and loader. |
24
|
|
|
* |
25
|
|
|
* @author Tobias Nyholm <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class FileStorage implements Storage, TransferableStorage |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var TranslationWriterInterface |
31
|
|
|
*/ |
32
|
|
|
private $writer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TranslationReaderInterface |
36
|
|
|
*/ |
37
|
|
|
private $reader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array directory path |
41
|
|
|
*/ |
42
|
|
|
private $dir; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array with option to the dumper |
46
|
|
|
*/ |
47
|
|
|
private $options; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MessageCatalogue[] Fetched catalogues |
51
|
|
|
*/ |
52
|
|
|
private $catalogues; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param TranslationWriterInterface $writer |
56
|
|
|
* @param TranslationReaderInterface $reader |
57
|
|
|
* @param array $dir |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
10 |
|
public function __construct($writer, $reader, array $dir, array $options = []) |
61
|
|
|
{ |
62
|
|
|
// Create a wrapper for legacy writer |
63
|
10 |
|
if (!$writer instanceof TranslationWriterInterface) { |
64
|
|
|
$writer = new LegacyTranslationWriter($writer); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Create a wrapper for legacy reader |
68
|
10 |
|
if (!$reader instanceof TranslationReaderInterface) { |
69
|
1 |
|
$reader = new LegacyTranslationReader($reader); |
70
|
|
|
} |
71
|
|
|
|
72
|
9 |
|
if (empty($dir)) { |
73
|
1 |
|
throw new \LogicException('Third parameter of FileStorage cannot be empty'); |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
if (!array_key_exists('xliff_version', $options)) { |
77
|
|
|
// Set default value for xliff version. |
78
|
8 |
|
$options['xliff_version'] = '2.0'; |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
$this->writer = $writer; |
|
|
|
|
82
|
8 |
|
$this->reader = $reader; |
|
|
|
|
83
|
8 |
|
$this->dir = $dir; |
84
|
8 |
|
$this->options = $options; |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function get($locale, $domain, $key) |
91
|
|
|
{ |
92
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
93
|
1 |
|
$translation = $catalogue->get($key, $domain); |
94
|
|
|
|
95
|
1 |
|
return new Message($key, $domain, $locale, $translation); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
2 |
|
public function create(Message $m) |
102
|
|
|
{ |
103
|
2 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
104
|
2 |
|
if (!$catalogue->defines($m->getKey(), $m->getDomain())) { |
105
|
2 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
106
|
2 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
107
|
|
|
} |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
1 |
|
public function update(Message $m) |
114
|
|
|
{ |
115
|
1 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
116
|
1 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
117
|
1 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
1 |
|
public function delete($locale, $domain, $key) |
124
|
|
|
{ |
125
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
126
|
1 |
|
$messages = $catalogue->all($domain); |
127
|
1 |
|
unset($messages[$key]); |
128
|
|
|
|
129
|
1 |
|
$catalogue->replace($messages, $domain); |
130
|
1 |
|
$this->writeCatalogue($catalogue, $locale, $domain); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function export(MessageCatalogueInterface $catalogue) |
137
|
|
|
{ |
138
|
1 |
|
$locale = $catalogue->getLocale(); |
139
|
1 |
|
$catalogue->addCatalogue($this->getCatalogue($locale)); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function import(MessageCatalogueInterface $catalogue) |
146
|
|
|
{ |
147
|
1 |
|
$domains = $catalogue->getDomains(); |
148
|
1 |
|
foreach ($domains as $domain) { |
149
|
1 |
|
$this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain); |
150
|
|
|
} |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Save catalogue back to file. |
155
|
|
|
* |
156
|
|
|
* @param MessageCatalogueInterface $catalogue |
157
|
|
|
* @param string $domain |
158
|
|
|
*/ |
159
|
5 |
|
private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $domain) |
160
|
|
|
{ |
161
|
5 |
|
$resources = $catalogue->getResources(); |
162
|
5 |
|
$options = $this->options; |
163
|
5 |
|
$written = false; |
164
|
5 |
|
foreach ($resources as $resource) { |
165
|
3 |
|
$path = (string) $resource; |
166
|
3 |
|
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) { |
167
|
3 |
|
$options['path'] = str_replace($matches[0], '', $path); |
168
|
3 |
|
$this->writer->write($catalogue, $matches[1], $options); |
|
|
|
|
169
|
3 |
|
$written = true; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
5 |
|
if ($written) { |
174
|
|
|
// We have written the translation to a file. |
175
|
3 |
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
$options['path'] = reset($this->dir); |
179
|
2 |
|
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf'; |
180
|
2 |
|
$this->writer->write($catalogue, $format, $options); |
|
|
|
|
181
|
2 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $locale |
185
|
|
|
* |
186
|
|
|
* @return MessageCatalogue |
187
|
|
|
*/ |
188
|
6 |
|
private function getCatalogue($locale) |
189
|
|
|
{ |
190
|
6 |
|
if (empty($this->catalogues[$locale])) { |
191
|
6 |
|
$this->loadCatalogue($locale, $this->dir); |
192
|
|
|
} |
193
|
|
|
|
194
|
6 |
|
return $this->catalogues[$locale]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Load catalogue from files. |
199
|
|
|
* |
200
|
|
|
* @param string $locale |
201
|
|
|
* @param array $dirs |
202
|
|
|
*/ |
203
|
6 |
|
private function loadCatalogue($locale, array $dirs) |
204
|
|
|
{ |
205
|
6 |
|
$currentCatalogue = new MessageCatalogue($locale); |
206
|
6 |
|
foreach ($dirs as $path) { |
207
|
6 |
|
if (is_dir($path)) { |
208
|
6 |
|
$this->reader->read($path, $currentCatalogue); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
6 |
|
$this->catalogues[$locale] = $currentCatalogue; |
213
|
6 |
|
} |
214
|
|
|
} |
215
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.