Skip to content
Permalink
Browse files

[config:override] Refactor 3772 allow nested configs (#4047)

* Option to allow nested configs

* Updated key

* Logig to deal with single or multi-value config

* Changed from plural to singular

* Enabled override multiple keys

* Fixed interactive mode
  • Loading branch information...
harold20 authored and enzolutions committed May 21, 2019
1 parent 304ff7e commit e64ab9bd93e24640b52209e09ce18188bcafae41
Showing with 67 additions and 26 deletions.
  1. +67 −26 src/Command/Config/OverrideCommand.php
@@ -9,6 +9,7 @@
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 Drupal\Console\Core\Command\Command;
use Drupal\Core\Config\CachedStorage;
@@ -51,15 +52,17 @@ protected function configure()
InputArgument::REQUIRED,
$this->trans('commands.config.override.arguments.name')
)
->addArgument(
->addOption(
'key',
InputArgument::REQUIRED,
$this->trans('commands.config.override.arguments.key')
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.override.options.key')
)
->addArgument(
->addOption(
'value',
InputArgument::REQUIRED,
$this->trans('commands.config.override.arguments.value')
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.config.override.options.value')
)
->setAliases(['co']);
}
@@ -89,23 +92,29 @@ protected function interact(InputInterface $input, OutputInterface $output)
);
$input->setArgument('name', $name);
}
$key = $input->getArgument('key');
$key = $input->getOption('key');
if (!$key) {
if ($this->configStorage->exists($name)) {
$configuration = $this->configStorage->read($name);
if (!$this->configStorage->exists($name)) {
$this->getIo()->newLine();
$this->getIo()->errorLite($this->trans('commands.config.override.messages.invalid-config-file'));
$this->getIo()->newLine();
return 0;
}
$key = $this->getIo()->choiceNoList(
$this->trans('commands.config.override.questions.key'),
array_keys($configuration)
);
$input->setArgument('key', $key);
$configuration = $this->configStorage->read($name);
$input->setOption('key', $this->getKeysFromConfig($configuration));
}
$value = $input->getArgument('value');
$value = $input->getOption('value');
if (!$value) {
$value = $this->getIo()->ask(
$this->trans('commands.config.override.questions.value')
);
$input->setArgument('value', $value);
foreach ($input->getOption('key') as $name) {
$value[] = $this->getIo()->ask(
sprintf(
$this->trans('commands.config.override.questions.value'),
$name
)
);
}
$input->setOption('value', $value);
}
}
/**
@@ -114,16 +123,24 @@ protected function interact(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output)
{
$configName = $input->getArgument('name');
$key = $input->getArgument('key');
$value = $input->getArgument('value');
$keys = $input->getOption('key');
$values = $input->getOption('value');
if(empty($keys)) {
return 1;
}
$config = $this->configFactory->getEditable($configName);
$configurationOverrideResult = $this->overrideConfiguration(
$config,
$key,
$value
);
$configurationOverrideResult = [];
foreach ($keys as $index => $key) {
$result = $this->overrideConfiguration(
$config,
$key,
$values[$index]
);
$configurationOverrideResult = array_merge($configurationOverrideResult, $result);
}
$config->save();
@@ -151,4 +168,28 @@ protected function overrideConfiguration($config, $key, $value)
return $result;
}
/**
* Allow to search a specific key to override.
*
* @param $configuration
* @param null $key
*
* @return array
*/
private function getKeysFromConfig($configuration, $key = null)
{
$choiceKey = $this->getIo()->choiceNoList(
$this->trans('commands.config.override.questions.key'),
array_keys($configuration)
);
$key = is_null($key) ? $choiceKey:$key.'.'.$choiceKey;
if(is_array($configuration[$choiceKey])){
return $this->getKeysFromConfig($configuration[$choiceKey], $key);
}
return [$key];
}
}

0 comments on commit e64ab9b

Please sign in to comment.
You can’t perform that action at this time.