ConsoleIO.php 8.7 KB

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