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\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader; |
15
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
16
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
17
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriter; |
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 TranslationWriter |
31
|
|
|
*/ |
32
|
|
|
private $writer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TranslationLoader|SymfonyTranslationLoader |
36
|
|
|
*/ |
37
|
|
|
private $loader; |
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 catalogies |
51
|
|
|
*/ |
52
|
|
|
private $catalogues; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param TranslationWriter $writer |
56
|
|
|
* @param SymfonyTranslationLoader|TranslationLoader $loader |
57
|
|
|
* @param array $dir |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
10 |
|
public function __construct(TranslationWriter $writer, $loader, array $dir, array $options = []) |
61
|
|
|
{ |
62
|
10 |
|
if (!$loader instanceof SymfonyTranslationLoader && !$loader instanceof TranslationLoader) { |
63
|
1 |
|
throw new \LogicException('Second parameter of FileStorage must be a Symfony translation loader or implement Translation\SymfonyStorage\TranslationLoader'); |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
if (empty($dir)) { |
67
|
1 |
|
throw new \LogicException('Third parameter of FileStorage cannot be empty'); |
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
$this->writer = $writer; |
71
|
8 |
|
$this->loader = $loader; |
72
|
8 |
|
$this->dir = $dir; |
73
|
8 |
|
$this->options = $options; |
74
|
8 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function get($locale, $domain, $key) |
80
|
|
|
{ |
81
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
82
|
1 |
|
$translation = $catalogue->get($key, $domain); |
83
|
|
|
|
84
|
1 |
|
return new Message($key, $domain, $locale, $translation); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function create(Message $m) |
91
|
|
|
{ |
92
|
2 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
93
|
2 |
|
if (!$catalogue->defines($m->getKey(), $m->getDomain())) { |
94
|
2 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
95
|
2 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
96
|
2 |
|
} |
97
|
2 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function update(Message $m) |
103
|
|
|
{ |
104
|
1 |
|
$catalogue = $this->getCatalogue($m->getLocale()); |
105
|
1 |
|
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
106
|
1 |
|
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function delete($locale, $domain, $key) |
113
|
|
|
{ |
114
|
1 |
|
$catalogue = $this->getCatalogue($locale); |
115
|
1 |
|
$messages = $catalogue->all($domain); |
116
|
1 |
|
unset($messages[$key]); |
117
|
|
|
|
118
|
1 |
|
$catalogue->replace($messages, $domain); |
119
|
1 |
|
$this->writeCatalogue($catalogue, $locale, $domain); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
1 |
|
public function export(MessageCatalogueInterface $catalogue) |
126
|
|
|
{ |
127
|
1 |
|
$locale = $catalogue->getLocale(); |
128
|
1 |
|
$catalogue->addCatalogue($this->getCatalogue($locale)); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
1 |
|
public function import(MessageCatalogueInterface $catalogue) |
135
|
|
|
{ |
136
|
1 |
|
$domains = $catalogue->getDomains(); |
137
|
1 |
|
foreach ($domains as $domain) { |
138
|
1 |
|
$this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain); |
|
|
|
|
139
|
1 |
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Save catalogue back to file. |
144
|
|
|
* |
145
|
|
|
* @param MessageCatalogue $catalogue |
146
|
|
|
* @param string $domain |
147
|
|
|
*/ |
148
|
5 |
|
private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain) |
149
|
|
|
{ |
150
|
5 |
|
$resources = $catalogue->getResources(); |
151
|
5 |
|
$options = $this->options; |
152
|
5 |
|
$written = false; |
153
|
5 |
|
foreach ($resources as $resource) { |
154
|
3 |
|
$path = (string) $resource; |
155
|
3 |
|
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) { |
156
|
3 |
|
$options['path'] = str_replace($matches[0], '', $path); |
157
|
3 |
|
$this->writer->writeTranslations($catalogue, $matches[1], $options); |
158
|
3 |
|
$written = true; |
159
|
3 |
|
} |
160
|
5 |
|
} |
161
|
|
|
|
162
|
5 |
|
if ($written) { |
163
|
|
|
// We have written the translation to a file. |
164
|
3 |
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
$options['path'] = reset($this->dir); |
168
|
2 |
|
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf'; |
169
|
2 |
|
$this->writer->writeTranslations($catalogue, $format, $options); |
170
|
2 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $locale |
174
|
|
|
* |
175
|
|
|
* @return MessageCatalogue |
176
|
|
|
*/ |
177
|
6 |
|
private function getCatalogue($locale) |
178
|
|
|
{ |
179
|
6 |
|
if (empty($this->catalogues[$locale])) { |
180
|
6 |
|
$this->loadCatalogue($locale, $this->dir); |
181
|
6 |
|
} |
182
|
|
|
|
183
|
6 |
|
return $this->catalogues[$locale]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Load catalogue from files. |
188
|
|
|
* |
189
|
|
|
* @param string $locale |
190
|
|
|
* @param array $dirs |
191
|
|
|
*/ |
192
|
6 |
|
private function loadCatalogue($locale, array $dirs) |
193
|
|
|
{ |
194
|
6 |
|
$currentCatalogue = new MessageCatalogue($locale); |
195
|
6 |
|
foreach ($dirs as $path) { |
196
|
6 |
|
if (is_dir($path)) { |
197
|
5 |
|
$this->loader->loadMessages($path, $currentCatalogue); |
198
|
5 |
|
} |
199
|
6 |
|
} |
200
|
|
|
|
201
|
6 |
|
$this->catalogues[$locale] = $currentCatalogue; |
202
|
6 |
|
} |
203
|
|
|
} |
204
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.