Application.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Composer\Command;
  19. use Composer\Command\Helper\DialogHelper;
  20. use Composer\Composer;
  21. use Composer\Factory;
  22. use Composer\IO\IOInterface;
  23. use Composer\IO\ConsoleIO;
  24. use Composer\Util\ErrorHandler;
  25. /**
  26. * The console application that handles the commands
  27. *
  28. * @author Ryan Weaver <ryan@knplabs.com>
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. * @author François Pluchino <francois.pluchino@opendisplay.com>
  31. */
  32. class Application extends BaseApplication
  33. {
  34. /**
  35. * @var Composer
  36. */
  37. protected $composer;
  38. /**
  39. * @var IOInterface
  40. */
  41. protected $io;
  42. public function __construct()
  43. {
  44. ErrorHandler::register();
  45. parent::__construct('Composer', Composer::VERSION);
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function run(InputInterface $input = null, OutputInterface $output = null)
  51. {
  52. if (null === $output) {
  53. $styles['highlight'] = new OutputFormatterStyle('red');
  54. $styles['warning'] = new OutputFormatterStyle('black', 'yellow');
  55. $formatter = new OutputFormatter(null, $styles);
  56. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  57. }
  58. return parent::run($input, $output);
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function doRun(InputInterface $input, OutputInterface $output)
  64. {
  65. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  66. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  67. $output->writeln('<warning>Composer only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP '.PHP_VERSION.', upgrading is strongly recommended.</warning>');
  68. }
  69. if (defined('COMPOSER_DEV_WARNING_TIME') && $this->getCommandName($input) !== 'self-update') {
  70. if (time() > COMPOSER_DEV_WARNING_TIME) {
  71. $output->writeln(sprintf('<warning>This dev build of composer is outdated, please run "%s self-update" to get the latest version.</warning>', $_SERVER['PHP_SELF']));
  72. }
  73. }
  74. return parent::doRun($input, $output);
  75. }
  76. /**
  77. * @param bool $required
  78. * @return \Composer\Composer
  79. */
  80. public function getComposer($required = true)
  81. {
  82. if (null === $this->composer) {
  83. try {
  84. $this->composer = Factory::create($this->io);
  85. } catch (\InvalidArgumentException $e) {
  86. if ($required) {
  87. $this->io->write($e->getMessage());
  88. exit(1);
  89. }
  90. }
  91. }
  92. return $this->composer;
  93. }
  94. /**
  95. * @return IOInterface
  96. */
  97. public function getIO()
  98. {
  99. return $this->io;
  100. }
  101. /**
  102. * Initializes all the composer commands
  103. */
  104. protected function getDefaultCommands()
  105. {
  106. $commands = parent::getDefaultCommands();
  107. $commands[] = new Command\AboutCommand();
  108. $commands[] = new Command\DependsCommand();
  109. $commands[] = new Command\InitCommand();
  110. $commands[] = new Command\InstallCommand();
  111. $commands[] = new Command\CreateProjectCommand();
  112. $commands[] = new Command\UpdateCommand();
  113. $commands[] = new Command\SearchCommand();
  114. $commands[] = new Command\ValidateCommand();
  115. $commands[] = new Command\ShowCommand();
  116. $commands[] = new Command\RequireCommand();
  117. $commands[] = new Command\DumpAutoloadCommand();
  118. if ('phar:' === substr(__FILE__, 0, 5)) {
  119. $commands[] = new Command\SelfUpdateCommand();
  120. }
  121. return $commands;
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. protected function getDefaultHelperSet()
  127. {
  128. $helperSet = parent::getDefaultHelperSet();
  129. $helperSet->set(new DialogHelper());
  130. return $helperSet;
  131. }
  132. }