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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
24 changes: 22 additions & 2 deletions Command/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 | 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', [])
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -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;
}
}
22 changes: 11 additions & 11 deletions Service/StorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Expand Down