Browse Source

Add interactive option to install dependencies after running init command (#7521)

* Add interactive option to install dependencies after running init command

* Only ask to install dependencies when dependencies where defined
Shalvah 6 years ago
parent
commit
42e88ac27a
1 changed files with 25 additions and 0 deletions
  1. 25 0
      src/Composer/Command/InitCommand.php

+ 25 - 0
src/Composer/Command/InitCommand.php

@@ -22,6 +22,7 @@ use Composer\Repository\CompositeRepository;
 use Composer\Repository\PlatformRepository;
 use Composer\Repository\RepositoryFactory;
 use Composer\Util\ProcessExecutor;
+use Symfony\Component\Console\Input\ArrayInput;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -145,6 +146,11 @@ EOT
                 }
             }
         }
+
+        $question = 'Would you like to install dependencies now [<comment>yes</comment>]? ';
+        if ($input->isInteractive() && $this->hasDependencies($options) && $io->askConfirmation($question, true)) {
+            $this->installDependencies($output);
+        }
     }
 
     /**
@@ -767,4 +773,23 @@ EOT
 
         return array_keys(array_slice($similarPackages, 0, 5));
     }
+
+    private function installDependencies($output)
+    {
+        try {
+            $installCommand = $this->getApplication()->find('install');
+            $installCommand->run(new ArrayInput(array()), $output);
+        } catch (\Exception $e) {
+            $this->getIO()->writeError('Could not install dependencies. Run `composer install` to see more information.');
+        }
+
+    }
+
+    private function hasDependencies($options)
+    {
+        $requires = (array) $options['require'];
+        $devRequires = isset($options['require-dev']) ? (array) $options['require-dev'] : array();
+
+        return !empty($requires) || !empty($devRequires);
+    }
 }