Application.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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->registerCommands();
  60. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  61. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  62. $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>');
  63. }
  64. return parent::doRun($input, $output);
  65. }
  66. /**
  67. * @return Composer
  68. */
  69. public function getComposer($required = true)
  70. {
  71. if (null === $this->composer) {
  72. try {
  73. $this->composer = Factory::create($this->io);
  74. } catch (\InvalidArgumentException $e) {
  75. if ($required) {
  76. $this->io->write($e->getMessage());
  77. exit(1);
  78. }
  79. return;
  80. }
  81. }
  82. return $this->composer;
  83. }
  84. /**
  85. * @return IOInterface
  86. */
  87. public function getIO()
  88. {
  89. return $this->io;
  90. }
  91. /**
  92. * Initializes all the composer commands
  93. */
  94. protected function registerCommands()
  95. {
  96. $this->add(new Command\AboutCommand());
  97. $this->add(new Command\DependsCommand());
  98. $this->add(new Command\InitCommand());
  99. $this->add(new Command\InstallCommand());
  100. $this->add(new Command\CreateProjectCommand());
  101. $this->add(new Command\UpdateCommand());
  102. $this->add(new Command\SearchCommand());
  103. $this->add(new Command\ValidateCommand());
  104. $this->add(new Command\ShowCommand());
  105. if ('phar:' === substr(__FILE__, 0, 5)) {
  106. $this->add(new Command\SelfUpdateCommand());
  107. }
  108. }
  109. /**
  110. * {@inheritDoc}
  111. */
  112. protected function getDefaultHelperSet()
  113. {
  114. $helperSet = parent::getDefaultHelperSet();
  115. $helperSet->set(new DialogHelper());
  116. return $helperSet;
  117. }
  118. }