Pārlūkot izejas kodu

cleaned up and added more comments

Joshua Estes 12 gadi atpakaļ
vecāks
revīzija
457d0aac94
1 mainītis faili ar 55 papildinājumiem un 27 dzēšanām
  1. 55 27
      src/Composer/Command/ConfigRepositoriesAddCommand.php

+ 55 - 27
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(<<<EOT
+By running this command you may add a repository of a given type to either your
+local composer.json file or to the global composer config file.
 
 EOT
             )
         ;
     }
 
-    protected function initialize(InputInterface $input, OutputInterface $output)
-    {
-        $config = Factory::createConfig();
-
-        $globalConfig = new JsonFile(Factory::createConfig()->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('<error>Command aborted by the user.</error>');
+                    return 1;
+                }
+            }
             $configFile->write($config);
+        } else {
+            $output->writeln('<info>No repositories have been added.</info>');
         }
     }
 
+    /**
+     * {@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);
     }
 }