InstallCommand.php 10 KB

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