Application.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Console;
  12. use Symfony\Component\Console\Application as BaseApplication;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Finder\Finder;
  16. use Composer\Command;
  17. use Composer\Composer;
  18. use Composer\Installer;
  19. use Composer\Downloader;
  20. use Composer\Repository;
  21. use Composer\Package;
  22. use Composer\Json\JsonFile;
  23. /**
  24. * The console application that handles the commands
  25. *
  26. * @author Ryan Weaver <ryan@knplabs.com>
  27. * @author Jordi Boggiano <j.boggiano@seld.be>
  28. */
  29. class Application extends BaseApplication
  30. {
  31. protected $composer;
  32. public function __construct()
  33. {
  34. parent::__construct('Composer', Composer::VERSION);
  35. }
  36. /**
  37. * Runs the current application.
  38. *
  39. * @param InputInterface $input An Input instance
  40. * @param OutputInterface $output An Output instance
  41. *
  42. * @return integer 0 if everything went fine, or an error code
  43. */
  44. public function doRun(InputInterface $input, OutputInterface $output)
  45. {
  46. $this->registerCommands();
  47. return parent::doRun($input, $output);
  48. }
  49. /**
  50. * @return Composer
  51. */
  52. public function getComposer()
  53. {
  54. if (null === $this->composer) {
  55. $this->composer = self::bootstrapComposer();
  56. }
  57. return $this->composer;
  58. }
  59. /**
  60. * Bootstraps a Composer instance
  61. *
  62. * @return Composer
  63. */
  64. public static function bootstrapComposer($composerFile = null)
  65. {
  66. // load Composer configuration
  67. if (null === $composerFile) {
  68. $composerFile = getenv('COMPOSER') ?: 'composer.json';
  69. }
  70. $file = new JsonFile($composerFile);
  71. if (!$file->exists()) {
  72. if ($composerFile === 'composer.json') {
  73. echo 'Composer could not find a composer.json file in '.getcwd().PHP_EOL;
  74. } else {
  75. echo 'Composer could not find the config file: '.$composerFile.PHP_EOL;
  76. }
  77. echo 'To initialize a project, please create a composer.json file as described on the http://packagist.org/ "Getting Started" section'.PHP_EOL;
  78. exit(1);
  79. }
  80. // Configuration defaults
  81. $composerConfig = array(
  82. 'vendor-dir' => 'vendor',
  83. );
  84. $packageConfig = $file->read();
  85. if (isset($packageConfig['config']) && is_array($packageConfig['config'])) {
  86. $packageConfig['config'] = array_merge($composerConfig, $packageConfig['config']);
  87. } else {
  88. $packageConfig['config'] = $composerConfig;
  89. }
  90. $vendorDir = $packageConfig['config']['vendor-dir'];
  91. if (!isset($packageConfig['config']['bin-dir'])) {
  92. $packageConfig['config']['bin-dir'] = $vendorDir.'/bin';
  93. }
  94. $binDir = $packageConfig['config']['bin-dir'];
  95. // initialize repository manager
  96. $rm = new Repository\RepositoryManager();
  97. $rm->setLocalRepository(new Repository\FilesystemRepository(new JsonFile($vendorDir.'/.composer/installed.json')));
  98. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  99. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  100. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  101. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  102. // initialize download manager
  103. $dm = new Downloader\DownloadManager();
  104. $dm->setDownloader('git', new Downloader\GitDownloader());
  105. $dm->setDownloader('svn', new Downloader\SvnDownloader());
  106. $dm->setDownloader('hg', new Downloader\HgDownloader());
  107. $dm->setDownloader('pear', new Downloader\PearDownloader());
  108. $dm->setDownloader('zip', new Downloader\ZipDownloader());
  109. // initialize installation manager
  110. $im = new Installer\InstallationManager($vendorDir);
  111. $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $rm->getLocalRepository(), null));
  112. $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $rm->getLocalRepository(), $im));
  113. // load package
  114. $loader = new Package\Loader\ArrayLoader($rm);
  115. $package = $loader->load($packageConfig);
  116. // load default repository unless it's explicitly disabled
  117. if (!isset($packageConfig['repositories']['packagist']) || $packageConfig['repositories']['packagist'] !== false) {
  118. $rm->addRepository(new Repository\ComposerRepository(array('url' => 'http://packagist.org')));
  119. }
  120. // init locker
  121. $lockFile = substr($composerFile, -5) === '.json' ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock';
  122. $locker = new Package\Locker(new JsonFile($lockFile), $rm);
  123. // initialize composer
  124. $composer = new Composer();
  125. $composer->setPackage($package);
  126. $composer->setLocker($locker);
  127. $composer->setRepositoryManager($rm);
  128. $composer->setDownloadManager($dm);
  129. $composer->setInstallationManager($im);
  130. return $composer;
  131. }
  132. /**
  133. * Initializes all the composer commands
  134. */
  135. protected function registerCommands()
  136. {
  137. $this->add(new Command\AboutCommand());
  138. $this->add(new Command\InstallCommand());
  139. $this->add(new Command\UpdateCommand());
  140. $this->add(new Command\DebugPackagesCommand());
  141. if ('phar:' === substr(__FILE__, 0, 5)) {
  142. $this->add(new Command\SelfUpdateCommand());
  143. }
  144. }
  145. }