Application.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. return parent::doRun($input, $output);
  70. }
  71. /**
  72. * @param bool $required
  73. * @return \Composer\Composer
  74. */
  75. public function getComposer($required = true)
  76. {
  77. if (null === $this->composer) {
  78. try {
  79. $this->composer = Factory::create($this->io);
  80. } catch (\InvalidArgumentException $e) {
  81. if ($required) {
  82. $this->io->write($e->getMessage());
  83. exit(1);
  84. }
  85. }
  86. }
  87. return $this->composer;
  88. }
  89. /**
  90. * @return IOInterface
  91. */
  92. public function getIO()
  93. {
  94. return $this->io;
  95. }
  96. /**
  97. * Initializes all the composer commands
  98. */
  99. protected function getDefaultCommands()
  100. {
  101. $commands = parent::getDefaultCommands();
  102. $commands[] = new Command\AboutCommand();
  103. $commands[] = new Command\DependsCommand();
  104. $commands[] = new Command\InitCommand();
  105. $commands[] = new Command\InstallCommand();
  106. $commands[] = new Command\CreateProjectCommand();
  107. $commands[] = new Command\UpdateCommand();
  108. $commands[] = new Command\SearchCommand();
  109. $commands[] = new Command\ValidateCommand();
  110. $commands[] = new Command\ShowCommand();
  111. $commands[] = new Command\RequireCommand();
  112. if ('phar:' === substr(__FILE__, 0, 5)) {
  113. $commands[] = new Command\SelfUpdateCommand();
  114. }
  115. return $commands;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. protected function getDefaultHelperSet()
  121. {
  122. $helperSet = parent::getDefaultHelperSet();
  123. $helperSet->set(new DialogHelper());
  124. return $helperSet;
  125. }
  126. }