InstallCommand.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\Script\ScriptEvents;
  13. use Composer\Script\EventDispatcher;
  14. use Composer\Autoload\AutoloadGenerator;
  15. use Composer\DependencyResolver;
  16. use Composer\DependencyResolver\Pool;
  17. use Composer\DependencyResolver\Request;
  18. use Composer\DependencyResolver\Operation;
  19. use Composer\Package\LinkConstraint\VersionConstraint;
  20. use Composer\Package\PackageInterface;
  21. use Composer\Repository\PlatformRepository;
  22. use Symfony\Component\Console\Input\InputInterface;
  23. use Symfony\Component\Console\Input\InputOption;
  24. use Symfony\Component\Console\Output\OutputInterface;
  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. $io = $this->getApplication()->getIO();
  63. $dispatcher = new EventDispatcher($this->getComposer(), $io);
  64. if ($preferSource) {
  65. $composer->getDownloadManager()->setPreferSource(true);
  66. }
  67. // create local repo, this contains all packages that are installed in the local project
  68. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  69. // create installed repo, this contains all local packages + platform packages (php & extensions)
  70. $installedRepo = new PlatformRepository($localRepo);
  71. // creating repository pool
  72. $pool = new Pool;
  73. $pool->addRepository($installedRepo);
  74. foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
  75. $pool->addRepository($repository);
  76. }
  77. // dispatch pre event
  78. if (!$dryRun) {
  79. $eventName = $update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD;
  80. $dispatcher->dispatchCommandEvent($eventName);
  81. }
  82. // creating requirements request
  83. $request = new Request($pool);
  84. if ($update) {
  85. $output->writeln('<info>Updating dependencies</info>');
  86. $installedPackages = $installedRepo->getPackages();
  87. $links = $this->collectLinks($input, $composer->getPackage());
  88. foreach ($links as $link) {
  89. foreach ($installedPackages as $package) {
  90. if ($package->getName() === $link->getTarget()) {
  91. $request->update($package->getName(), new VersionConstraint('=', $package->getVersion()));
  92. break;
  93. }
  94. }
  95. $request->install($link->getTarget(), $link->getConstraint());
  96. }
  97. } elseif ($composer->getLocker()->isLocked()) {
  98. $output->writeln('<info>Installing from lock file</info>');
  99. if (!$composer->getLocker()->isFresh()) {
  100. $output->writeln('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
  101. }
  102. foreach ($composer->getLocker()->getLockedPackages() as $package) {
  103. $constraint = new VersionConstraint('=', $package->getVersion());
  104. $request->install($package->getName(), $constraint);
  105. }
  106. } else {
  107. $output->writeln('<info>Installing dependencies</info>');
  108. $links = $this->collectLinks($input, $composer->getPackage());
  109. foreach ($links as $link) {
  110. $request->install($link->getTarget(), $link->getConstraint());
  111. }
  112. }
  113. // prepare solver
  114. $installationManager = $composer->getInstallationManager();
  115. $policy = new DependencyResolver\DefaultPolicy();
  116. $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo);
  117. // solve dependencies
  118. $operations = $solver->solve($request);
  119. // check for missing deps
  120. // TODO this belongs in the solver, but this will do for now to report top-level deps missing at least
  121. foreach ($request->getJobs() as $job) {
  122. if ('install' === $job['cmd']) {
  123. foreach ($installedRepo->getPackages() as $package) {
  124. if (in_array($job['packageName'], $package->getNames())) {
  125. continue 2;
  126. }
  127. }
  128. foreach ($operations as $operation) {
  129. if ('install' === $operation->getJobType() && in_array($job['packageName'], $operation->getPackage()->getNames())) {
  130. continue 2;
  131. }
  132. if ('update' === $operation->getJobType() && in_array($job['packageName'], $operation->getTargetPackage()->getNames())) {
  133. continue 2;
  134. }
  135. }
  136. if ($pool->whatProvides($job['packageName'])) {
  137. 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.');
  138. }
  139. throw new \UnexpectedValueException('Package '.$job['packageName'].' was not found in the package pool, check the name for typos.');
  140. }
  141. }
  142. // execute operations
  143. if (!$operations) {
  144. $output->writeln('<info>Nothing to install/update</info>');
  145. }
  146. foreach ($operations as $operation) {
  147. if ($verbose) {
  148. $output->writeln((string) $operation);
  149. }
  150. if (!$dryRun) {
  151. $dispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  152. $installationManager->execute($operation);
  153. $dispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  154. }
  155. }
  156. if (!$dryRun) {
  157. if ($update || !$composer->getLocker()->isLocked()) {
  158. $composer->getLocker()->lockPackages($localRepo->getPackages());
  159. $output->writeln('<info>Writing lock file</info>');
  160. }
  161. $localRepo->write();
  162. $output->writeln('<info>Generating autoload files</info>');
  163. $generator = new AutoloadGenerator;
  164. $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer');
  165. // dispatch post event
  166. $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
  167. $dispatcher->dispatchCommandEvent($eventName);
  168. }
  169. }
  170. private function collectLinks(InputInterface $input, PackageInterface $package)
  171. {
  172. $links = $package->getRequires();
  173. if (!$input->getOption('no-install-recommends')) {
  174. $links = array_merge($links, $package->getRecommends());
  175. }
  176. if ($input->getOption('install-suggests')) {
  177. $links = array_merge($links, $package->getSuggests());
  178. }
  179. return $links;
  180. }
  181. }