InstallCommand.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\DependencyResolver;
  13. use Composer\DependencyResolver\Pool;
  14. use Composer\DependencyResolver\Request;
  15. use Composer\DependencyResolver\Operation;
  16. use Composer\Package\LinkConstraint\VersionConstraint;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. * @author Ryan Weaver <ryan@knplabs.com>
  22. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  23. */
  24. class InstallCommand extends Command
  25. {
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('install')
  30. ->setDescription('Parses the composer.json file and downloads the needed dependencies.')
  31. ->setHelp(<<<EOT
  32. The <info>install</info> command reads the composer.json file from the
  33. current directory, processes it, and downloads and installs all the
  34. libraries and dependencies outlined in that file.
  35. <info>php composer install</info>
  36. EOT
  37. )
  38. ;
  39. }
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $composer = $this->getComposer();
  43. // creating repository pool
  44. $pool = new Pool;
  45. $pool->addRepository($composer->getRepositoryManager()->getLocalRepository());
  46. foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
  47. $pool->addRepository($repository);
  48. }
  49. // creating requirements request
  50. $request = new Request($pool);
  51. if ($composer->getLocker()->isLocked()) {
  52. $output->writeln('> Found lockfile. Reading.');
  53. foreach ($composer->getLocker()->getLockedPackages() as $package) {
  54. $constraint = new VersionConstraint('=', $package->getVersion());
  55. $request->install($package->getName(), $constraint);
  56. }
  57. } else {
  58. foreach ($composer->getPackage()->getRequires() as $link) {
  59. $request->install($link->getTarget(), $link->getConstraint());
  60. }
  61. }
  62. // prepare solver
  63. $installationManager = $composer->getInstallationManager();
  64. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  65. $policy = new DependencyResolver\DefaultPolicy();
  66. $solver = new DependencyResolver\Solver($policy, $pool, $localRepo);
  67. // solve dependencies and execute operations
  68. foreach ($solver->solve($request) as $operation) {
  69. $installationManager->execute($operation);
  70. }
  71. if (!$composer->getLocker()->isLocked()) {
  72. $composer->getLocker()->lockPackages($localRepo->getPackages());
  73. $output->writeln('> Locked');
  74. }
  75. $localRepo->write();
  76. $output->writeln('> Done');
  77. }
  78. }