Skip to content

Commit 33e7084

Browse files
authored
Add support for arbitrary options to StorageInterface::export()/import() (#378)
* Add support for arbitrary options to StorageInterface::export()/import() * Added the same support for DownloadCommand
1 parent 6a0e645 commit 33e7084

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

Command/DownloadCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected function configure(): void
6363
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
6464
->addOption('cache', null, InputOption::VALUE_NONE, '[DEPRECATED] Cache is now automatically cleared when translations have changed.')
6565
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
66+
->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', [])
6667
;
6768
}
6869

@@ -86,7 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8687
$translationsDirectory = $config->getOutputDir();
8788
$md5BeforeDownload = $this->hashDirectory($translationsDirectory);
8889

89-
$catalogues = $storage->download();
90+
$exportOptions = $this->cleanParameters($input->getOption('export-config'));
91+
$catalogues = $storage->download($exportOptions);
9092
$this->catalogueWriter->writeCatalogues($config, $catalogues);
9193

9294
$translationsCount = 0;
@@ -129,4 +131,17 @@ private function hashDirectory(string $directory)
129131

130132
return \hash_final($hash);
131133
}
134+
135+
public function cleanParameters(array $raw)
136+
{
137+
$config = [];
138+
139+
foreach ($raw as $string) {
140+
// Assert $string looks like "foo:bar"
141+
list($key, $value) = \explode(':', $string, 2);
142+
$config[$key][] = $value;
143+
}
144+
145+
return $config;
146+
}
132147
}

Command/SyncCommand.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use Translation\Bundle\Service\StorageManager;
1920
use Translation\Bundle\Service\StorageService;
@@ -40,7 +41,10 @@ protected function configure(): void
4041
->setName(self::$defaultName)
4142
->setDescription('Sync the translations with the remote storage')
4243
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
43-
->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down');
44+
->addArgument('direction', InputArgument::OPTIONAL, 'Use "down" if local changes should be overwritten, otherwise "up"', 'down')
45+
->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', [])
46+
->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', [])
47+
;
4448
}
4549

4650
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -60,8 +64,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6064
return 0;
6165
}
6266

63-
$this->getStorage($input->getArgument('configuration'))->sync($direction);
67+
$export = $this->cleanParameters($input->getOption('export-config'));
68+
$import = $this->cleanParameters($input->getOption('import-config'));
69+
70+
$this->getStorage($input->getArgument('configuration'))->sync($direction, $import, $export);
6471

6572
return 0;
6673
}
74+
75+
public function cleanParameters(array $raw)
76+
{
77+
$config = [];
78+
79+
foreach ($raw as $string) {
80+
// Assert $string looks like "foo:bar"
81+
list($key, $value) = \explode(':', $string, 2);
82+
$config[$key][] = $value;
83+
}
84+
85+
return $config;
86+
}
6787
}

Service/StorageService.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public function __construct(CatalogueFetcher $catalogueFetcher, Configuration $c
5555
*
5656
* @return MessageCatalogue[]
5757
*/
58-
public function download(): array
58+
public function download(array $exportOptions = []): array
5959
{
6060
$catalogues = [];
6161
foreach ($this->config->getLocales() as $locale) {
6262
$catalogues[$locale] = new MessageCatalogue($locale);
6363
foreach ($this->remoteStorages as $storage) {
6464
if ($storage instanceof TransferableStorage) {
65-
$storage->export($catalogues[$locale]);
65+
$storage->export($catalogues[$locale], $exportOptions);
6666
}
6767
}
6868
}
@@ -73,17 +73,17 @@ public function download(): array
7373
/**
7474
* Synchronize translations with remote.
7575
*/
76-
public function sync(string $direction = self::DIRECTION_DOWN): void
76+
public function sync(string $direction = self::DIRECTION_DOWN, array $importOptions = [], array $exportOptions = []): void
7777
{
7878
switch ($direction) {
7979
case self::DIRECTION_DOWN:
80-
$this->mergeDown();
81-
$this->mergeUp();
80+
$this->mergeDown($exportOptions);
81+
$this->mergeUp($importOptions);
8282

8383
break;
8484
case self::DIRECTION_UP:
85-
$this->mergeUp();
86-
$this->mergeDown();
85+
$this->mergeUp($importOptions);
86+
$this->mergeDown($exportOptions);
8787

8888
break;
8989
default:
@@ -95,9 +95,9 @@ public function sync(string $direction = self::DIRECTION_DOWN): void
9595
* Download and merge all translations from remote storages down to your local storages.
9696
* Only the local storages will be changed.
9797
*/
98-
public function mergeDown(): void
98+
public function mergeDown(array $exportOptions = []): void
9999
{
100-
$catalogues = $this->download();
100+
$catalogues = $this->download($exportOptions);
101101

102102
foreach ($catalogues as $locale => $catalogue) {
103103
foreach ($catalogue->all() as $domain => $messages) {
@@ -115,13 +115,13 @@ public function mergeDown(): void
115115
*
116116
* This will overwrite your remote copy.
117117
*/
118-
public function mergeUp(): void
118+
public function mergeUp(array $importOptions = []): void
119119
{
120120
$catalogues = $this->catalogueFetcher->getCatalogues($this->config);
121121
foreach ($catalogues as $catalogue) {
122122
foreach ($this->remoteStorages as $storage) {
123123
if ($storage instanceof TransferableStorage) {
124-
$storage->import($catalogue);
124+
$storage->import($catalogue, $importOptions);
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)