1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\Service; |
13
|
|
|
|
14
|
|
|
use ONGR\TranslationsBundle\Document\Message; |
15
|
|
|
use ONGR\TranslationsBundle\Document\Translation; |
16
|
|
|
use ONGR\TranslationsBundle\Storage\StorageInterface; |
17
|
|
|
use ONGR\TranslationsBundle\Translation\Export\ExporterInterface; |
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Export. |
22
|
|
|
*/ |
23
|
|
|
class Export |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var StorageInterface |
27
|
|
|
*/ |
28
|
|
|
private $storage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ExporterInterface |
32
|
|
|
*/ |
33
|
|
|
private $exporter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LoadersContainer |
37
|
|
|
*/ |
38
|
|
|
private $loadersContainer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $managedLocales = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Translation[] |
47
|
|
|
*/ |
48
|
|
|
private $refresh = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param LoadersContainer $loadersContainer |
52
|
|
|
* @param StorageInterface $storage |
53
|
|
|
* @param ExporterInterface $exporter |
54
|
|
|
*/ |
55
|
5 |
|
public function __construct( |
56
|
|
|
LoadersContainer $loadersContainer, |
57
|
|
|
StorageInterface $storage, |
58
|
|
|
ExporterInterface $exporter |
59
|
|
|
) { |
60
|
5 |
|
$this->storage = $storage; |
61
|
5 |
|
$this->exporter = $exporter; |
62
|
5 |
|
$this->loadersContainer = $loadersContainer; |
63
|
5 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Exports translations from ES to files. |
67
|
|
|
* |
68
|
|
|
* @param array $domains To export. |
69
|
|
|
*/ |
70
|
5 |
|
public function export($domains = []) |
71
|
|
|
{ |
72
|
5 |
|
foreach ($this->readStorage($domains) as $file => $translations) { |
73
|
1 |
|
// TODO: refactor after ESB gives easy way to access object! |
74
|
1 |
|
$translations = $translations instanceof \Traversable ? iterator_to_array($translations) : $translations; |
75
|
|
|
if (!file_exists($file)) { |
76
|
1 |
|
$this->getFilesystem()->touch($file); |
77
|
1 |
|
} |
78
|
|
|
list($domain, $locale, $extension) = explode('.', $file); |
79
|
|
|
if ($this->loadersContainer && $this->loadersContainer->has($extension)) { |
80
|
|
|
$messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain); |
81
|
|
|
$translations = array_merge($messageCatalogue->all($domain), $translations); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
$this->exporter->export($file, $translations); |
85
|
1 |
|
} |
86
|
1 |
|
|
87
|
1 |
|
if (!empty($this->refresh)) { |
88
|
|
|
$this->storage->write($this->refresh); |
89
|
1 |
|
$this->refresh = []; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets managed locales. |
95
|
|
|
* |
96
|
4 |
|
* @param array $managedLocales |
97
|
|
|
*/ |
98
|
4 |
|
public function setManagedLocales($managedLocales) |
99
|
4 |
|
{ |
100
|
|
|
$this->managedLocales = $managedLocales; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
5 |
|
* @return array |
105
|
|
|
*/ |
106
|
5 |
|
public function getManagedLocales() |
107
|
|
|
{ |
108
|
|
|
return $this->managedLocales; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get translations for export. |
113
|
|
|
* |
114
|
|
|
* @param array $domains To read from storage. |
115
|
|
|
* |
116
|
5 |
|
* @return array |
117
|
|
|
*/ |
118
|
5 |
|
private function readStorage($domains) |
119
|
5 |
|
{ |
120
|
|
|
$data = []; |
121
|
|
|
$translations = $this->storage->read($this->getManagedLocales(), $domains); |
122
|
1 |
|
|
123
|
1 |
|
/** @var Translation $translation */ |
124
|
1 |
|
foreach ($translations as $translation) { |
125
|
|
|
$messages = $translation->getMessages(); |
126
|
1 |
|
// TODO: refactor after ESB gives easy way to access object! |
127
|
1 |
|
$messages = $messages instanceof \Traversable ? iterator_to_array($messages) : $messages; |
128
|
1 |
|
$wasDirty = false; |
129
|
1 |
|
|
130
|
1 |
|
foreach ($messages as $key => $message) { |
131
|
1 |
|
if ($message->getStatus() === Message::DIRTY) { |
132
|
1 |
|
$path = sprintf( |
133
|
1 |
|
'%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
134
|
|
|
$translation->getPath(), |
135
|
1 |
|
$translation->getDomain(), |
136
|
|
|
$message->getLocale(), |
137
|
1 |
|
$translation->getFormat() |
138
|
1 |
|
); |
139
|
1 |
|
$data[$path][$translation->getKey()] = $message->getMessage(); |
140
|
|
|
|
141
|
|
|
$message->setStatus(Message::FRESH); |
142
|
|
|
$messages[$key] = $message; |
143
|
1 |
|
$wasDirty = true; |
144
|
1 |
|
} |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
if ($wasDirty) { |
148
|
|
|
$translation->setMessages($messages); |
149
|
1 |
|
$this->refresh[] = $translation; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $data; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return Filesystem |
158
|
|
|
*/ |
159
|
|
|
protected function getFilesystem() |
160
|
|
|
{ |
161
|
|
|
return new Filesystem(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|