setName('mautic:theme:json-config')
->setDefinition([
new InputOption(
'theme', null, InputOption::VALUE_REQUIRED,
'The name of the theme whose config you are converting.'
),
new InputOption(
'save-php-config', null, InputOption::VALUE_NONE,
'When used, the theme\'s PHP config file will be saved.'
),
])
->setHelp(<<<'EOT'
The %command.name% command converts a PHP theme config file to JSON.
php %command.full_name%
You must specify the name of the theme via the --theme parameter:
php %command.full_name% --theme=
You may opt to save the PHP config file by using the --save-php-config option.
php %command.full_name% --save-php-config
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$options = $input->getOptions();
$theme = $options['theme'];
$savePhpConfig = $options['save-php-config'];
$themePath = realpath($this->pathsHelper->getSystemPath('themes', true).'/'.$theme);
if (empty($themePath)) {
$output->writeln("\n\nThe specified theme ($theme) does not exist.");
return Command::FAILURE;
}
$jsonConfigPath = $themePath.'/config.json';
if (file_exists($jsonConfigPath)) {
$output->writeln("\n\nThe specified theme ($theme) already has a JSON config file.");
return Command::FAILURE;
}
$configPath = $themePath.'/config.php';
if (!file_exists($configPath)) {
$output->writeln("\n\nThe php config file for the specified theme ($theme) could not be found.");
return Command::FAILURE;
}
$config = include $configPath;
if (!is_array($config) || !array_key_exists('name', $config)) {
$output->writeln("\n\nThe php config file for the specified theme ($theme) is not a valid config file.");
return Command::FAILURE;
}
$jsonConfig = json_encode($config, JSON_PRETTY_PRINT);
if (!file_put_contents($jsonConfigPath, $jsonConfig)) {
$output->writeln("\n\nError writing json config file for the specified theme ($theme).");
return Command::FAILURE;
} else {
$output->writeln("\n\nSuccessfully wrote json config file for the specified theme ($theme).");
}
if (!$savePhpConfig) {
if (!unlink($configPath)) {
$output->writeln("\n\nError deleting php config file for the specified theme ($theme).");
} else {
$output->writeln("\n\nPHP config file for theme ($theme) has been deleted.");
}
} else {
$output->writeln("\n\nPHP config file for theme ($theme) was preserved.");
}
return Command::SUCCESS;
}
protected static $defaultDescription = 'Converts theme config to JSON from PHP';
}