Skip to content

Commit

Permalink
Added logger to service
Browse files Browse the repository at this point in the history
  • Loading branch information
nediam committed Sep 16, 2015
1 parent d6ce69f commit 28dfe2c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
25 changes: 13 additions & 12 deletions Command/PhraseAppUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,19 @@


use nediam\PhraseAppBundle\Service\PhraseApp;
use Psr\Log\LogLevel;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Translation\Catalogue\DiffOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;

class PhraseAppUpdateCommand extends ContainerAwareCommand
{
private $availableLocales;
/** @var TranslationLoader */
private $loader;
/** @var TranslationWriter */
private $writer;
/** @var PhraseApp */
private $phraseApp;
/** @var string */
private $translationsPath;
/** @var string */
private $tmpPath;
/** @var array */
private $validators = [];
/** @var array */
Expand Down Expand Up @@ -85,6 +75,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->phraseApp = $container->get('phrase_app.service');
$this->availableLocales = $this->phraseApp->getLocales();

$this->phraseApp->setLogger(new ConsoleLogger($output, [
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE,
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
]));

$unsupportedLocales = array_diff($this->locales, array_keys($this->availableLocales));
if (count($unsupportedLocales)) {
throw new RuntimeException(sprintf('Unsupported locales "%s"', implode(', ', $unsupportedLocales)));
Expand Down
1 change: 1 addition & 0 deletions Resources/config/phraseapp-services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument id="translation.loader" type="service"/>
<argument id="translation.writer" type="service"/>
<argument>%phrase_app.config%</argument>
<argument id="logger" type="service" on-invalid="null"/>
</service>
</services>

Expand Down
37 changes: 35 additions & 2 deletions Service/PhraseApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@


use nediam\PhraseApp\PhraseAppClient;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Translation\Catalogue\DiffOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;

class PhraseApp
class PhraseApp implements LoggerAwareInterface
{
/**
* @var PhraseAppClient
Expand Down Expand Up @@ -52,16 +54,21 @@ class PhraseApp
* @var array
*/
private $downloadedLocales = [];
/**
* @var LoggerInterface
*/
private $logger;

/**
* PhraseApp constructor.
*
* @param PhraseAppClient $client
* @param TranslationLoader $translationLoader
* @param TranslationWriter $translationWriter
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct(PhraseAppClient $client, TranslationLoader $translationLoader, TranslationWriter $translationWriter, array $config)
public function __construct(PhraseAppClient $client, TranslationLoader $translationLoader, TranslationWriter $translationWriter, array $config, LoggerInterface $logger = null)
{
$this->client = $client;
$this->translationLoader = $translationLoader;
Expand All @@ -70,6 +77,19 @@ public function __construct(PhraseAppClient $client, TranslationLoader $translat
$this->locales = $config['locales'];
$this->outputFormat = $config['output_format'];
$this->translationsPath = $config['translations_path'];
$this->logger = $logger;
}

/**
* Sets a logger instance on the object.
*
* @param LoggerInterface $logger
*
* @return null
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -123,12 +143,20 @@ protected function downloadLocale($targetLocale)
$tmpFile = $this->getTmpPath() . '/' . 'messages.' . $targetLocale . '.yml';

if (true === array_key_exists($sourceLocale, $this->downloadedLocales)) {
$this->logger->notice(strtr('Copying translations for locale "{targetLocale}" from "{sourceLocale}".', [
'{targetLocale}' => $targetLocale,
'{sourceLocale}' => $sourceLocale
]));
// Make copy because operated catalogues must belong to the same locale
copy($this->downloadedLocales[$sourceLocale], $tmpFile);

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

$this->logger->notice(strtr('Downloading translations for locale "{targetLocale}" from "{sourceLocale}".', [
'{targetLocale}' => $targetLocale,
'{sourceLocale}' => $sourceLocale
]));
$phraseAppMessage = $this->makeDownloadRequest($sourceLocale, 'yml_symfony2');
file_put_contents($tmpFile, $phraseAppMessage);
$this->downloadedLocales[$sourceLocale] = $tmpFile;
Expand All @@ -142,20 +170,25 @@ protected function downloadLocale($targetLocale)
protected function dumpMessages($targetLocale)
{
// load downloaded messages
$this->logger->notice(strtr('Loading downloaded catalogues from "{tmpPath}"', ['{tmpPath}' => $this->getTmpPath()]));
$extractedCatalogue = new MessageCatalogue($targetLocale);
$this->translationLoader->loadMessages($this->getTmpPath(), $extractedCatalogue);

// load any existing messages from the translation files
$this->logger->notice(strtr('Loading existing catalogues from "{translationsPath}"', ['{translationsPath}' => $this->translationsPath]));
$currentCatalogue = new MessageCatalogue($targetLocale);
$this->translationLoader->loadMessages($this->translationsPath, $currentCatalogue);

$operation = new DiffOperation($currentCatalogue, $extractedCatalogue);

// Exit if no messages found.
if (0 === count($operation->getDomains())) {
$this->logger->warning(strtr('No translation found for locale {locale}', ['{locale}' => $targetLocale]));

return;
}

$this->logger->notice(strtr('Writing translation file for locale "{locale}".', ['{locale}' => $targetLocale]));
$this->translationWriter->writeTranslations($operation->getResult(), $this->outputFormat, ['path' => $this->translationsPath]);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
}
},
"require": {
"nediam/phraseapp-lib": "^1.0"
"nediam/phraseapp-lib": ">=1.2 <2.0.0"
}
}

0 comments on commit 28dfe2c

Please sign in to comment.