From 2e9b9fe083907c9dfeec8f5610a9379298080219 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Mon, 6 Jan 2020 20:38:23 +0100 Subject: [PATCH 1/2] Add support for arbitrary options to StorageInterface::export()/import() --- Command/SyncCommand.php | 24 ++++++++++++++++++++++-- Service/StorageService.php | 22 +++++++++++----------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/Command/SyncCommand.php b/Command/SyncCommand.php index b0cbc931..2202e04d 100644 --- a/Command/SyncCommand.php +++ b/Command/SyncCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Translation\Bundle\Service\StorageManager; use Translation\Bundle\Service\StorageService; @@ -40,7 +41,10 @@ protected function configure(): void ->setName(self::$defaultName) ->setDescription('Sync the translations with the remote storage') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') - ->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down'); + ->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down') + ->addOption('export-config', 'exconf', InputOption::VALUE_IS_ARRAY, 'Options to send to the StorageInterface::export() function. Ie, when downloading. Example: --export-conf foo:bar', []) + ->addOption('import-config', 'imconf', InputOption::VALUE_IS_ARRAY, 'Options to send to the StorageInterface::import() function. Ie, when uploading. Example: --import-conf foo:bar', []) + ; } protected function execute(InputInterface $input, OutputInterface $output): int @@ -60,8 +64,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - $this->getStorage($input->getArgument('configuration'))->sync($direction); + $export = $this->cleanParameters($input->getOption('export-config')); + $import = $this->cleanParameters($input->getOption('import-config')); + + $this->getStorage($input->getArgument('configuration'))->sync($direction, $import, $export); return 0; } + + public function cleanParameters(array $raw) + { + $config = []; + + foreach ($raw as $string) { + // Assert $string looks like "foo:bar" + list($key, $value) = \explode(':', $string, 2); + $config[$key][] = $value; + } + + return $config; + } } diff --git a/Service/StorageService.php b/Service/StorageService.php index b4ecb171..2442ae3c 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -55,14 +55,14 @@ public function __construct(CatalogueFetcher $catalogueFetcher, Configuration $c * * @return MessageCatalogue[] */ - public function download(): array + public function download(array $exportOptions = []): 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]); + $storage->export($catalogues[$locale], $exportOptions); } } } @@ -73,17 +73,17 @@ public function download(): array /** * Synchronize translations with remote. */ - public function sync(string $direction = self::DIRECTION_DOWN): void + public function sync(string $direction = self::DIRECTION_DOWN, array $importOptions = [], array $exportOptions = []): void { switch ($direction) { case self::DIRECTION_DOWN: - $this->mergeDown(); - $this->mergeUp(); + $this->mergeDown($exportOptions); + $this->mergeUp($importOptions); break; case self::DIRECTION_UP: - $this->mergeUp(); - $this->mergeDown(); + $this->mergeUp($importOptions); + $this->mergeDown($exportOptions); break; default: @@ -95,9 +95,9 @@ public function sync(string $direction = self::DIRECTION_DOWN): void * Download and merge all translations from remote storages down to your local storages. * Only the local storages will be changed. */ - public function mergeDown(): void + public function mergeDown(array $exportOptions = []): void { - $catalogues = $this->download(); + $catalogues = $this->download($exportOptions); foreach ($catalogues as $locale => $catalogue) { foreach ($catalogue->all() as $domain => $messages) { @@ -115,13 +115,13 @@ public function mergeDown(): void * * This will overwrite your remote copy. */ - public function mergeUp(): void + public function mergeUp(array $importOptions = []): void { $catalogues = $this->catalogueFetcher->getCatalogues($this->config); foreach ($catalogues as $catalogue) { foreach ($this->remoteStorages as $storage) { if ($storage instanceof TransferableStorage) { - $storage->import($catalogue); + $storage->import($catalogue, $importOptions); } } } From 88a683c964493fdb1725de9d6ac8ae0139f8c16b Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 18 Jan 2020 10:55:52 +0100 Subject: [PATCH 2/2] Added the same support for DownloadCommand --- Command/DownloadCommand.php | 17 ++++++++++++++++- Command/SyncCommand.php | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Command/DownloadCommand.php b/Command/DownloadCommand.php index 990093c6..c4532c0b 100644 --- a/Command/DownloadCommand.php +++ b/Command/DownloadCommand.php @@ -63,6 +63,7 @@ protected function configure(): void ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->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.') + ->addOption('export-config', 'exconf', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Options to send to the StorageInterface::export() function. Ie, when downloading. Example: --export-config foo:bar', []) ; } @@ -86,7 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $translationsDirectory = $config->getOutputDir(); $md5BeforeDownload = $this->hashDirectory($translationsDirectory); - $catalogues = $storage->download(); + $exportOptions = $this->cleanParameters($input->getOption('export-config')); + $catalogues = $storage->download($exportOptions); $this->catalogueWriter->writeCatalogues($config, $catalogues); $translationsCount = 0; @@ -129,4 +131,17 @@ private function hashDirectory(string $directory) return \hash_final($hash); } + + public function cleanParameters(array $raw) + { + $config = []; + + foreach ($raw as $string) { + // Assert $string looks like "foo:bar" + list($key, $value) = \explode(':', $string, 2); + $config[$key][] = $value; + } + + return $config; + } } diff --git a/Command/SyncCommand.php b/Command/SyncCommand.php index 2202e04d..345affb3 100644 --- a/Command/SyncCommand.php +++ b/Command/SyncCommand.php @@ -42,8 +42,8 @@ protected function configure(): void ->setDescription('Sync the translations with the remote storage') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down') - ->addOption('export-config', 'exconf', InputOption::VALUE_IS_ARRAY, 'Options to send to the StorageInterface::export() function. Ie, when downloading. Example: --export-conf foo:bar', []) - ->addOption('import-config', 'imconf', InputOption::VALUE_IS_ARRAY, 'Options to send to the StorageInterface::import() function. Ie, when uploading. Example: --import-conf foo:bar', []) + ->addOption('export-config', 'exconf', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Options to send to the StorageInterface::export() function. Ie, when downloading. Example: --export-config foo:bar', []) + ->addOption('import-config', 'imconf', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Options to send to the StorageInterface::import() function. Ie, when uploading. Example: --import-config foo:bar', []) ; }