Skip to content

Commit

Permalink
Merge pull request #2 from gregi99/patch-1
Browse files Browse the repository at this point in the history
cache tags + destruct at the end
  • Loading branch information
nediam committed Feb 11, 2016
2 parents 9f5edda + 56d4cd7 commit 0412e2d
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions Service/PhraseApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class PhraseApp implements LoggerAwareInterface
*/
private $downloadedLocales = [];

/**
* @var array
*/
private $tagCache = [];

/**
* @var LoggerInterface
*/
Expand Down Expand Up @@ -108,6 +113,11 @@ public function __construct(PhraseAppClient $client, TranslationLoader $translat
$this->fileMerger = $fileMerger;
}

public function __destruct()
{
$this->cleanUp();
}

/**
* Sets a logger instance on the object.
*
Expand Down Expand Up @@ -151,26 +161,9 @@ public function getLocales()
*/
protected function downloadLocale($targetLocale)
{

$sourceLocale = $this->locales[$targetLocale];

if (true === array_key_exists($sourceLocale, $this->downloadedLocales)) {
$this->logger->notice('Copying translations for locale "{targetLocale}" from "{sourceLocale}".', [
'targetLocale' => $targetLocale,
'sourceLocale' => $sourceLocale
]);

foreach ($this->catalogues as $catalogueName => $catalogueConfig) {
$extension = $catalogueConfig['output_format'];

$tmpFile = sprintf('%s/%s.%s.%s', $this->getTmpPath(), $catalogueName, $targetLocale, $extension);

// Make copy because operated catalogues must belong to the same locale
copy($this->downloadedLocales[$sourceLocale][$catalogueName], $tmpFile);
}

return $this->downloadedLocales[$sourceLocale];
}

$this->logger->notice('Downloading translations for locale "{targetLocale}" from "{sourceLocale}".', [
'targetLocale' => $targetLocale,
'sourceLocale' => $sourceLocale
Expand Down Expand Up @@ -229,14 +222,22 @@ protected function downloadLocale($targetLocale)
*/
protected function makeDownloadRequest($locale, $format, $tag)
{
if (array_key_exists($locale, $this->tagCache) && array_key_exists($tag, $this->tagCache[$locale])) {
return $this->tagCache[$locale][$tag];
}

$response = $this->client->request('locale.download', [
'project_id' => $this->projectId,
'id' => $locale,
'file_format' => $format,
'tag' => $tag,
]);

return $response['text']->getContents();
$content = $response['text']->getContents();

$this->tagCache[$locale][$tag] = $content;

return $content;
}

/**
Expand Down Expand Up @@ -270,8 +271,6 @@ public function process(array $locales)
$this->downloadLocale($locale);
$this->dumpMessages($locale);
}

$this->cleanUp();
}

/**
Expand Down

0 comments on commit 0412e2d

Please sign in to comment.