Skip to content

Commit 0b808fc

Browse files
author
Olivier Dolbeau
authored
Merge pull request #369 from odolbeau/improve-download-command
Improve download command
2 parents 6726c8a + eaf8af4 commit 0b808fc

File tree

5 files changed

+60
-64
lines changed

5 files changed

+60
-64
lines changed

Command/DownloadCommand.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\Finder\Finder;
21+
use Translation\Bundle\Catalogue\CatalogueWriter;
2022
use Translation\Bundle\Service\CacheClearer;
2123
use Translation\Bundle\Service\ConfigurationManager;
2224
use Translation\Bundle\Service\StorageManager;
@@ -31,24 +33,21 @@ class DownloadCommand extends Command
3133

3234
protected static $defaultName = 'translation:download';
3335

34-
/**
35-
* @var ConfigurationManager
36-
*/
3736
private $configurationManager;
38-
39-
/**
40-
* @var CacheClearer
41-
*/
4237
private $cacheCleaner;
38+
private $catalogueWriter;
4339

4440
public function __construct(
4541
StorageManager $storageManager,
4642
ConfigurationManager $configurationManager,
47-
CacheClearer $cacheCleaner
43+
CacheClearer $cacheCleaner,
44+
CatalogueWriter $catalogueWriter
4845
) {
4946
$this->storageManager = $storageManager;
5047
$this->configurationManager = $configurationManager;
5148
$this->cacheCleaner = $cacheCleaner;
49+
$this->catalogueWriter = $catalogueWriter;
50+
5251
parent::__construct();
5352
}
5453

@@ -57,31 +56,55 @@ protected function configure(): void
5756
$this
5857
->setName(self::$defaultName)
5958
->setDescription('Replace local messages with messages from remote')
59+
->setHelp(<<<EOT
60+
The <info>%command.name%</info> will erase all your local translations and replace them with translations downloaded from the remote.
61+
EOT
62+
)
6063
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
61-
->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
64+
->addOption('cache', null, InputOption::VALUE_NONE, '[DEPRECATED] Cache is now automatically cleared when translations have changed.')
6265
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
6366
;
6467
}
6568

6669
protected function execute(InputInterface $input, OutputInterface $output): int
6770
{
71+
$io = new SymfonyStyle($input, $output);
72+
73+
if ($input->getOption('cache')) {
74+
$message = 'The --cache option is deprecated as it\'s now the default behaviour of this command.';
75+
76+
$io->note($message);
77+
@\trigger_error($message, E_USER_DEPRECATED);
78+
}
79+
6880
$configName = $input->getArgument('configuration');
6981
$config = $this->configurationManager->getConfiguration($configName);
7082
$storage = $this->getStorage($configName);
7183

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

74-
if ($input->getOption('cache')) {
75-
$translationsDirectory = $config->getOutputDir();
76-
$md5BeforeDownload = $this->hashDirectory($translationsDirectory);
77-
$storage->download();
78-
$md5AfterDownload = $this->hashDirectory($translationsDirectory);
86+
$translationsDirectory = $config->getOutputDir();
87+
$md5BeforeDownload = $this->hashDirectory($translationsDirectory);
88+
89+
$catalogues = $storage->download();
90+
$this->catalogueWriter->writeCatalogues($config, $catalogues);
7991

80-
if ($md5BeforeDownload !== $md5AfterDownload) {
81-
$this->cacheCleaner->clearAndWarmUp();
92+
$translationsCount = 0;
93+
foreach ($catalogues as $locale => $catalogue) {
94+
foreach ($catalogue->all() as $domain => $messages) {
95+
$translationsCount += \count($messages);
8296
}
97+
}
98+
99+
$io->text("<info>$translationsCount</info> translations have been downloaded.");
100+
101+
$md5AfterDownload = $this->hashDirectory($translationsDirectory);
102+
103+
if ($md5BeforeDownload !== $md5AfterDownload) {
104+
$io->success('Translations updated successfully!');
105+
$this->cacheCleaner->clearAndWarmUp();
83106
} else {
84-
$storage->download();
107+
$io->success('All translations were up to date.');
85108
}
86109

87110
return 0;

DependencyInjection/TranslationExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config): v
117117
* Configure storage chain service
118118
*/
119119
$storageDefinition = new ChildDefinition('php_translation.storage.abstract');
120-
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
120+
$storageDefinition->replaceArgument(1, new Reference($configurationServiceId));
121121
$storageDefinition->setPublic(true);
122122
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition);
123123
$storageManager->addMethodCall('addStorage', [$name, new Reference('php_translation.storage.'.$name)]);

Resources/config/console.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
- '@Translation\Bundle\Service\StorageManager'
1616
- '@Translation\Bundle\Service\ConfigurationManager'
1717
- '@Translation\Bundle\Service\CacheClearer'
18+
- '@Translation\Bundle\Catalogue\CatalogueWriter'
1819
tags:
1920
- { name: console.command, command: translation:download }
2021

Resources/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
php_translation.storage.abstract:
1111
class: Translation\Bundle\Service\StorageService
1212
abstract: true
13-
arguments: ['@Translation\Bundle\Catalogue\CatalogueFetcher', '@Translation\Bundle\Catalogue\CatalogueWriter', ~]
13+
arguments: ['@Translation\Bundle\Catalogue\CatalogueFetcher', ~]
1414

1515
Translation\Bundle\Catalogue\CatalogueManager:
1616
public: true

Service/StorageService.php

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Translation\MessageCatalogue;
1515
use Translation\Bundle\Catalogue\CatalogueFetcher;
16-
use Translation\Bundle\Catalogue\CatalogueWriter;
1716
use Translation\Bundle\Model\Configuration;
1817
use Translation\Common\Exception\LogicException;
1918
use Translation\Common\Model\Message;
@@ -42,40 +41,33 @@ final class StorageService implements Storage
4241
*/
4342
private $remoteStorages = [];
4443

45-
/**
46-
* @var CatalogueFetcher
47-
*/
4844
private $catalogueFetcher;
49-
50-
/**
51-
* @var CatalogueWriter
52-
*/
53-
private $catalogueWriter;
54-
55-
/**
56-
* @var Configuration
57-
*/
5845
private $config;
5946

60-
public function __construct(
61-
CatalogueFetcher $catalogueFetcher,
62-
CatalogueWriter $catalogueWriter,
63-
Configuration $config
64-
) {
47+
public function __construct(CatalogueFetcher $catalogueFetcher, Configuration $config)
48+
{
6549
$this->catalogueFetcher = $catalogueFetcher;
66-
$this->catalogueWriter = $catalogueWriter;
6750
$this->config = $config;
6851
}
6952

7053
/**
71-
* Download all remote storages into all local storages.
72-
* This will overwrite your local copy.
54+
* Download catalogues from all storages.
55+
*
56+
* @return MessageCatalogue[]
7357
*/
74-
public function download(): void
58+
public function download(): array
7559
{
76-
$catalogues = $this->doDownload();
60+
$catalogues = [];
61+
foreach ($this->config->getLocales() as $locale) {
62+
$catalogues[$locale] = new MessageCatalogue($locale);
63+
foreach ($this->remoteStorages as $storage) {
64+
if ($storage instanceof TransferableStorage) {
65+
$storage->export($catalogues[$locale]);
66+
}
67+
}
68+
}
7769

78-
$this->catalogueWriter->writeCatalogues($this->config, $catalogues);
70+
return $catalogues;
7971
}
8072

8173
/**
@@ -105,7 +97,7 @@ public function sync(string $direction = self::DIRECTION_DOWN): void
10597
*/
10698
public function mergeDown(): void
10799
{
108-
$catalogues = $this->doDownload();
100+
$catalogues = $this->download();
109101

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

265257
return $this;
266258
}
267-
268-
/**
269-
* Download catalogues from all storages.
270-
*
271-
* @return MessageCatalogue[]
272-
*/
273-
private function doDownload(): array
274-
{
275-
$catalogues = [];
276-
foreach ($this->config->getLocales() as $locale) {
277-
$catalogues[$locale] = new MessageCatalogue($locale);
278-
foreach ($this->remoteStorages as $storage) {
279-
if ($storage instanceof TransferableStorage) {
280-
$storage->export($catalogues[$locale]);
281-
}
282-
}
283-
}
284-
285-
return $catalogues;
286-
}
287259
}

0 commit comments

Comments
 (0)