InstallCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $installFromLock = false;
  103. $request = new Request($pool);
  104. if ($update) {
  105. $io->write('<info>Updating dependencies</info>');
  106. $installedPackages = $installedRepo->getPackages();
  107. $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
  108. foreach ($links as $link) {
  109. foreach ($installedPackages as $package) {
  110. if ($package->getName() === $link->getTarget()) {
  111. $request->update($package->getName(), new VersionConstraint('=', $package->getVersion()));
  112. break;
  113. }
  114. }
  115. $request->install($link->getTarget(), $link->getConstraint());
  116. }
  117. } elseif ($composer->getLocker()->isLocked()) {
  118. $installFromLock = true;
  119. $io->write('<info>Installing from lock file</info>');
  120. if (!$composer->getLocker()->isFresh()) {
  121. $io->write('<warning>Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies</warning>');
  122. }
  123. foreach ($composer->getLocker()->getLockedPackages() as $package) {
  124. $constraint = new VersionConstraint('=', $package->getVersion());
  125. $request->install($package->getName(), $constraint);
  126. }
  127. } else {
  128. $io->write('<info>Installing dependencies</info>');
  129. $links = $this->collectLinks($composer->getPackage(), $noInstallRecommends, $installSuggests);
  130. foreach ($links as $link) {
  131. $request->install($link->getTarget(), $link->getConstraint());
  132. }
  133. }
  134. // prepare solver
  135. $installationManager = $composer->getInstallationManager();
  136. $policy = new DependencyResolver\DefaultPolicy();
  137. $solver = new DependencyResolver\Solver($policy, $pool, $installedRepo);
  138. // solve dependencies
  139. $operations = $solver->solve($request);
  140. // check for missing deps
  141. // TODO this belongs in the solver, but this will do for now to report top-level deps missing at least
  142. foreach ($request->getJobs() as $job) {
  143. if ('install' === $job['cmd']) {
  144. foreach ($installedRepo->getPackages() as $package ) {
  145. if ($installedRepo->hasPackage($package) && !$package->isPlatform() && !$installationManager->isPackageInstalled($package)) {
  146. $operations[$job['packageName']] = new InstallOperation($package, Solver::RULE_PACKAGE_NOT_EXIST);
  147. }
  148. if (in_array($job['packageName'], $package->getNames())) {
  149. continue 2;
  150. }
  151. }
  152. foreach ($operations as $operation) {
  153. if ('install' === $operation->getJobType() && in_array($job['packageName'], $operation->getPackage()->getNames())) {
  154. continue 2;
  155. }
  156. if ('update' === $operation->getJobType() && in_array($job['packageName'], $operation->getTargetPackage()->getNames())) {
  157. continue 2;
  158. }
  159. }
  160. if ($pool->whatProvides($job['packageName'])) {
  161. 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.');
  162. }
  163. throw new \UnexpectedValueException('Package '.$job['packageName'].' was not found in the package pool, check the name for typos.');
  164. }
  165. }
  166. // execute operations
  167. if (!$operations) {
  168. $io->write('<info>Nothing to install/update</info>');
  169. }
  170. foreach ($operations as $operation) {
  171. if ($verbose) {
  172. $io->write((string) $operation);
  173. }
  174. if (!$dryRun) {
  175. $eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  176. // if installing from lock, restore dev packages' references to their locked state
  177. if ($installFromLock) {
  178. $package = null;
  179. if ('update' === $operation->getJobType()) {
  180. $package = $operation->getTargetPackage();
  181. } elseif ('install' === $operation->getJobType()) {
  182. $package = $operation->getPackage();
  183. }
  184. if ($package && $package->isDev()) {
  185. foreach ($composer->getLocker()->getLockedPackages() as $lockedPackage) {
  186. if (!empty($lockedPackage['source_reference']) && strtolower($lockedPackage['package']) === $package->getName()) {
  187. $package->setSourceReference($lockedPackage['source_reference']);
  188. }
  189. }
  190. }
  191. }
  192. $installationManager->execute($operation);
  193. $eventDispatcher->dispatchPackageEvent(constant('Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType())), $operation);
  194. }
  195. }
  196. if (!$dryRun) {
  197. if ($update || !$composer->getLocker()->isLocked()) {
  198. $composer->getLocker()->lockPackages($localRepo->getPackages());
  199. $io->write('<info>Writing lock file</info>');
  200. }
  201. $localRepo->write();
  202. $io->write('<info>Generating autoload files</info>');
  203. $generator = new AutoloadGenerator;
  204. $generator->dump($localRepo, $composer->getPackage(), $installationManager, $installationManager->getVendorPath().'/.composer');
  205. // dispatch post event
  206. $eventName = $update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD;
  207. $eventDispatcher->dispatchCommandEvent($eventName);
  208. }
  209. }
  210. private function collectLinks(PackageInterface $package, $noInstallRecommends, $installSuggests)
  211. {
  212. $links = $package->getRequires();
  213. if (!$noInstallRecommends) {
  214. $links = array_merge($links, $package->getRecommends());
  215. }
  216. if ($installSuggests) {
  217. $links = array_merge($links, $package->getSuggests());
  218. }
  219. return $links;
  220. }
  221. }