InstallCommand.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Autoload\AutoloadGenerator;
  13. use Composer\DependencyResolver;
  14. use Composer\DependencyResolver\Pool;
  15. use Composer\DependencyResolver\Request;
  16. use Composer\DependencyResolver\Operation;
  17. use Composer\Package\LinkConstraint\VersionConstraint;
  18. use Composer\Package\PackageInterface;
  19. use Composer\Repository\PlatformRepository;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Input\InputOption;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. use Composer\DependencyResolver\Operation\InstallOperation;
  24. use Composer\DependencyResolver\Solver;
  25. /**
  26. * @author Jordi Boggiano <j.boggiano@seld.be>
  27. * @author Ryan Weaver <ryan@knplabs.com>
  28. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  29. */
  30. class InstallCommand extends Command
  31. {
  32. protected function configure()
  33. {
  34. $this
  35. ->setName('install')
  36. ->setDescription('Parses the composer.json file and downloads the needed dependencies.')
  37. ->setDefinition(array(
  38. new InputOption('dev', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'),
  39. new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'),
  40. new InputOption('no-install-recommends', null, InputOption::VALUE_NONE, 'Do not install recommended packages (ignored when installing from an existing lock file).'),
  41. new InputOption('install-suggests', null, InputOption::VALUE_NONE, 'Also install suggested packages (ignored when installing from an existing lock file).'),
  42. ))
  43. ->setHelp(<<<EOT
  44. The <info>install</info> command reads the composer.json file from the
  45. current directory, processes it, and downloads and installs all the
  46. libraries and dependencies outlined in that file.
  47. <info>php composer.phar install</info>
  48. EOT
  49. )
  50. ;
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. return $this->install($input, $output);
  55. }
  56. public function install(InputInterface $input, OutputInterface $output, $update = false)
  57. {
  58. $preferSource = (Boolean) $input->getOption('dev');
  59. $dryRun = (Boolean) $input->getOption('dry-run');
  60. $verbose = $dryRun || $input->getOption('verbose');
  61. $composer = $this->getComposer();
  62. if ($preferSource) {
  63. $composer->getDownloadManager()->setPreferSource(true);
  64. }
  65. // create local repo, this contains all packages that are installed in the local project
  66. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  67. // create installed repo, this contains all local packages + platform packages (php & extensions)
  68. $installedRepo = new PlatformRepository($localRepo);
  69. // creating repository pool
  70. $pool = new Pool;
  71. $pool->addRepository($installedRepo);
  72. foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
  73. $pool->addRepository($repository);
  74. }
  75. // creating requirements request
  76. $request = new Request($pool);
  77. if ($update) {
  78. $output->writeln('<info>Updating dependencies</info>');
  79. $installedPackages = $installedRepo->getPackages();
  80. $links = $this->collectLinks($input, $composer->getPackage());
  81. foreach ($links as $link) {
  82. foreach ($installedPackages as $package) {
  83. if ($package->getName() === $link->getTarget()) {
  84. $request->update($package->getName(), new VersionConstraint('=', $package->getVersion()));
  85. break;
  86. }
  87. }
  88. $request->install($link->getTarget(), $link->getConstraint());
  89. }
  90. } elseif ($composer->getLocker()->isLocked()) {
  91. $output->writeln('<info>Installing from lock file</info>');
  92. if (!$composer->getLocker()->isFresh()) {
  93. $output->writeln('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
  94. }
  95. foreach ($composer->getLocker()->getLockedPackages() as $package) {
  96. $constraint = new VersionConstraint('=', $package->getVersion());
  97. $request->install($package->getName(), $constraint);
  98. }
  99. } else {
  100. $output->writeln('<info>Installing dependencies</info>');
  101. $links = $this->collectLinks($input, $composer->getPackage());
  102. foreach ($links as $link) {
  103. $request->install($link->getTarget(), $link->getConstraint());
  104. }
  105. }
  106. // prepare solver
  107. $installationManager = $composer->getInstallationManager();
  108. $policy = new DependencyResolver\DefaultPolicy();
  109. $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo);
  110. // solve dependencies
  111. $operations = $solver->solve($request);
  112. // check for missing deps
  113. // TODO this belongs in the solver, but this will do for now to report top-level deps missing at least
  114. foreach ($request->getJobs() as $job) {
  115. if ('install' === $job['cmd']) {
  116. foreach ($installedRepo->getPackages() as $package ) {
  117. if ($installedRepo->hasPackage($package) && !$package->isPlatform() && !$installationManager->isPackageInstalled($package)) {
  118. $operations[$job['packageName']] = new InstallOperation($package, Solver::RULE_PACKAGE_NOT_EXIST);
  119. }
  120. if (in_array($job['packageName'], $package->getNames())) {
  121. continue 2;
  122. }
  123. }
  124. foreach ($operations as $operation) {
  125. if ('install' === $operation->getJobType() && in_array($job['packageName'], $operation->getPackage()->getNames())) {
  126. continue 2;
  127. }
  128. if ('update' === $operation->getJobType() && in_array($job['packageName'], $operation->getTargetPackage()->getNames())) {
  129. continue 2;
  130. }
  131. }
  132. if ($pool->whatProvides($job['packageName'])) {
  133. throw new \UnexpectedValueException('Package '.$job['packageName'].' can not be installed, either because its version constraint is incorrect, or because one of its dependencies was not found.');
  134. }
  135. throw new \UnexpectedValueException('Package '.$job['packageName'].' was not found in the package pool, check the name for typos.');
  136. }
  137. }
  138. // execute operations
  139. if (!$operations) {
  140. $output->writeln('<info>Nothing to install/update</info>');
  141. }
  142. foreach ($operations as $operation) {
  143. if ($verbose) {
  144. $output->writeln((string) $operation);
  145. }
  146. if (!$dryRun) {
  147. $installationManager->execute($operation);
  148. }
  149. }
  150. if (!$dryRun) {
  151. if ($update || !$composer->getLocker()->isLocked()) {
  152. $composer->getLocker()->lockPackages($localRepo->getPackages());
  153. $output->writeln('<info>Writing lock file</info>');
  154. }
  155. $localRepo->write();
  156. $output->writeln('<info>Generating autoload files</info>');
  157. $generator = new AutoloadGenerator;
  158. $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer');
  159. }
  160. }
  161. private function collectLinks(InputInterface $input, PackageInterface $package)
  162. {
  163. $links = $package->getRequires();
  164. if (!$input->getOption('no-install-recommends')) {
  165. $links = array_merge($links, $package->getRecommends());
  166. }
  167. if ($input->getOption('install-suggests')) {
  168. $links = array_merge($links, $package->getSuggests());
  169. }
  170. return $links;
  171. }
  172. }