diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index f8f2f1292..1ccfa8b86 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -22,11 +22,6 @@ use Composer\Json\JsonFile; class ConfigCommand extends Command { - /** - * @var array - */ - protected $repositories = array(); - /** * @var Composer\Json\JsonFile */ @@ -44,8 +39,10 @@ class ConfigCommand extends Command new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.'), new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'), new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'), - // @todo insert argument here + new InputArgument('setting-key', null, 'Setting key'), + new InputArgument('setting-value', null, 'Setting value'), )) + // @todo Document ->setHelp(<<getOption('editor')) { // @todo Find a way to use another editor - $editor = 'vim'; + $editor = system("bash -cl 'echo \$EDITOR'"); system($editor . ' ' . $this->configFile->getPath() . ' > `tty`'); return 0; } @@ -92,6 +89,28 @@ EOT $this->displayFileContents($this->configFile->read(), $output); return 0; } + + // If the user enters in a config variable, parse it and save to file + if ($input->getArgument('setting-key')) { + if (null === $input->getArgument('setting-value')) { + throw new \RuntimeException('You must include a setting value.'); + } + $setting = $this->parseSetting($input->getArgument('setting-key'), $input->getArgument('setting-value')); + $configSettings = $this->configFile->read(); + $settings = array_merge($configSettings, $setting); + + // Make confirmation + if ($input->isInteractive()) { + $dialog = $this->getHelperSet()->get('dialog'); + $output->writeln(JsonFile::encode($settings)); + if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm?', 'yes', '?'), true)) { + $output->writeln('Command Aborted by User'); + return 1; + } + } + + $this->configFile->write($settings); + } } /** @@ -112,7 +131,7 @@ EOT protected function displayFileContents(array $contents, OutputInterface $output, &$depth = 0, $k = null) { // @todo Look into a way to refactor this code, as it is right now, I - // don't like it + // don't like it, also the name of the function could be better foreach ($contents as $key => $value) { if (is_array($value)) { $depth++; @@ -128,6 +147,27 @@ EOT $output->writeln('[' . $k . $key . '] ' . $value . ''); } } + + /** + * This function will take a setting key (a.b.c) and return an + * array that matches this + * + * @param string $key + * @param string $value + * @return array + */ + protected function parseSetting($key, $value) + { + $parts = array_reverse(explode('.', $key)); + $tmp = array(); + for($i=0;$i