InstallCommand.php 8.0 KB

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