Application.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. protected $composer;
  35. protected $io;
  36. public function __construct()
  37. {
  38. ErrorHandler::register();
  39. parent::__construct('Composer', Composer::VERSION);
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function run(InputInterface $input = null, OutputInterface $output = null)
  45. {
  46. if (null === $output) {
  47. $styles['highlight'] = new OutputFormatterStyle('red');
  48. $styles['warning'] = new OutputFormatterStyle('black', 'yellow');
  49. $formatter = new OutputFormatter(null, $styles);
  50. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  51. }
  52. return parent::run($input, $output);
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function doRun(InputInterface $input, OutputInterface $output)
  58. {
  59. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  60. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  61. $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>');
  62. }
  63. return parent::doRun($input, $output);
  64. }
  65. /**
  66. * @return Composer
  67. */
  68. public function getComposer($required = true)
  69. {
  70. if (null === $this->composer) {
  71. try {
  72. $this->composer = Factory::create($this->io);
  73. } catch (\InvalidArgumentException $e) {
  74. if ($required) {
  75. $this->io->write($e->getMessage());
  76. exit(1);
  77. }
  78. return;
  79. }
  80. }
  81. return $this->composer;
  82. }
  83. /**
  84. * @return IOInterface
  85. */
  86. public function getIO()
  87. {
  88. return $this->io;
  89. }
  90. /**
  91. * Initializes all the composer commands
  92. */
  93. protected function getDefaultCommands()
  94. {
  95. $commands = parent::getDefaultCommands();
  96. $commands[] = new Command\AboutCommand();
  97. $commands[] = new Command\DependsCommand();
  98. $commands[] = new Command\InitCommand();
  99. $commands[] = new Command\InstallCommand();
  100. $commands[] = new Command\CreateProjectCommand();
  101. $commands[] = new Command\UpdateCommand();
  102. $commands[] = new Command\SearchCommand();
  103. $commands[] = new Command\ValidateCommand();
  104. $commands[] = new Command\ShowCommand();
  105. $commands[] = new Command\RequireCommand();
  106. if ('phar:' === substr(__FILE__, 0, 5)) {
  107. $commands[] = new Command\SelfUpdateCommand();
  108. }
  109. return $commands;
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. protected function getDefaultHelperSet()
  115. {
  116. $helperSet = parent::getDefaultHelperSet();
  117. $helperSet->set(new DialogHelper());
  118. return $helperSet;
  119. }
  120. }