Application.php 6.4 KB

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