Application.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Output\ConsoleOutput;
  17. use Symfony\Component\Console\Formatter\OutputFormatter;
  18. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  19. use Composer\Command;
  20. use Composer\Command\Helper\DialogHelper;
  21. use Composer\Composer;
  22. use Composer\Factory;
  23. use Composer\IO\IOInterface;
  24. use Composer\IO\ConsoleIO;
  25. use Composer\Util\ErrorHandler;
  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. * @author François Pluchino <francois.pluchino@opendisplay.com>
  32. */
  33. class Application extends BaseApplication
  34. {
  35. /**
  36. * @var Composer
  37. */
  38. protected $composer;
  39. /**
  40. * @var IOInterface
  41. */
  42. protected $io;
  43. public function __construct()
  44. {
  45. ErrorHandler::register();
  46. parent::__construct('Composer', Composer::VERSION);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function run(InputInterface $input = null, OutputInterface $output = null)
  52. {
  53. if (null === $output) {
  54. $styles['highlight'] = new OutputFormatterStyle('red');
  55. $styles['warning'] = new OutputFormatterStyle('black', 'yellow');
  56. $formatter = new OutputFormatter(null, $styles);
  57. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  58. }
  59. return parent::run($input, $output);
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function doRun(InputInterface $input, OutputInterface $output)
  65. {
  66. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  67. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  68. $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>');
  69. }
  70. if (defined('COMPOSER_DEV_WARNING_TIME') && $this->getCommandName($input) !== 'self-update') {
  71. if (time() > COMPOSER_DEV_WARNING_TIME) {
  72. $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']));
  73. }
  74. }
  75. if ($input->hasParameterOption('--profile')) {
  76. $startTime = microtime(true);
  77. }
  78. $result = parent::doRun($input, $output);
  79. if (isset($startTime)) {
  80. $output->writeln('<info>Memory usage: '.round(memory_get_usage() / 1024 / 1024, 2).'MB (peak: '.round(memory_get_peak_usage() / 1024 / 1024, 2).'MB), time: '.round(microtime(true) - $startTime, 2).'s');
  81. }
  82. return $result;
  83. }
  84. /**
  85. * @param bool $required
  86. * @return \Composer\Composer
  87. */
  88. public function getComposer($required = true)
  89. {
  90. if (null === $this->composer) {
  91. try {
  92. $this->composer = Factory::create($this->io);
  93. } catch (\InvalidArgumentException $e) {
  94. if ($required) {
  95. $this->io->write($e->getMessage());
  96. exit(1);
  97. }
  98. }
  99. }
  100. return $this->composer;
  101. }
  102. /**
  103. * @return IOInterface
  104. */
  105. public function getIO()
  106. {
  107. return $this->io;
  108. }
  109. /**
  110. * Initializes all the composer commands
  111. */
  112. protected function getDefaultCommands()
  113. {
  114. $commands = parent::getDefaultCommands();
  115. $commands[] = new Command\AboutCommand();
  116. $commands[] = new Command\DependsCommand();
  117. $commands[] = new Command\InitCommand();
  118. $commands[] = new Command\InstallCommand();
  119. $commands[] = new Command\CreateProjectCommand();
  120. $commands[] = new Command\UpdateCommand();
  121. $commands[] = new Command\SearchCommand();
  122. $commands[] = new Command\ValidateCommand();
  123. $commands[] = new Command\ShowCommand();
  124. $commands[] = new Command\RequireCommand();
  125. $commands[] = new Command\DumpAutoloadCommand();
  126. $commands[] = new Command\StatusCommand();
  127. if ('phar:' === substr(__FILE__, 0, 5)) {
  128. $commands[] = new Command\SelfUpdateCommand();
  129. }
  130. return $commands;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. protected function getDefaultInputDefinition()
  136. {
  137. $definition = parent::getDefaultInputDefinition();
  138. $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
  139. return $definition;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. protected function getDefaultHelperSet()
  145. {
  146. $helperSet = parent::getDefaultHelperSet();
  147. $helperSet->set(new DialogHelper());
  148. return $helperSet;
  149. }
  150. }