Application.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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\Json\JsonValidationException;
  25. use Composer\Util\ErrorHandler;
  26. /**
  27. * The console application that handles the commands
  28. *
  29. * @author Ryan Weaver <ryan@knplabs.com>
  30. * @author Jordi Boggiano <j.boggiano@seld.be>
  31. * @author François Pluchino <francois.pluchino@opendisplay.com>
  32. */
  33. class Application extends BaseApplication
  34. {
  35. /**
  36. * @var Composer
  37. */
  38. protected $composer;
  39. /**
  40. * @var IOInterface
  41. */
  42. protected $io;
  43. private static $logo = ' ______
  44. / ____/___ ____ ___ ____ ____ ________ _____
  45. / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
  46. / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
  47. \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
  48. /_/
  49. ';
  50. public function __construct()
  51. {
  52. if (function_exists('ini_set') && extension_loaded('xdebug')) {
  53. ini_set('xdebug.show_exception_trace', false);
  54. ini_set('xdebug.scream', false);
  55. }
  56. if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
  57. date_default_timezone_set(@date_default_timezone_get());
  58. }
  59. ErrorHandler::register();
  60. parent::__construct('Composer', Composer::VERSION);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function run(InputInterface $input = null, OutputInterface $output = null)
  66. {
  67. if (null === $output) {
  68. $styles = Factory::createAdditionalStyles();
  69. $formatter = new OutputFormatter(null, $styles);
  70. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  71. }
  72. return parent::run($input, $output);
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function doRun(InputInterface $input, OutputInterface $output)
  78. {
  79. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  80. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  81. $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>');
  82. }
  83. if (defined('COMPOSER_DEV_WARNING_TIME') && $this->getCommandName($input) !== 'self-update' && $this->getCommandName($input) !== 'selfupdate') {
  84. if (time() > COMPOSER_DEV_WARNING_TIME) {
  85. $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']));
  86. }
  87. }
  88. if (getenv('COMPOSER_NO_INTERACTION')) {
  89. $input->setInteractive(false);
  90. }
  91. // add non-standard scripts as own commands
  92. if ($composer = $this->getComposer(false)) {
  93. foreach ($composer->getPackage()->getScripts() as $script => $dummy) {
  94. if (!defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  95. $this->add(new Command\ScriptAliasCommand($script));
  96. }
  97. }
  98. }
  99. if ($input->hasParameterOption('--profile')) {
  100. $startTime = microtime(true);
  101. $this->io->enableDebugging($startTime);
  102. }
  103. if ($newWorkDir = $this->getNewWorkingDir($input)) {
  104. $oldWorkingDir = getcwd();
  105. chdir($newWorkDir);
  106. }
  107. $result = parent::doRun($input, $output);
  108. if (isset($oldWorkingDir)) {
  109. chdir($oldWorkingDir);
  110. }
  111. if (isset($startTime)) {
  112. $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');
  113. }
  114. return $result;
  115. }
  116. /**
  117. * @param InputInterface $input
  118. * @throws \RuntimeException
  119. */
  120. private function getNewWorkingDir(InputInterface $input)
  121. {
  122. $workingDir = $input->getParameterOption(array('--working-dir', '-d'));
  123. if (false !== $workingDir && !is_dir($workingDir)) {
  124. throw new \RuntimeException('Invalid working directory specified.');
  125. }
  126. return $workingDir;
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function renderException($exception, $output)
  132. {
  133. try {
  134. $composer = $this->getComposer(false);
  135. if ($composer) {
  136. $config = $composer->getConfig();
  137. $minSpaceFree = 1024*1024;
  138. if ((($df = @disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree)
  139. || (($df = @disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree)
  140. ) {
  141. $output->writeln('<error>The disk hosting '.$dir.' is full, this may be the cause of the following exception</error>');
  142. }
  143. }
  144. } catch (\Exception $e) {}
  145. return parent::renderException($exception, $output);
  146. }
  147. /**
  148. * @param bool $required
  149. * @param bool $disablePlugins
  150. * @throws JsonValidationException
  151. * @return \Composer\Composer
  152. */
  153. public function getComposer($required = true, $disablePlugins = false)
  154. {
  155. if (null === $this->composer) {
  156. try {
  157. $this->composer = Factory::create($this->io, null, $disablePlugins);
  158. } catch (\InvalidArgumentException $e) {
  159. if ($required) {
  160. $this->io->write($e->getMessage());
  161. exit(1);
  162. }
  163. } catch (JsonValidationException $e) {
  164. $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
  165. $message = $e->getMessage() . ':' . PHP_EOL . $errors;
  166. throw new JsonValidationException($message);
  167. }
  168. }
  169. return $this->composer;
  170. }
  171. /**
  172. * @return IOInterface
  173. */
  174. public function getIO()
  175. {
  176. return $this->io;
  177. }
  178. public function getHelp()
  179. {
  180. return self::$logo . parent::getHelp();
  181. }
  182. /**
  183. * Initializes all the composer commands
  184. */
  185. protected function getDefaultCommands()
  186. {
  187. $commands = parent::getDefaultCommands();
  188. $commands[] = new Command\AboutCommand();
  189. $commands[] = new Command\ConfigCommand();
  190. $commands[] = new Command\DependsCommand();
  191. $commands[] = new Command\InitCommand();
  192. $commands[] = new Command\InstallCommand();
  193. $commands[] = new Command\CreateProjectCommand();
  194. $commands[] = new Command\UpdateCommand();
  195. $commands[] = new Command\SearchCommand();
  196. $commands[] = new Command\ValidateCommand();
  197. $commands[] = new Command\ShowCommand();
  198. $commands[] = new Command\RequireCommand();
  199. $commands[] = new Command\DumpAutoloadCommand();
  200. $commands[] = new Command\StatusCommand();
  201. $commands[] = new Command\ArchiveCommand();
  202. $commands[] = new Command\DiagnoseCommand();
  203. $commands[] = new Command\RunScriptCommand();
  204. $commands[] = new Command\LicensesCommand();
  205. $commands[] = new Command\GlobalCommand();
  206. $commands[] = new Command\ClearCacheCommand();
  207. $commands[] = new Command\RemoveCommand();
  208. $commands[] = new Command\HomeCommand();
  209. if ('phar:' === substr(__FILE__, 0, 5)) {
  210. $commands[] = new Command\SelfUpdateCommand();
  211. }
  212. return $commands;
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. public function getLongVersion()
  218. {
  219. return parent::getLongVersion() . ' ' . Composer::RELEASE_DATE;
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. protected function getDefaultInputDefinition()
  225. {
  226. $definition = parent::getDefaultInputDefinition();
  227. $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
  228. $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.'));
  229. return $definition;
  230. }
  231. /**
  232. * {@inheritDoc}
  233. */
  234. protected function getDefaultHelperSet()
  235. {
  236. $helperSet = parent::getDefaultHelperSet();
  237. $helperSet->set(new DialogHelper());
  238. return $helperSet;
  239. }
  240. }