1 | <?php |
||
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( |
|
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 | if (!file_exists($file)) { |
|
74 | 1 | $this->getFilesystem()->touch($file); |
|
75 | } |
||
76 | 1 | list($domain, $locale, $extension) = explode('.', $file); |
|
77 | 1 | if ($this->loadersContainer && $this->loadersContainer->has($extension)) { |
|
78 | $messageCatalogue = $this->loadersContainer->get($extension)->load($file, $locale, $domain); |
||
79 | $translations = array_merge($messageCatalogue->all($domain), $translations); |
||
80 | } |
||
81 | |||
82 | 1 | $this->exporter->export($file, $translations); |
|
83 | } |
||
84 | |||
85 | 1 | if (!empty($this->refresh)) { |
|
86 | 1 | $this->storage->write($this->refresh); |
|
87 | 1 | $this->refresh = []; |
|
88 | } |
||
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * Sets managed locales. |
||
93 | * |
||
94 | * @param array $managedLocales |
||
95 | */ |
||
96 | 4 | public function setManagedLocales($managedLocales) |
|
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | 5 | public function getManagedLocales() |
|
108 | |||
109 | /** |
||
110 | * Get translations for export. |
||
111 | * |
||
112 | * @param array $domains To read from storage. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 5 | private function readStorage($domains) |
|
117 | { |
||
118 | 5 | $data = []; |
|
119 | 5 | $translations = $this->storage->read($this->getManagedLocales(), $domains); |
|
120 | |||
121 | /** @var Translation $translation */ |
||
122 | 1 | foreach ($translations as $translation) { |
|
123 | 1 | $messages = $translation->getMessages(); |
|
124 | 1 | $wasDirty = false; |
|
125 | |||
126 | 1 | foreach ($messages as $key => $message) { |
|
127 | 1 | if ($message->getStatus() === Message::DIRTY) { |
|
128 | 1 | $path = sprintf( |
|
129 | 1 | '%s' . DIRECTORY_SEPARATOR . '%s.%s.%s', |
|
130 | 1 | $translation->getPath(), |
|
131 | 1 | $translation->getDomain(), |
|
132 | 1 | $message->getLocale(), |
|
133 | 1 | $translation->getFormat() |
|
134 | ); |
||
135 | 1 | $data[$path][$translation->getKey()] = $message->getMessage(); |
|
136 | |||
137 | 1 | $message->setStatus(Message::FRESH); |
|
138 | 1 | $messages[$key] = $message; |
|
139 | 1 | $wasDirty = true; |
|
140 | } |
||
141 | } |
||
142 | |||
143 | 1 | if ($wasDirty) { |
|
144 | 1 | $translation->setMessages($messages); |
|
145 | 1 | $this->refresh[] = $translation; |
|
146 | } |
||
147 | } |
||
148 | |||
149 | 1 | return $data; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return Filesystem |
||
154 | */ |
||
155 | protected function getFilesystem() |
||
159 | } |
||
160 |