InstallCommand.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. $listedPackages = array();
  78. $installedPackages = $installedRepo->getPackages();
  79. $links = $this->collectLinks($input, $composer->getPackage());
  80. foreach ($links as $link) {
  81. $listedPackages[] = $link->getTarget();
  82. foreach ($installedPackages as $package) {
  83. if ($package->getName() === $link->getTarget()) {
  84. $constraint = new VersionConstraint('=', $package->getVersion());
  85. if ($link->getConstraint()->matches($constraint)) {
  86. continue 2;
  87. }
  88. // TODO this should just update to the exact version (once constraints are available on update, see #125)
  89. $request->remove($package->getName(), $constraint);
  90. break;
  91. }
  92. }
  93. $request->install($link->getTarget(), $link->getConstraint());
  94. }
  95. } elseif ($composer->getLocker()->isLocked()) {
  96. $output->writeln('<info>Installing from lockfile.</info> (Run "composer update" to add or update packages)');
  97. foreach ($composer->getLocker()->getLockedPackages() as $package) {
  98. $constraint = new VersionConstraint('=', $package->getVersion());
  99. $request->install($package->getName(), $constraint);
  100. }
  101. } else {
  102. $output->writeln('<info>Installing dependencies.</info>');
  103. $links = $this->collectLinks($input, $composer->getPackage());
  104. foreach ($links as $link) {
  105. $request->install($link->getTarget(), $link->getConstraint());
  106. }
  107. }
  108. // prepare solver
  109. $installationManager = $composer->getInstallationManager();
  110. $policy = new DependencyResolver\DefaultPolicy();
  111. $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo);
  112. // solve dependencies
  113. $operations = $solver->solve($request);
  114. // check for missing deps
  115. // TODO this belongs in the solver, but this will do for now to report top-level deps missing at least
  116. foreach ($request->getJobs() as $job) {
  117. if ('install' === $job['cmd']) {
  118. foreach ($installedRepo->getPackages() as $package) {
  119. if (in_array($job['packageName'], $package->getNames())) {
  120. continue 2;
  121. }
  122. }
  123. foreach ($operations as $operation) {
  124. if ('install' === $operation->getJobType() && in_array($job['packageName'], $operation->getPackage()->getNames())) {
  125. continue 2;
  126. }
  127. if ('update' === $operation->getJobType() && in_array($job['packageName'], $operation->getTargetPackage()->getNames())) {
  128. continue 2;
  129. }
  130. }
  131. if ($pool->whatProvides($job['packageName'])) {
  132. throw new \UnexpectedValueException('Your version constraint for package '.$job['packageName'].' does not match any existing version, if it only has -dev versions make sure you include -dev in your version constraint.');
  133. }
  134. throw new \UnexpectedValueException('Package '.$job['packageName'].' was not found in the package pool, check the name for typos.');
  135. }
  136. }
  137. // execute operations
  138. foreach ($operations as $operation) {
  139. if ($verbose) {
  140. $output->writeln((string) $operation);
  141. }
  142. if (!$dryRun) {
  143. $installationManager->execute($operation);
  144. }
  145. }
  146. if (!$dryRun) {
  147. if ($update || !$composer->getLocker()->isLocked()) {
  148. $composer->getLocker()->lockPackages($localRepo->getPackages());
  149. $output->writeln('<info>Locked</info>');
  150. }
  151. $localRepo->write();
  152. $output->writeln('<info>Generating autoload files</info>');
  153. $generator = new AutoloadGenerator;
  154. $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer');
  155. }
  156. $output->writeln('<info>Done</info>');
  157. }
  158. private function collectLinks(InputInterface $input, PackageInterface $package)
  159. {
  160. $links = $package->getRequires();
  161. if (!$input->getOption('no-install-recommends')) {
  162. $links = array_merge($links, $package->getRecommends());
  163. }
  164. if ($input->getOption('install-suggests')) {
  165. $links = array_merge($links, $package->getSuggests());
  166. }
  167. return $links;
  168. }
  169. }