* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Console;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Database\DatabaseDriver;
use Joomla\Registry\Registry;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Console command Setting Configuration options
*
* @since 4.0.0
*/
class SetConfigurationCommand extends AbstractCommand
{
/**
* The default command name
*
* @var string
* @since 4.0.0
*/
protected static $defaultName = 'config:set';
/**
* Stores the Input Object
* @var Input
* @since 4.0.0
*/
private $cliInput;
/**
* SymfonyStyle Object
* @var SymfonyStyle
* @since 4.0.0
*/
private $ioStyle;
/**
* Options Array
* @var array
* @since 4.0.0
*/
private $options;
/**
* Return code if configuration is set successfully
* @since 4.0.0
*/
public const CONFIG_SET_SUCCESSFUL = 0;
/**
* Return code if configuration set failed
* @since 4.0.0
*/
public const CONFIG_SET_FAILED = 1;
/**
* Return code if config validation failed
* @since 4.0.0
*/
public const CONFIG_VALIDATION_FAILED = 2;
/**
* Return code if options are wrong
* @since 4.0.0
*/
public const CONFIG_OPTIONS_WRONG = 3;
/**
* Return code if database validation failed
* @since 4.0.0
*/
public const DB_VALIDATION_FAILED = 4;
/**
* Configures the IO
*
* @param InputInterface $input Console Input
* @param OutputInterface $output Console Output
*
* @return void
*
* @since 4.0.0
*
*/
private function configureIO(InputInterface $input, OutputInterface $output)
{
$language = Factory::getLanguage();
$language->load('', JPATH_INSTALLATION, null, false, false) ||
$language->load('', JPATH_INSTALLATION, null, true);
$language->load('com_config', JPATH_ADMINISTRATOR, null, false, false) ||
$language->load('com_config', JPATH_ADMINISTRATOR, null, true);
$this->cliInput = $input;
$this->ioStyle = new SymfonyStyle($input, $output);
}
/**
* Collects options from user input
*
* @param array $options Options input by users
*
* @return boolean
*
* @since 4.0.0
*/
private function retrieveOptionsFromInput(array $options): bool
{
$collected = [];
foreach ($options as $option) {
if (strpos($option, '=') === false) {
$this->ioStyle->error('Options and values should be separated by "="');
return false;
}
list($option, $value) = explode('=', $option);
$collected[$option] = $value;
}
$this->options = $collected;
return true;
}
/**
* Validates the options provided
*
* @return boolean
*
* @since 4.0.0
*/
private function validateOptions(): bool
{
$config = $this->getInitialConfigurationOptions();
$configs = $config->toArray();
$valid = true;
array_walk(
$this->options,
function ($value, $key) use ($configs, &$valid) {
if (!array_key_exists($key, $configs)) {
$this->ioStyle->error("Can't find option *$key* in configuration list");
$valid = false;
}
}
);
return $valid;
}
/**
* Sets the options array
*
* @param string $options Options string
*
* @since 4.0.0
*
* @return void
*/
public function setOptions($options)
{
$this->options = explode(' ', $options);
}
/**
* Collects the options array
*
* @return array|mixed
*
* @since 4.0.0
*/
public function getOptions()
{
return $this->cliInput->getArgument('options');
}
/**
* Returns Default configuration Object
*
* @return Registry
*
* @since 4.0.0
*/
public function getInitialConfigurationOptions(): Registry
{
return (new Registry(new \JConfig()));
}
/**
* Save the configuration file
*
* @param array $options Options array
*
* @return boolean
*
* @since 4.0.0
*/
public function saveConfiguration($options): bool
{
$app = $this->getApplication();
// Check db connection encryption properties
$model = $app->bootComponent('com_config')->getMVCFactory($app)->createModel('Application', 'Administrator');
if (!$model->save($options)) {
$this->ioStyle->error(Text::_('Failed to save properties'));
return false;
}
return true;
}
/**
* Initialise the command.
*
* @return void
*
* @since 4.0.0
*/
protected function configure(): void
{
$this->addArgument(
'options',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'All the options you want to set'
);
$help = "%command.name% sets the value for a configuration option
\nUsage: php %command.full_name%