Application.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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')) {
  84. $commandName = '';
  85. if ($name = $this->getCommandName($input)) {
  86. try {
  87. $commandName = $this->find($name)->getName();
  88. } catch (\InvalidArgumentException $e) {
  89. }
  90. }
  91. if ($commandName !== 'self-update' && $commandName !== 'selfupdate') {
  92. if (time() > COMPOSER_DEV_WARNING_TIME) {
  93. $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']));
  94. }
  95. }
  96. }
  97. if (getenv('COMPOSER_NO_INTERACTION')) {
  98. $input->setInteractive(false);
  99. }
  100. // switch working dir
  101. if ($newWorkDir = $this->getNewWorkingDir($input)) {
  102. $oldWorkingDir = getcwd();
  103. chdir($newWorkDir);
  104. if ($output->getVerbosity() >= 4) {
  105. $output->writeln('Changed CWD to ' . getcwd());
  106. }
  107. }
  108. // add non-standard scripts as own commands
  109. $file = Factory::getComposerFile();
  110. if (is_file($file) && is_readable($file) && is_array($composer = json_decode(file_get_contents($file), true))) {
  111. if (isset($composer['scripts']) && is_array($composer['scripts'])) {
  112. foreach ($composer['scripts'] as $script => $dummy) {
  113. if (!defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  114. if ($this->has($script)) {
  115. $output->writeln('<warning>A script named '.$script.' would override a native Composer function and has been skipped</warning>');
  116. } else {
  117. $this->add(new Command\ScriptAliasCommand($script));
  118. }
  119. }
  120. }
  121. }
  122. }
  123. if ($input->hasParameterOption('--profile')) {
  124. $startTime = microtime(true);
  125. $this->io->enableDebugging($startTime);
  126. }
  127. $result = parent::doRun($input, $output);
  128. if (isset($oldWorkingDir)) {
  129. chdir($oldWorkingDir);
  130. }
  131. if (isset($startTime)) {
  132. $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');
  133. }
  134. return $result;
  135. }
  136. /**
  137. * @param InputInterface $input
  138. * @return string
  139. * @throws \RuntimeException
  140. */
  141. private function getNewWorkingDir(InputInterface $input)
  142. {
  143. $workingDir = $input->getParameterOption(array('--working-dir', '-d'));
  144. if (false !== $workingDir && !is_dir($workingDir)) {
  145. throw new \RuntimeException('Invalid working directory specified.');
  146. }
  147. return $workingDir;
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function renderException($exception, $output)
  153. {
  154. try {
  155. $composer = $this->getComposer(false, true);
  156. if ($composer) {
  157. $config = $composer->getConfig();
  158. $minSpaceFree = 1024*1024;
  159. if ((($df = @disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree)
  160. || (($df = @disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree)
  161. || (($df = @disk_free_space($dir = sys_get_temp_dir())) !== false && $df < $minSpaceFree)
  162. ) {
  163. $output->writeln('<error>The disk hosting '.$dir.' is full, this may be the cause of the following exception</error>');
  164. }
  165. }
  166. } catch (\Exception $e) {
  167. }
  168. if (defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) {
  169. $output->writeln('<error>The following exception may be caused by a stale entry in your cmd.exe AutoRun</error>');
  170. $output->writeln('<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details</error>');
  171. }
  172. if (false !== strpos($exception->getMessage(), 'fork failed - Cannot allocate memory')) {
  173. $output->writeln('<error>The following exception is caused by a lack of memory and not having swap configured</error>');
  174. $output->writeln('<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details</error>');
  175. }
  176. return parent::renderException($exception, $output);
  177. }
  178. /**
  179. * @param bool $required
  180. * @param bool $disablePlugins
  181. * @throws JsonValidationException
  182. * @return \Composer\Composer
  183. */
  184. public function getComposer($required = true, $disablePlugins = false)
  185. {
  186. if (null === $this->composer) {
  187. try {
  188. $this->composer = Factory::create($this->io, null, $disablePlugins);
  189. } catch (\InvalidArgumentException $e) {
  190. if ($required) {
  191. $this->io->write($e->getMessage());
  192. exit(1);
  193. }
  194. } catch (JsonValidationException $e) {
  195. $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
  196. $message = $e->getMessage() . ':' . PHP_EOL . $errors;
  197. throw new JsonValidationException($message);
  198. }
  199. }
  200. return $this->composer;
  201. }
  202. /**
  203. * Removes the cached composer instance
  204. */
  205. public function resetComposer()
  206. {
  207. $this->composer = null;
  208. }
  209. /**
  210. * @return IOInterface
  211. */
  212. public function getIO()
  213. {
  214. return $this->io;
  215. }
  216. public function getHelp()
  217. {
  218. return self::$logo . parent::getHelp();
  219. }
  220. /**
  221. * Initializes all the composer commands
  222. */
  223. protected function getDefaultCommands()
  224. {
  225. $commands = parent::getDefaultCommands();
  226. $commands[] = new Command\AboutCommand();
  227. $commands[] = new Command\ConfigCommand();
  228. $commands[] = new Command\DependsCommand();
  229. $commands[] = new Command\InitCommand();
  230. $commands[] = new Command\InstallCommand();
  231. $commands[] = new Command\CreateProjectCommand();
  232. $commands[] = new Command\UpdateCommand();
  233. $commands[] = new Command\SearchCommand();
  234. $commands[] = new Command\ValidateCommand();
  235. $commands[] = new Command\ShowCommand();
  236. $commands[] = new Command\RequireCommand();
  237. $commands[] = new Command\DumpAutoloadCommand();
  238. $commands[] = new Command\StatusCommand();
  239. $commands[] = new Command\ArchiveCommand();
  240. $commands[] = new Command\DiagnoseCommand();
  241. $commands[] = new Command\RunScriptCommand();
  242. $commands[] = new Command\LicensesCommand();
  243. $commands[] = new Command\GlobalCommand();
  244. $commands[] = new Command\ClearCacheCommand();
  245. $commands[] = new Command\RemoveCommand();
  246. $commands[] = new Command\HomeCommand();
  247. if ('phar:' === substr(__FILE__, 0, 5)) {
  248. $commands[] = new Command\SelfUpdateCommand();
  249. }
  250. return $commands;
  251. }
  252. /**
  253. * {@inheritDoc}
  254. */
  255. public function getLongVersion()
  256. {
  257. if (Composer::BRANCH_ALIAS_VERSION) {
  258. return sprintf(
  259. '<info>%s</info> version <comment>%s (%s)</comment> %s',
  260. $this->getName(),
  261. Composer::BRANCH_ALIAS_VERSION,
  262. $this->getVersion(),
  263. Composer::RELEASE_DATE
  264. );
  265. }
  266. return parent::getLongVersion() . ' ' . Composer::RELEASE_DATE;
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. protected function getDefaultInputDefinition()
  272. {
  273. $definition = parent::getDefaultInputDefinition();
  274. $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
  275. $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.'));
  276. return $definition;
  277. }
  278. /**
  279. * {@inheritDoc}
  280. */
  281. protected function getDefaultHelperSet()
  282. {
  283. $helperSet = parent::getDefaultHelperSet();
  284. $helperSet->set(new DialogHelper());
  285. return $helperSet;
  286. }
  287. }