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\Service; |
13
|
|
|
|
14
|
|
|
use Translation\Common\Exception\LogicException; |
15
|
|
|
use Translation\Common\Model\Message; |
16
|
|
|
use Translation\Common\Storage; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A service that you use to handle the storages. |
20
|
|
|
* |
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class StorageService implements Storage |
24
|
|
|
{ |
25
|
|
|
const DIRECTION_UP = 'up'; |
26
|
|
|
const DIRECTION_DOWN = 'down'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Storage[] |
30
|
|
|
*/ |
31
|
|
|
private $localStorages = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Storage[] |
35
|
|
|
*/ |
36
|
|
|
private $remoteStorages = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Download all remote storages into all local storages. |
40
|
|
|
* This will overwrite your local copy. |
41
|
|
|
*/ |
42
|
|
|
public function download() |
43
|
|
|
{ |
44
|
|
|
// TODO |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Upload all local storages into all remote storages |
49
|
|
|
* This will overwrite your remote copy. |
50
|
|
|
*/ |
51
|
|
|
public function upload() |
52
|
|
|
{ |
53
|
|
|
// TODO |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Synchronize translations with remote. |
58
|
|
|
*/ |
59
|
|
|
public function sync($direction = self::DIRECTION_DOWN) |
60
|
|
|
{ |
61
|
|
|
switch ($direction) { |
62
|
|
|
case self::DIRECTION_DOWN: |
63
|
|
|
$this->mergeDown(); |
64
|
|
|
$this->mergeUp(); |
65
|
|
|
break; |
66
|
|
|
case self::DIRECTION_UP: |
67
|
|
|
$this->mergeUp(); |
68
|
|
|
$this->mergeDown(); |
69
|
|
|
break; |
70
|
|
|
default: |
71
|
|
|
throw new LogicException(sprintf('Direction must be either "up" or "down". Value "%s" was provided', $direction)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Download and merge all translations from remote storages down to your local storages. |
77
|
|
|
* Only the local storages will be changed. |
78
|
|
|
*/ |
79
|
|
|
public function mergeDown() |
80
|
|
|
{ |
81
|
|
|
// TODO |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Upload and merge all translations from local storages up to your remote storages. |
86
|
|
|
* Only the remote storages will be changed. |
87
|
|
|
*/ |
88
|
|
|
public function mergeUp() |
89
|
|
|
{ |
90
|
|
|
// TODO |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the very latest version we know of a message. First look at the remote storage |
95
|
|
|
* fall back on the local ones. |
96
|
|
|
* |
97
|
|
|
* @param string $locale |
98
|
|
|
* @param string $domain |
99
|
|
|
* @param string $key |
100
|
|
|
* |
101
|
|
|
* @return null|Message |
102
|
|
|
*/ |
103
|
|
|
public function syncAndFetchMessage($locale, $domain, $key) |
104
|
|
|
{ |
105
|
|
|
$message = $this->getFromStorages($this->remoteStorages, $locale, $domain, $key); |
106
|
|
|
if (!$message) { |
|
|
|
|
107
|
|
|
// If message is not in remote storages |
108
|
|
|
$message = $this->getFromStorages($this->localStorages, $locale, $domain, $key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->updateStorages($this->localStorages, $message); |
|
|
|
|
112
|
|
|
|
113
|
|
|
return $message; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Try to get a translation from all the storages, start looking in the first |
118
|
|
|
* local storage and then move on to the remote storages. |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function get($locale, $domain, $key) |
122
|
|
|
{ |
123
|
|
|
foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
124
|
|
|
$value = $this->getFromStorages($storages, $locale, $domain, $key); |
125
|
|
|
if (!empty($value)) { |
126
|
|
|
return $value; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Storage[] $storages |
135
|
|
|
* @param string $locale |
136
|
|
|
* @param string $domain |
137
|
|
|
* @param string $key |
138
|
|
|
* |
139
|
|
|
* @return null|Message |
140
|
|
|
*/ |
141
|
|
|
private function getFromStorages($storages, $locale, $domain, $key) |
142
|
|
|
{ |
143
|
|
|
foreach ($storages as $storage) { |
144
|
|
|
$value = $storage->get($locale, $domain, $key); |
145
|
|
|
if (!empty($value)) { |
146
|
|
|
return $value; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Update all configured storages with this message. |
155
|
|
|
* |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function update(Message $message) |
159
|
|
|
{ |
160
|
|
|
foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
161
|
|
|
$this->updateStorages($storages, $message); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Storage[] $storages |
167
|
|
|
* @param Message $message |
168
|
|
|
*/ |
169
|
|
|
private function updateStorages($storages, Message $message) |
170
|
|
|
{ |
171
|
|
|
// Validate if message actually has data |
172
|
|
|
if (empty((array) $message)) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($storages as $storage) { |
177
|
|
|
$storage->update($message); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Delete the message form all storages. |
183
|
|
|
* |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function delete($locale, $domain, $key) |
187
|
|
|
{ |
188
|
|
|
foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
189
|
|
|
$this->deleteFromStorages($storages, $locale, $domain, $key); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param Storage[] $storages |
195
|
|
|
* @param string $locale |
196
|
|
|
* @param string $domain |
197
|
|
|
* @param string $key |
198
|
|
|
*/ |
199
|
|
|
private function deleteFromStorages($storages, $locale, $domain, $key) |
200
|
|
|
{ |
201
|
|
|
foreach ($storages as $storage) { |
202
|
|
|
$storage->delete($locale, $domain, $key); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param Storage $localStorage |
208
|
|
|
* |
209
|
|
|
* @return StorageService |
210
|
|
|
*/ |
211
|
|
|
public function addLocalStorage(Storage $localStorage) |
212
|
|
|
{ |
213
|
|
|
$this->localStorages[] = $localStorage; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param Storage $remoteStorages |
|
|
|
|
220
|
|
|
* |
221
|
|
|
* @return StorageService |
222
|
|
|
*/ |
223
|
|
|
public function addRemoteStorage(Storage $remoteStorage) |
224
|
|
|
{ |
225
|
|
|
$this->remoteStorages[] = $remoteStorage; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: