diff --git a/src/Composer/Command/ConfigRepositoriesAddCommand.php b/src/Composer/Command/ConfigRepositoriesAddCommand.php index 26cf198ee..eae0abd24 100644 --- a/src/Composer/Command/ConfigRepositoriesAddCommand.php +++ b/src/Composer/Command/ConfigRepositoriesAddCommand.php @@ -19,47 +19,56 @@ use Symfony\Component\Console\Output\OutputInterface; use Composer\Factory; use Composer\Installer; use Composer\Json\JsonFile; -use Composer\Json\JsonManipulator; class ConfigRepositoriesAddCommand extends Command { + /** + * @var array + */ protected $repositories = array(); + /** + * {@inheritDoc} + */ protected function configure() { + // @todo Make it so the user can pass into this command an array $this ->setName('config:repositories:add') ->setDescription('Add a repository') ->setDefinition(array( - new InputOption('global', null, InputOption::VALUE_NONE, 'Set this as a global config settings.') + new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.') )) ->setHelp(<<get('home') . '/config.json'); - if (!$globalConfig->exists()) { - touch($globalConfig->getPath()); - $globalConfig->write(array()); - } - } - + /** + * {@inheritDoc} + */ protected function execute(InputInterface $input, OutputInterface $output) { + // Get the local composer.json or the global config.json $configFile = $input->getOption('global') ? (Factory::createConfig()->get('home') . '/config.json') : 'composer.json'; $configFile = new JsonFile($configFile); - + if (!$configFile->exists()) { + touch($globalConfig->getPath()); + // If you read an empty file, Composer throws an error + $globalConfig->write(array()); + } + + // Make sure we have something to add if (count($this->repositories)) { + // @todo Check and make sure the type/url combo does not + // alredy exist. $config = $configFile->read(); foreach ($this->repositories as $repo) { $config['repositories'][] = array( @@ -67,35 +76,54 @@ EOT 'url' => $repo['url'], ); } + + if ($input->isInteractive()) { + $output->writeln(array( + '', + JsonFile::encode($config), + '', + )); + $dialog = $this->getHelperSet()->get('dialog'); + if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you want to continuw and save the repositories', 'yes', '?'), true)) { + $output->writeln('Command aborted by the user.'); + return 1; + } + } $configFile->write($config); + } else { + $output->writeln('No repositories have been added.'); } } + /** + * {@inheritDoc} + */ protected function interact(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); - /** - * @todo Update this with more info - */ $output->writeln(array( '', - 'Add a repository', + 'With this command you can add as many repositories to either the', + 'local composer.json file or the global composer config file.', + '', + 'Type can be any of the following: composer, vcs, pear, package', + '', + 'For more information see docs: http://getcomposer.org/doc/05-repositories.md', '', )); - /** - * @todo put this into a loop so user can add many repositories at - * the same time. - */ - $type = $dialog->ask($output, $dialog->getQuestion('Repository Type')); - $repo = $dialog->ask($output, $dialog->getQuestion('Repository URL')); - if (null !== $type && null !== $repo) { + do { + $type = $dialog->ask($output, $dialog->getQuestion('Repository Type')); + $repo = $dialog->ask($output, $dialog->getQuestion('Repository URL')); + if (null === $type && null === $repo) { + break; + } $this->repositories[] = array( 'type' => $type, - 'url' => $repo, + 'url' => $repo, ); - } + } while(true); } }