Application.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Output\ConsoleOutput;
  18. use Symfony\Component\Console\Formatter\OutputFormatter;
  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. use Composer\Json\JsonValidationException;
  26. use Composer\Util\ErrorHandler;
  27. /**
  28. * The console application that handles the commands
  29. *
  30. * @author Ryan Weaver <ryan@knplabs.com>
  31. * @author Jordi Boggiano <j.boggiano@seld.be>
  32. * @author François Pluchino <francois.pluchino@opendisplay.com>
  33. */
  34. class Application extends BaseApplication
  35. {
  36. /**
  37. * @var Composer
  38. */
  39. protected $composer;
  40. /**
  41. * @var IOInterface
  42. */
  43. protected $io;
  44. private static $logo = ' ______
  45. / ____/___ ____ ___ ____ ____ ________ _____
  46. / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
  47. / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
  48. \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
  49. /_/
  50. ';
  51. public function __construct()
  52. {
  53. if (function_exists('ini_set') && extension_loaded('xdebug')) {
  54. ini_set('xdebug.show_exception_trace', false);
  55. ini_set('xdebug.scream', false);
  56. }
  57. if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
  58. date_default_timezone_set(@date_default_timezone_get());
  59. }
  60. ErrorHandler::register();
  61. parent::__construct('Composer', Composer::VERSION);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function run(InputInterface $input = null, OutputInterface $output = null)
  67. {
  68. if (null === $output) {
  69. $styles = Factory::createAdditionalStyles();
  70. $formatter = new OutputFormatter(null, $styles);
  71. $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
  72. }
  73. return parent::run($input, $output);
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function doRun(InputInterface $input, OutputInterface $output)
  79. {
  80. $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
  81. if (version_compare(PHP_VERSION, '5.3.2', '<')) {
  82. $this->getIO()->writeError('<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>');
  83. }
  84. if (defined('COMPOSER_DEV_WARNING_TIME')) {
  85. $commandName = '';
  86. if ($name = $this->getCommandName($input)) {
  87. try {
  88. $commandName = $this->find($name)->getName();
  89. } catch (\InvalidArgumentException $e) {
  90. }
  91. }
  92. if ($commandName !== 'self-update' && $commandName !== 'selfupdate') {
  93. if (time() > COMPOSER_DEV_WARNING_TIME) {
  94. $this->getIO()->writeError(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']));
  95. }
  96. }
  97. }
  98. if (getenv('COMPOSER_NO_INTERACTION')) {
  99. $input->setInteractive(false);
  100. }
  101. // switch working dir
  102. if ($newWorkDir = $this->getNewWorkingDir($input)) {
  103. $oldWorkingDir = getcwd();
  104. chdir($newWorkDir);
  105. if ($this->getIO()->isDebug() >= 4) {
  106. $this->getIO()->writeError('Changed CWD to ' . getcwd());
  107. }
  108. }
  109. // add non-standard scripts as own commands
  110. $file = Factory::getComposerFile();
  111. if (is_file($file) && is_readable($file) && is_array($composer = json_decode(file_get_contents($file), true))) {
  112. if (isset($composer['scripts']) && is_array($composer['scripts'])) {
  113. foreach ($composer['scripts'] as $script => $dummy) {
  114. if (!defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  115. if ($this->has($script)) {
  116. $this->getIO()->writeError('<warning>A script named '.$script.' would override a native Composer function and has been skipped</warning>');
  117. } else {
  118. $this->add(new Command\ScriptAliasCommand($script));
  119. }
  120. }
  121. }
  122. }
  123. }
  124. if ($input->hasParameterOption('--profile')) {
  125. $startTime = microtime(true);
  126. $this->io->enableDebugging($startTime);
  127. }
  128. $result = parent::doRun($input, $output);
  129. if (isset($oldWorkingDir)) {
  130. chdir($oldWorkingDir);
  131. }
  132. if (isset($startTime)) {
  133. $this->getIO()->writeError('<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');
  134. }
  135. return $result;
  136. }
  137. /**
  138. * @param InputInterface $input
  139. * @return string
  140. * @throws \RuntimeException
  141. */
  142. private function getNewWorkingDir(InputInterface $input)
  143. {
  144. $workingDir = $input->getParameterOption(array('--working-dir', '-d'));
  145. if (false !== $workingDir && !is_dir($workingDir)) {
  146. throw new \RuntimeException('Invalid working directory specified.');
  147. }
  148. return $workingDir;
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public function renderException($exception, $output)
  154. {
  155. try {
  156. $composer = $this->getComposer(false, true);
  157. if ($composer) {
  158. $config = $composer->getConfig();
  159. $minSpaceFree = 1024*1024;
  160. if ((($df = @disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree)
  161. || (($df = @disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree)
  162. || (($df = @disk_free_space($dir = sys_get_temp_dir())) !== false && $df < $minSpaceFree)
  163. ) {
  164. $this->getIO()->writeError('<error>The disk hosting '.$dir.' is full, this may be the cause of the following exception</error>');
  165. }
  166. }
  167. } catch (\Exception $e) {
  168. }
  169. if (defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) {
  170. $this->getIO()->writeError('<error>The following exception may be caused by a stale entry in your cmd.exe AutoRun</error>');
  171. $this->getIO()->writeError('<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details</error>');
  172. }
  173. if (false !== strpos($exception->getMessage(), 'fork failed - Cannot allocate memory')) {
  174. $this->getIO()->writeError('<error>The following exception is caused by a lack of memory and not having swap configured</error>');
  175. $this->getIO()->writeError('<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details</error>');
  176. }
  177. if ($output instanceof ConsoleOutputInterface) {
  178. parent::renderException($exception, $output->getErrorOutput());
  179. } else {
  180. parent::renderException($exception, $output);
  181. }
  182. }
  183. /**
  184. * @param bool $required
  185. * @param bool $disablePlugins
  186. * @throws JsonValidationException
  187. * @return \Composer\Composer
  188. */
  189. public function getComposer($required = true, $disablePlugins = false)
  190. {
  191. if (null === $this->composer) {
  192. try {
  193. $this->composer = Factory::create($this->io, null, $disablePlugins);
  194. } catch (\InvalidArgumentException $e) {
  195. if ($required) {
  196. $this->io->writeError($e->getMessage());
  197. exit(1);
  198. }
  199. } catch (JsonValidationException $e) {
  200. $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
  201. $message = $e->getMessage() . ':' . PHP_EOL . $errors;
  202. throw new JsonValidationException($message);
  203. }
  204. }
  205. return $this->composer;
  206. }
  207. /**
  208. * Removes the cached composer instance
  209. */
  210. public function resetComposer()
  211. {
  212. $this->composer = null;
  213. }
  214. /**
  215. * @return IOInterface
  216. */
  217. public function getIO()
  218. {
  219. return $this->io;
  220. }
  221. public function getHelp()
  222. {
  223. return self::$logo . parent::getHelp();
  224. }
  225. /**
  226. * Initializes all the composer commands
  227. */
  228. protected function getDefaultCommands()
  229. {
  230. $commands = parent::getDefaultCommands();
  231. $commands[] = new Command\AboutCommand();
  232. $commands[] = new Command\ConfigCommand();
  233. $commands[] = new Command\DependsCommand();
  234. $commands[] = new Command\InitCommand();
  235. $commands[] = new Command\InstallCommand();
  236. $commands[] = new Command\CreateProjectCommand();
  237. $commands[] = new Command\UpdateCommand();
  238. $commands[] = new Command\SearchCommand();
  239. $commands[] = new Command\ValidateCommand();
  240. $commands[] = new Command\ShowCommand();
  241. $commands[] = new Command\RequireCommand();
  242. $commands[] = new Command\DumpAutoloadCommand();
  243. $commands[] = new Command\StatusCommand();
  244. $commands[] = new Command\ArchiveCommand();
  245. $commands[] = new Command\DiagnoseCommand();
  246. $commands[] = new Command\RunScriptCommand();
  247. $commands[] = new Command\LicensesCommand();
  248. $commands[] = new Command\GlobalCommand();
  249. $commands[] = new Command\ClearCacheCommand();
  250. $commands[] = new Command\RemoveCommand();
  251. $commands[] = new Command\HomeCommand();
  252. if ('phar:' === substr(__FILE__, 0, 5)) {
  253. $commands[] = new Command\SelfUpdateCommand();
  254. }
  255. return $commands;
  256. }
  257. /**
  258. * {@inheritDoc}
  259. */
  260. public function getLongVersion()
  261. {
  262. if (Composer::BRANCH_ALIAS_VERSION) {
  263. return sprintf(
  264. '<info>%s</info> version <comment>%s (%s)</comment> %s',
  265. $this->getName(),
  266. Composer::BRANCH_ALIAS_VERSION,
  267. $this->getVersion(),
  268. Composer::RELEASE_DATE
  269. );
  270. }
  271. return parent::getLongVersion() . ' ' . Composer::RELEASE_DATE;
  272. }
  273. /**
  274. * {@inheritDoc}
  275. */
  276. protected function getDefaultInputDefinition()
  277. {
  278. $definition = parent::getDefaultInputDefinition();
  279. $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
  280. $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.'));
  281. return $definition;
  282. }
  283. /**
  284. * {@inheritDoc}
  285. */
  286. protected function getDefaultHelperSet()
  287. {
  288. $helperSet = parent::getDefaultHelperSet();
  289. $helperSet->set(new DialogHelper());
  290. return $helperSet;
  291. }
  292. }