ConsoleIO.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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\IO;
  12. use Composer\Question\StrictConfirmationQuestion;
  13. use Symfony\Component\Console\Helper\HelperSet;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Question\ChoiceQuestion;
  18. use Symfony\Component\Console\Question\Question;
  19. /**
  20. * The Input/Output helper.
  21. *
  22. * @author François Pluchino <francois.pluchino@opendisplay.com>
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class ConsoleIO extends BaseIO
  26. {
  27. /** @var InputInterface */
  28. protected $input;
  29. /** @var OutputInterface */
  30. protected $output;
  31. /** @var HelperSet */
  32. protected $helperSet;
  33. /** @var string */
  34. protected $lastMessage;
  35. /** @var string */
  36. protected $lastMessageErr;
  37. /** @var float */
  38. private $startTime;
  39. /** @var array<int, int> */
  40. private $verbosityMap;
  41. /**
  42. * Constructor.
  43. *
  44. * @param InputInterface $input The input instance
  45. * @param OutputInterface $output The output instance
  46. * @param HelperSet $helperSet The helperSet instance
  47. */
  48. public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
  49. {
  50. $this->input = $input;
  51. $this->output = $output;
  52. $this->helperSet = $helperSet;
  53. $this->verbosityMap = array(
  54. self::QUIET => OutputInterface::VERBOSITY_QUIET,
  55. self::NORMAL => OutputInterface::VERBOSITY_NORMAL,
  56. self::VERBOSE => OutputInterface::VERBOSITY_VERBOSE,
  57. self::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE,
  58. self::DEBUG => OutputInterface::VERBOSITY_DEBUG,
  59. );
  60. }
  61. /**
  62. * @param float $startTime
  63. */
  64. public function enableDebugging($startTime)
  65. {
  66. $this->startTime = $startTime;
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function isInteractive()
  72. {
  73. return $this->input->isInteractive();
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function isDecorated()
  79. {
  80. return $this->output->isDecorated();
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function isVerbose()
  86. {
  87. return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function isVeryVerbose()
  93. {
  94. return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function isDebug()
  100. {
  101. return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function write($messages, $newline = true, $verbosity = self::NORMAL)
  107. {
  108. $this->doWrite($messages, $newline, false, $verbosity);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
  114. {
  115. $this->doWrite($messages, $newline, true, $verbosity);
  116. }
  117. /**
  118. * @param array|string $messages
  119. * @param bool $newline
  120. * @param bool $stderr
  121. * @param int $verbosity
  122. */
  123. private function doWrite($messages, $newline, $stderr, $verbosity)
  124. {
  125. $sfVerbosity = $this->verbosityMap[$verbosity];
  126. if ($sfVerbosity > $this->output->getVerbosity()) {
  127. return;
  128. }
  129. // hack to keep our usage BC with symfony<2.8 versions
  130. // this removes the quiet output but there is no way around it
  131. // see https://github.com/composer/composer/pull/4913
  132. if (OutputInterface::VERBOSITY_QUIET === 0) {
  133. $sfVerbosity = OutputInterface::OUTPUT_NORMAL;
  134. }
  135. if (null !== $this->startTime) {
  136. $memoryUsage = memory_get_usage() / 1024 / 1024;
  137. $timeSpent = microtime(true) - $this->startTime;
  138. $messages = array_map(function ($message) use ($memoryUsage, $timeSpent) {
  139. return sprintf('[%.1fMB/%.2fs] %s', $memoryUsage, $timeSpent, $message);
  140. }, (array) $messages);
  141. }
  142. if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
  143. $this->output->getErrorOutput()->write($messages, $newline, $sfVerbosity);
  144. $this->lastMessageErr = implode($newline ? "\n" : '', (array) $messages);
  145. return;
  146. }
  147. $this->output->write($messages, $newline, $sfVerbosity);
  148. $this->lastMessage = implode($newline ? "\n" : '', (array) $messages);
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public function overwrite($messages, $newline = true, $size = null, $verbosity = self::NORMAL)
  154. {
  155. $this->doOverwrite($messages, $newline, $size, false, $verbosity);
  156. }
  157. /**
  158. * {@inheritDoc}
  159. */
  160. public function overwriteError($messages, $newline = true, $size = null, $verbosity = self::NORMAL)
  161. {
  162. $this->doOverwrite($messages, $newline, $size, true, $verbosity);
  163. }
  164. /**
  165. * @param array|string $messages
  166. * @param bool $newline
  167. * @param int|null $size
  168. * @param bool $stderr
  169. * @param int $verbosity
  170. */
  171. private function doOverwrite($messages, $newline, $size, $stderr, $verbosity)
  172. {
  173. // messages can be an array, let's convert it to string anyway
  174. $messages = implode($newline ? "\n" : '', (array) $messages);
  175. // since overwrite is supposed to overwrite last message...
  176. if (!isset($size)) {
  177. // removing possible formatting of lastMessage with strip_tags
  178. $size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage));
  179. }
  180. // ...let's fill its length with backspaces
  181. $this->doWrite(str_repeat("\x08", $size), false, $stderr, $verbosity);
  182. // write the new message
  183. $this->doWrite($messages, false, $stderr, $verbosity);
  184. // In cmd.exe on Win8.1 (possibly 10?), the line can not be cleared, so we need to
  185. // track the length of previous output and fill it with spaces to make sure the line is cleared.
  186. // See https://github.com/composer/composer/pull/5836 for more details
  187. $fill = $size - strlen(strip_tags($messages));
  188. if ($fill > 0) {
  189. // whitespace whatever has left
  190. $this->doWrite(str_repeat(' ', $fill), false, $stderr, $verbosity);
  191. // move the cursor back
  192. $this->doWrite(str_repeat("\x08", $fill), false, $stderr, $verbosity);
  193. }
  194. if ($newline) {
  195. $this->doWrite('', true, $stderr, $verbosity);
  196. }
  197. if ($stderr) {
  198. $this->lastMessageErr = $messages;
  199. } else {
  200. $this->lastMessage = $messages;
  201. }
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function ask($question, $default = null)
  207. {
  208. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  209. $helper = $this->helperSet->get('question');
  210. $question = new Question($question, $default);
  211. return $helper->ask($this->input, $this->getErrorOutput(), $question);
  212. }
  213. /**
  214. * {@inheritDoc}
  215. */
  216. public function askConfirmation($question, $default = true)
  217. {
  218. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  219. $helper = $this->helperSet->get('question');
  220. $question = new StrictConfirmationQuestion($question, $default);
  221. return $helper->ask($this->input, $this->getErrorOutput(), $question);
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function askAndValidate($question, $validator, $attempts = null, $default = null)
  227. {
  228. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  229. $helper = $this->helperSet->get('question');
  230. $question = new Question($question, $default);
  231. $question->setValidator($validator);
  232. $question->setMaxAttempts($attempts);
  233. return $helper->ask($this->input, $this->getErrorOutput(), $question);
  234. }
  235. /**
  236. * {@inheritDoc}
  237. */
  238. public function askAndHideAnswer($question)
  239. {
  240. $this->writeError($question, false);
  241. return \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
  242. }
  243. /**
  244. * {@inheritDoc}
  245. */
  246. public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
  247. {
  248. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  249. $helper = $this->helperSet->get('question');
  250. $question = new ChoiceQuestion($question, $choices, $default);
  251. $question->setMaxAttempts($attempts ?: null); // IOInterface requires false, and Question requires null or int
  252. $question->setErrorMessage($errorMessage);
  253. $question->setMultiselect($multiselect);
  254. $result = $helper->ask($this->input, $this->getErrorOutput(), $question);
  255. if (!is_array($result)) {
  256. return (string) array_search($result, $choices, true);
  257. }
  258. $results = array();
  259. foreach ($choices as $index => $choice) {
  260. if (in_array($choice, $result, true)) {
  261. $results[] = (string) $index;
  262. }
  263. }
  264. return $results;
  265. }
  266. /**
  267. * @return OutputInterface
  268. */
  269. private function getErrorOutput()
  270. {
  271. if ($this->output instanceof ConsoleOutputInterface) {
  272. return $this->output->getErrorOutput();
  273. }
  274. return $this->output;
  275. }
  276. }