diff --git a/Command/DownloadCommand.php b/Command/DownloadCommand.php index 02215e9b..990093c6 100644 --- a/Command/DownloadCommand.php +++ b/Command/DownloadCommand.php @@ -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; @@ -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(); } @@ -57,31 +56,55 @@ protected function configure(): void $this ->setName(self::$defaultName) ->setDescription('Replace local messages with messages from remote') + ->setHelp(<<%command.name% 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.') ->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("$translationsCount 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; diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 9e10221f..992ed809 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -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)]); diff --git a/Resources/config/console.yaml b/Resources/config/console.yaml index ab005652..f9eeb2a9 100644 --- a/Resources/config/console.yaml +++ b/Resources/config/console.yaml @@ -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 } diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index b065dbac..23cec40c 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -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 diff --git a/Service/StorageService.php b/Service/StorageService.php index 03215800..29bff0e9 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -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; @@ -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; } /** @@ -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) { @@ -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; - } }