Browse files

Allow the config command to read out values if no new value is provided

  • Loading branch information...
1 parent 24963fb commit 0d506228d6a2d278a85da275a1f8713a992bfa2d @Seldaek Seldaek committed Nov 13, 2012
Showing with 46 additions and 4 deletions.
  1. +46 −4 src/Composer/Command/ConfigCommand.php
View
50 src/Composer/Command/ConfigCommand.php
@@ -151,22 +151,65 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}
- if (!$input->getArgument('setting-key')) {
+ $settingKey = $input->getArgument('setting-key');
+ if (!$settingKey) {
return 0;
}
// If the user enters in a config variable, parse it and save to file
if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
throw new \RuntimeException('You can not combine a setting value with --unset');
}
+
+ // list value
if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
- throw new \RuntimeException('You must include a setting value or pass --unset to clear the value');
+ $data = $this->configFile->read();
+ if (preg_match('/^repos?(?:itories)?(?:\.(.+))?/', $settingKey, $matches)) {
+ if (empty($matches[1])) {
+ $value = isset($data['repositories']) ? $data['repositories'] : array();
+ } else {
+ if (!isset($data['repositories'][$matches[1]])) {
+ throw new \InvalidArgumentException('There is no '.$matches[1].' repository defined');
+ }
+
+ $value = $data['repositories'][$matches[1]];
+ }
+ } elseif (strpos($settingKey, '.')) {
+ $bits = explode('.', $settingKey);
+ $data = $data['config'];
+ foreach ($bits as $bit) {
+ if (isset($data[$bit])) {
+ $data = $data[$bit];
+ } elseif (isset($data[implode('.', $bits)])) {
+ // last bit can contain domain names and such so try to join whatever is left if it exists
+ $data = $data[implode('.', $bits)];
+ break;
+ } else {
+ throw new \RuntimeException($settingKey.' is not defined');
+ }
+ array_shift($bits);
+ }
+
+ $value = $data;
+ } elseif (isset($data['config'][$settingKey])) {
+ $value = $data['config'][$settingKey];
+ } else {
+ throw new \RuntimeException($settingKey.' is not defined');
+ }
+
+ if (is_array($value)) {
+ $value = json_encode($value);
+ }
+
+ $output->writeln($value);
+
+ return 0;
}
$values = $input->getArgument('setting-value'); // what the user is trying to add/change
// handle repositories
- if (preg_match('/^repos?(?:itories)?\.(.+)/', $input->getArgument('setting-key'), $matches)) {
+ if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
return $this->configSource->removeRepository($matches[1]);
}
@@ -214,7 +257,6 @@ function ($vals) {
),
);
- $settingKey = $input->getArgument('setting-key');
foreach ($uniqueConfigValues as $name => $callbacks) {
if ($settingKey === $name) {
if ($input->getOption('unset')) {

0 comments on commit 0d50622

Please sign in to comment.