InstallCommand.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  13. * @author Jordi Boggiano <j.boggiano@seld.be>
  14. */
  15. class InstallCommand
  16. {
  17. protected $composer;
  18. public function install($composer)
  19. {
  20. $this->composer = $composer;
  21. $config = $this->loadConfig();
  22. foreach ($config['repositories'] as $name => $spec) {
  23. $composer->addRepository($name, $spec);
  24. }
  25. // TODO this should just do dependency solving based on all repositories
  26. $packages = array();
  27. foreach ($composer->getRepositories() as $repository) {
  28. $packages[] = $repository->getPackages();
  29. }
  30. $packages = call_user_func_array('array_merge', $packages);
  31. $lock = array();
  32. // TODO this should use the transaction returned by the solver
  33. foreach ($config['require'] as $name => $version) {
  34. unset($package);
  35. foreach ($packages as $pkg) {
  36. if (strtolower($pkg->getName()) === strtolower($name)) {
  37. $package = $pkg;
  38. break;
  39. }
  40. }
  41. if (!isset($package)) {
  42. throw new \UnexpectedValueException('Could not find package '.$name.' in any of your repositories');
  43. }
  44. $downloader = $composer->getDownloader($package->getSourceType());
  45. $installer = $composer->getInstaller($package->getType());
  46. $lock[$name] = $installer->install($package, $downloader);
  47. echo '> '.$name.' installed'.PHP_EOL;
  48. }
  49. $this->storeLockFile($lock);
  50. }
  51. protected function loadConfig()
  52. {
  53. if (!file_exists('composer.json')) {
  54. throw new \UnexpectedValueException('composer.json config file not found in '.getcwd());
  55. }
  56. $config = json_decode(file_get_contents('composer.json'), true);
  57. if (!$config) {
  58. throw new \UnexpectedValueException('Incorrect composer.json file');
  59. }
  60. return $config;
  61. }
  62. protected function storeLockFile(array $content)
  63. {
  64. file_put_contents('composer.lock', json_encode($content)."\n");
  65. echo '> composer.lock dumped'.PHP_EOL;
  66. }
  67. }