setName(self::NAME)
->addOption('bundle', null, InputOption::VALUE_OPTIONAL, 'Optional bundle to pull. Example value: WebhookBundle', null)
->setHelp(<<<'EOT'
The %command.name% command is used to push translation resources to Transifex
php %command.full_name%
You can optionally choose to update resources for one bundle only with the --bundle option:
php %command.full_name% --bundle AssetBundle
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$bundleFilter = $input->getOption('bundle');
$files = $this->languageHelper->getLanguageFiles($bundleFilter ? [$bundleFilter] : []);
try {
$transifex = $this->transifexFactory->getTransifex();
} catch (InvalidConfigurationException) {
$output->writeln($this->translator->trans(
'mautic.core.command.transifex_no_credentials')
);
return Command::FAILURE;
}
$resources = $transifex->getConnector(Resources::class);
\assert($resources instanceof Resources);
$existingResources = json_decode((string) $resources->getAll()->getBody(), true);
$promises = new \SplQueue();
foreach ($files as $bundle => $stringFiles) {
foreach ($stringFiles as $file) {
$name = $bundle.' '.str_replace('.ini', '', basename($file));
$alias = UrlHelper::stringURLUnicodeSlug($name);
$content = file_get_contents($file);
$output->writeln(
$this->translator->trans(
'mautic.core.command.transifex_processing_resource',
['%resource%' => $name]
)
);
try {
if (false === $content) {
throw new \RuntimeException('Unable to read file '.$file);
}
if (!$resources->resourceExists($existingResources['data'], $alias)) {
$resources->create($name, $alias, 'INI');
$output->writeln(
$this->translator->trans('mautic.core.command.transifex_resource_created')
);
}
$promise = $transifex->getApiConnector()->createPromise(
$resources->uploadContent($alias, $content, true)
);
$promise->setFilePath($file);
$promises->enqueue($promise);
} catch (TransifexException $exception) {
$output->writeln(
$this->translator->trans(
'mautic.core.command.transifex_error_pushing_data',
['%message%' => $exception->getMessage()]
)
);
}
}
}
$transifex->getApiConnector()->fulfillPromises(
$promises,
function (ResponseInterface $response, Promise $promise) use ($output): void {
$output->writeln(
$this->translator->trans(
'mautic.core.command.transifex_resource_updated',
['%file%' => $promise->getFilePath()]
)
);
},
function (ResponseException $exception, Promise $promise) use ($output): void {
$output->writeln($promise->getFilePath());
$output->writeln($exception->getMessage());
}
);
return Command::SUCCESS;
}
protected static $defaultDescription = 'Pushes Mautic translation resources to Transifex';
}