From 06154550877f664b7de632590fcd583e2a503fec Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 7 Oct 2012 15:47:32 +0200 Subject: [PATCH] Clean up syntax/docs & simplify some bits of code --- src/Composer/Command/ConfigCommand.php | 68 ++++++++++++-------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 114954606..e0cd2c1fd 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -22,6 +22,9 @@ use Composer\Factory; use Composer\Json\JsonFile; use Composer\Json\JsonValidationException; +/** + * @author Joshua Estes + */ class ConfigCommand extends Command { /** @@ -38,10 +41,10 @@ class ConfigCommand extends Command ->setName('config') ->setDescription('Set config options') ->setDefinition(array( - new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.'), + new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'), new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'), new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'), - new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json'), + new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'), new InputArgument('setting-key', null, 'Setting key'), new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'), )) @@ -49,8 +52,6 @@ class ConfigCommand extends Command This command allows you to edit some basic composer settings in either the local composer.json file or the global config.json file. -USAGE: - To edit the global config.json file: php composer.phar --global @@ -62,11 +63,13 @@ To add a repository: You can add a repository to the global config.json file by passing in the --global option. -If you want to launch your editor with the composer.json file you must have "EDITOR" set. +To edit the file in an external editor: php composer.phar --edit -To get a list of configuration values in the file, pass the --list option. +To choose your editor you can set the "EDITOR" env variable. + +To get a list of configuration values in the file: php composer.phar --list @@ -74,11 +77,6 @@ You can always pass more than one option. As an example, if you want to edit the global config.json file. php composer.phar --edit --global - -LIMITATIONS - -The command only supports repositories and process-timeout right now. - EOT ) ; @@ -89,17 +87,15 @@ EOT */ protected function initialize(InputInterface $input, OutputInterface $output) { - if ($input->getOption('global') && $input->getOption('file')) { - throw new \RuntimeException('Cannot use both, you must pick to edit either the global config or path to composer.json'); + if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) { + throw new \RuntimeException('--file and --global can not be combined'); } // Get the local composer.json, global config.json, or if the user // passed in a file to use $this->configFile = $input->getOption('global') ? (Factory::createConfig()->get('home') . '/config.json') - : (null !== $input->getOption('file') - ? $input->getOption('file') - : 'composer.json'); + : $input->getOption('file'); $this->configFile = new JsonFile($this->configFile); if (!$this->configFile->exists()) { @@ -128,6 +124,7 @@ EOT // List the configuration of the file settings if ($input->getOption('list')) { $this->displayFileContents($this->configFile->read(), $output); + return 0; } @@ -136,6 +133,7 @@ EOT if (null === $input->getArgument('setting-value')) { throw new \RuntimeException('You must include a setting value.'); } + /** * The user needs the ability to add a repository with one command. * For example "config -g repository.foo 'vcs http://example.com' @@ -156,11 +154,11 @@ EOT 'type' => $values[0], 'url' => $values[1], )); - + // Could there be a better way to do this? $settings = array_merge_recursive($configSettings, $setting); $this->validateSchema($settings); - } + } // process-timeout elseif (preg_match('/^process-timeout/', $input->getArgument('setting-key'))) { if (1 !== count($values)) { @@ -191,42 +189,37 @@ EOT return 1; } } - + $this->configFile->write($settings); } } - /** - * {@inheritDoc} - */ - protected function interact(InputInterface $input, OutputInterface $output) - { - } - /** * Display the contents of the file in a pretty formatted way * * @param array $contents * @param OutputInterface $output - * @param integer $depth * @param string|null $k */ - protected function displayFileContents(array $contents, OutputInterface $output, &$depth = 0, $k = null) + protected function displayFileContents(array $contents, OutputInterface $output, $k = null) { // @todo Look into a way to refactor this code, as it is right now, I // don't like it, also the name of the function could be better foreach ($contents as $key => $value) { if (is_array($value)) { - $depth++; $k .= $key . '.'; - $this->displayFileContents($value, $output, $depth, $k); + $this->displayFileContents($value, $output, $k); + if (substr_count($k,'.') > 1) { $k = str_split($k,strrpos($k,'.',-2)); $k = $k[0] . '.'; - } else { $k = null; } - $depth--; + } else { + $k = null; + } + continue; } + $output->writeln('[' . $k . $key . '] ' . $value . ''); } } @@ -243,12 +236,13 @@ EOT { $parts = array_reverse(explode('.', $key)); $tmp = array(); - for($i=0;$iproperties->name, $schemaData->properties->description @@ -287,5 +281,3 @@ EOT return true; } } - -