Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Translation\Bundle\Catalogue\CatalogueWriter;
use Translation\Bundle\Service\CacheClearer;
use Translation\Bundle\Service\ConfigurationManager;
use Translation\Bundle\Service\StorageManager;
Expand All @@ -31,24 +33,21 @@ class DownloadCommand extends Command

protected static $defaultName = 'translation:download';

/**
* @var ConfigurationManager
*/
private $configurationManager;

/**
* @var CacheClearer
*/
private $cacheCleaner;
private $catalogueWriter;

public function __construct(
StorageManager $storageManager,
ConfigurationManager $configurationManager,
CacheClearer $cacheCleaner
CacheClearer $cacheCleaner,
CatalogueWriter $catalogueWriter
) {
$this->storageManager = $storageManager;
$this->configurationManager = $configurationManager;
$this->cacheCleaner = $cacheCleaner;
$this->catalogueWriter = $catalogueWriter;

parent::__construct();
}

Expand All @@ -57,31 +56,55 @@ protected function configure(): void
$this
->setName(self::$defaultName)
->setDescription('Replace local messages with messages from remote')
->setHelp(<<<EOT
The <info>%command.name%</info> will erase all your local translations and replace them with translations downloaded from the remote.
EOT
)
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
->addOption('cache', null, InputOption::VALUE_NONE, '[DEPRECATED] Cache is now automatically cleared when translations have changed.')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about this?

->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

if ($input->getOption('cache')) {
$message = 'The --cache option is deprecated as it\'s now the default behaviour of this command.';

$io->note($message);
@\trigger_error($message, E_USER_DEPRECATED);
}

$configName = $input->getArgument('configuration');
$config = $this->configurationManager->getConfiguration($configName);
$storage = $this->getStorage($configName);

$this->configureBundleDirs($input, $config);

if ($input->getOption('cache')) {
$translationsDirectory = $config->getOutputDir();
$md5BeforeDownload = $this->hashDirectory($translationsDirectory);
$storage->download();
$md5AfterDownload = $this->hashDirectory($translationsDirectory);
$translationsDirectory = $config->getOutputDir();
$md5BeforeDownload = $this->hashDirectory($translationsDirectory);

$catalogues = $storage->download();
$this->catalogueWriter->writeCatalogues($config, $catalogues);

if ($md5BeforeDownload !== $md5AfterDownload) {
$this->cacheCleaner->clearAndWarmUp();
$translationsCount = 0;
foreach ($catalogues as $locale => $catalogue) {
foreach ($catalogue->all() as $domain => $messages) {
$translationsCount += \count($messages);
}
}

$io->text("<info>$translationsCount</info> translations have been downloaded.");

$md5AfterDownload = $this->hashDirectory($translationsDirectory);

if ($md5BeforeDownload !== $md5AfterDownload) {
$io->success('Translations updated successfully!');
$this->cacheCleaner->clearAndWarmUp();
} else {
$storage->download();
$io->success('All translations were up to date.');
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config): v
* Configure storage chain service
*/
$storageDefinition = new ChildDefinition('php_translation.storage.abstract');
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
$storageDefinition->replaceArgument(1, new Reference($configurationServiceId));
$storageDefinition->setPublic(true);
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition);
$storageManager->addMethodCall('addStorage', [$name, new Reference('php_translation.storage.'.$name)]);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/console.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
- '@Translation\Bundle\Service\StorageManager'
- '@Translation\Bundle\Service\ConfigurationManager'
- '@Translation\Bundle\Service\CacheClearer'
- '@Translation\Bundle\Catalogue\CatalogueWriter'
tags:
- { name: console.command, command: translation:download }

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
php_translation.storage.abstract:
class: Translation\Bundle\Service\StorageService
abstract: true
arguments: ['@Translation\Bundle\Catalogue\CatalogueFetcher', '@Translation\Bundle\Catalogue\CatalogueWriter', ~]
arguments: ['@Translation\Bundle\Catalogue\CatalogueFetcher', ~]

Translation\Bundle\Catalogue\CatalogueManager:
public: true
Expand Down
62 changes: 17 additions & 45 deletions Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Translation\MessageCatalogue;
use Translation\Bundle\Catalogue\CatalogueFetcher;
use Translation\Bundle\Catalogue\CatalogueWriter;
use Translation\Bundle\Model\Configuration;
use Translation\Common\Exception\LogicException;
use Translation\Common\Model\Message;
Expand Down Expand Up @@ -42,40 +41,33 @@ final class StorageService implements Storage
*/
private $remoteStorages = [];

/**
* @var CatalogueFetcher
*/
private $catalogueFetcher;

/**
* @var CatalogueWriter
*/
private $catalogueWriter;

/**
* @var Configuration
*/
private $config;

public function __construct(
CatalogueFetcher $catalogueFetcher,
CatalogueWriter $catalogueWriter,
Configuration $config
) {
public function __construct(CatalogueFetcher $catalogueFetcher, Configuration $config)
{
$this->catalogueFetcher = $catalogueFetcher;
$this->catalogueWriter = $catalogueWriter;
$this->config = $config;
}

/**
* Download all remote storages into all local storages.
* This will overwrite your local copy.
* Download catalogues from all storages.
*
* @return MessageCatalogue[]
*/
public function download(): void
public function download(): array
{
$catalogues = $this->doDownload();
$catalogues = [];
foreach ($this->config->getLocales() as $locale) {
$catalogues[$locale] = new MessageCatalogue($locale);
foreach ($this->remoteStorages as $storage) {
if ($storage instanceof TransferableStorage) {
$storage->export($catalogues[$locale]);
}
}
}

$this->catalogueWriter->writeCatalogues($this->config, $catalogues);
return $catalogues;
}

/**
Expand Down Expand Up @@ -105,7 +97,7 @@ public function sync(string $direction = self::DIRECTION_DOWN): void
*/
public function mergeDown(): void
{
$catalogues = $this->doDownload();
$catalogues = $this->download();

foreach ($catalogues as $locale => $catalogue) {
foreach ($catalogue->all() as $domain => $messages) {
Expand Down Expand Up @@ -264,24 +256,4 @@ public function addRemoteStorage(Storage $remoteStorage): self

return $this;
}

/**
* Download catalogues from all storages.
*
* @return MessageCatalogue[]
*/
private function doDownload(): array
{
$catalogues = [];
foreach ($this->config->getLocales() as $locale) {
$catalogues[$locale] = new MessageCatalogue($locale);
foreach ($this->remoteStorages as $storage) {
if ($storage instanceof TransferableStorage) {
$storage->export($catalogues[$locale]);
}
}
}

return $catalogues;
}
}