Skip to content

Commit 0412e2d

Browse files
committed
Merge pull request #2 from gregi99/patch-1
cache tags + destruct at the end
2 parents 9f5edda + 56d4cd7 commit 0412e2d

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

Service/PhraseApp.php

+20-21
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class PhraseApp implements LoggerAwareInterface
6464
*/
6565
private $downloadedLocales = [];
6666

67+
/**
68+
* @var array
69+
*/
70+
private $tagCache = [];
71+
6772
/**
6873
* @var LoggerInterface
6974
*/
@@ -108,6 +113,11 @@ public function __construct(PhraseAppClient $client, TranslationLoader $translat
108113
$this->fileMerger = $fileMerger;
109114
}
110115

116+
public function __destruct()
117+
{
118+
$this->cleanUp();
119+
}
120+
111121
/**
112122
* Sets a logger instance on the object.
113123
*
@@ -151,26 +161,9 @@ public function getLocales()
151161
*/
152162
protected function downloadLocale($targetLocale)
153163
{
164+
154165
$sourceLocale = $this->locales[$targetLocale];
155166

156-
if (true === array_key_exists($sourceLocale, $this->downloadedLocales)) {
157-
$this->logger->notice('Copying translations for locale "{targetLocale}" from "{sourceLocale}".', [
158-
'targetLocale' => $targetLocale,
159-
'sourceLocale' => $sourceLocale
160-
]);
161-
162-
foreach ($this->catalogues as $catalogueName => $catalogueConfig) {
163-
$extension = $catalogueConfig['output_format'];
164-
165-
$tmpFile = sprintf('%s/%s.%s.%s', $this->getTmpPath(), $catalogueName, $targetLocale, $extension);
166-
167-
// Make copy because operated catalogues must belong to the same locale
168-
copy($this->downloadedLocales[$sourceLocale][$catalogueName], $tmpFile);
169-
}
170-
171-
return $this->downloadedLocales[$sourceLocale];
172-
}
173-
174167
$this->logger->notice('Downloading translations for locale "{targetLocale}" from "{sourceLocale}".', [
175168
'targetLocale' => $targetLocale,
176169
'sourceLocale' => $sourceLocale
@@ -229,14 +222,22 @@ protected function downloadLocale($targetLocale)
229222
*/
230223
protected function makeDownloadRequest($locale, $format, $tag)
231224
{
225+
if (array_key_exists($locale, $this->tagCache) && array_key_exists($tag, $this->tagCache[$locale])) {
226+
return $this->tagCache[$locale][$tag];
227+
}
228+
232229
$response = $this->client->request('locale.download', [
233230
'project_id' => $this->projectId,
234231
'id' => $locale,
235232
'file_format' => $format,
236233
'tag' => $tag,
237234
]);
238235

239-
return $response['text']->getContents();
236+
$content = $response['text']->getContents();
237+
238+
$this->tagCache[$locale][$tag] = $content;
239+
240+
return $content;
240241
}
241242

242243
/**
@@ -270,8 +271,6 @@ public function process(array $locales)
270271
$this->downloadLocale($locale);
271272
$this->dumpMessages($locale);
272273
}
273-
274-
$this->cleanUp();
275274
}
276275

277276
/**

0 commit comments

Comments
 (0)