Application.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. if (function_exists('ini_set')) {
  46. ini_set('xdebug.show_exception_trace', false);
  47. ini_set('xdebug.scream', false);
  48. }
  49. parent::__construct('Composer', Composer::VERSION);
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function run(InputInterface $input = null, OutputInterface $output = null)
  55. {
  56. if (null === $output) {
  57. $styles = Factory::createAdditionalStyles();
  58. $formatter = new OutputFormatter(null, $styles);
  59. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  60. }
  61. return parent::run($input, $output);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function doRun(InputInterface $input, OutputInterface $output)
  67. {
  68. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  69. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  70. $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>');
  71. }
  72. if (defined('COMPOSER_DEV_WARNING_TIME') && $this->getCommandName($input) !== 'self-update') {
  73. if (time() > COMPOSER_DEV_WARNING_TIME) {
  74. $output->writeln(sprintf('<warning>Warning: This development build of composer is over 30 days old. It is recommended to update it by running "%s self-update" to get the latest version.</warning>', $_SERVER['PHP_SELF']));
  75. }
  76. }
  77. if ($input->hasParameterOption('--profile')) {
  78. $startTime = microtime(true);
  79. }
  80. $oldWorkingDir = getcwd();
  81. $this->switchWorkingDir($input);
  82. $result = parent::doRun($input, $output);
  83. chdir($oldWorkingDir);
  84. if (isset($startTime)) {
  85. $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');
  86. }
  87. return $result;
  88. }
  89. /**
  90. * @param InputInterface $input
  91. * @throws \RuntimeException
  92. */
  93. private function switchWorkingDir(InputInterface $input)
  94. {
  95. $workingDir = $input->getParameterOption(array('--working-dir', '-d'), getcwd());
  96. if (!is_dir($workingDir)) {
  97. throw new \RuntimeException('Invalid working directory specified.');
  98. }
  99. chdir($workingDir);
  100. }
  101. /**
  102. * @param bool $required
  103. * @return \Composer\Composer
  104. */
  105. public function getComposer($required = true)
  106. {
  107. if (null === $this->composer) {
  108. try {
  109. $this->composer = Factory::create($this->io);
  110. } catch (\InvalidArgumentException $e) {
  111. if ($required) {
  112. $this->io->write($e->getMessage());
  113. exit(1);
  114. }
  115. }
  116. }
  117. return $this->composer;
  118. }
  119. /**
  120. * @return IOInterface
  121. */
  122. public function getIO()
  123. {
  124. return $this->io;
  125. }
  126. /**
  127. * Initializes all the composer commands
  128. */
  129. protected function getDefaultCommands()
  130. {
  131. $commands = parent::getDefaultCommands();
  132. $commands[] = new Command\AboutCommand();
  133. $commands[] = new Command\ConfigCommand();
  134. $commands[] = new Command\DependsCommand();
  135. $commands[] = new Command\InitCommand();
  136. $commands[] = new Command\InstallCommand();
  137. $commands[] = new Command\CreateProjectCommand();
  138. $commands[] = new Command\UpdateCommand();
  139. $commands[] = new Command\SearchCommand();
  140. $commands[] = new Command\ValidateCommand();
  141. $commands[] = new Command\ShowCommand();
  142. $commands[] = new Command\RequireCommand();
  143. $commands[] = new Command\DumpAutoloadCommand();
  144. $commands[] = new Command\StatusCommand();
  145. if ('phar:' === substr(__FILE__, 0, 5)) {
  146. $commands[] = new Command\SelfUpdateCommand();
  147. }
  148. return $commands;
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. protected function getDefaultInputDefinition()
  154. {
  155. $definition = parent::getDefaultInputDefinition();
  156. $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
  157. $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.'));
  158. return $definition;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. protected function getDefaultHelperSet()
  164. {
  165. $helperSet = parent::getDefaultHelperSet();
  166. $helperSet->set(new DialogHelper());
  167. return $helperSet;
  168. }
  169. }