Application.php 3.5 KB

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