ConsoleIO.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. use Symfony\Component\Process\ExecutableFinder;
  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. protected $input;
  28. protected $output;
  29. protected $helperSet;
  30. protected $lastMessage;
  31. protected $lastMessageErr;
  32. private $startTime;
  33. /**
  34. * Constructor.
  35. *
  36. * @param InputInterface $input The input instance
  37. * @param OutputInterface $output The output instance
  38. * @param HelperSet $helperSet The helperSet instance
  39. */
  40. public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
  41. {
  42. $this->input = $input;
  43. $this->output = $output;
  44. $this->helperSet = $helperSet;
  45. }
  46. public function enableDebugging($startTime)
  47. {
  48. $this->startTime = $startTime;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function isInteractive()
  54. {
  55. return $this->input->isInteractive();
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function isDecorated()
  61. {
  62. return $this->output->isDecorated();
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function isVerbose()
  68. {
  69. return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function isVeryVerbose()
  75. {
  76. return $this->output->getVerbosity() >= 3; // OutputInterface::VERSOBITY_VERY_VERBOSE
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function isDebug()
  82. {
  83. return $this->output->getVerbosity() >= 4; // OutputInterface::VERBOSITY_DEBUG
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function write($messages, $newline = true)
  89. {
  90. $this->doWrite($messages, $newline, false);
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function writeError($messages, $newline = true)
  96. {
  97. $this->doWrite($messages, $newline, true);
  98. }
  99. /**
  100. * @param array $messages
  101. * @param boolean $newline
  102. * @param boolean $stderr
  103. */
  104. private function doWrite($messages, $newline, $stderr)
  105. {
  106. if (null !== $this->startTime) {
  107. $memoryUsage = memory_get_usage() / 1024 / 1024;
  108. $timeSpent = microtime(true) - $this->startTime;
  109. $messages = array_map(function ($message) use ($memoryUsage, $timeSpent) {
  110. return sprintf('[%.1fMB/%.2fs] %s', $memoryUsage, $timeSpent, $message);
  111. }, (array) $messages);
  112. }
  113. if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
  114. $this->output->getErrorOutput()->write($messages, $newline);
  115. $this->lastMessageErr = join($newline ? "\n" : '', (array) $messages);
  116. return;
  117. }
  118. $this->output->write($messages, $newline);
  119. $this->lastMessage = join($newline ? "\n" : '', (array) $messages);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function overwrite($messages, $newline = true, $size = null)
  125. {
  126. $this->doOverwrite($messages, $newline, $size, false);
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function overwriteError($messages, $newline = true, $size = null)
  132. {
  133. $this->doOverwrite($messages, $newline, $size, true);
  134. }
  135. /**
  136. * @param array $messages
  137. * @param boolean $newline
  138. * @param integer $size
  139. * @param boolean $stderr
  140. */
  141. private function doOverwrite($messages, $newline, $size, $stderr)
  142. {
  143. if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
  144. $output = $this->output->getErrorOutput();
  145. } else {
  146. $output = $this->output;
  147. }
  148. // messages can be an array, let's convert it to string anyway
  149. $messages = join($newline ? "\n" : '', (array) $messages);
  150. // since overwrite is supposed to overwrite last message...
  151. if (!isset($size)) {
  152. // removing possible formatting of lastMessage with strip_tags
  153. $size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage));
  154. }
  155. // ...let's fill its length with backspaces
  156. $this->doWrite(str_repeat("\x08", $size), false, $stderr);
  157. // write the new message
  158. $this->doWrite($messages, false, $stderr);
  159. $fill = $size - strlen(strip_tags($messages));
  160. if ($fill > 0) {
  161. // whitespace whatever has left
  162. $this->doWrite(str_repeat(' ', $fill), false, $stderr);
  163. // move the cursor back
  164. $this->doWrite(str_repeat("\x08", $fill), false, $stderr);
  165. }
  166. if ($newline) {
  167. $this->doWrite('', true, $stderr);
  168. }
  169. if ($stderr) {
  170. $this->lastMessageErr = $messages;
  171. } else {
  172. $this->lastMessage = $messages;
  173. }
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function ask($question, $default = null)
  179. {
  180. $output = $this->output;
  181. if ($output instanceof ConsoleOutputInterface) {
  182. $output = $output->getErrorOutput();
  183. }
  184. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  185. $helper = $this->helperSet->get('question');
  186. $question = new Question($question, $default);
  187. return $helper->ask($this->input, $output, $question);
  188. }
  189. /**
  190. * {@inheritDoc}
  191. */
  192. public function askConfirmation($question, $default = true)
  193. {
  194. $output = $this->output;
  195. if ($output instanceof ConsoleOutputInterface) {
  196. $output = $output->getErrorOutput();
  197. }
  198. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  199. $helper = $this->helperSet->get('question');
  200. $question = new ConfirmationQuestion($question, $default);
  201. return $helper->ask($this->input, $output, $question);
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function askAndValidate($question, $validator, $attempts = null, $default = null)
  207. {
  208. $output = $this->output;
  209. if ($output instanceof ConsoleOutputInterface) {
  210. $output = $output->getErrorOutput();
  211. }
  212. /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
  213. $helper = $this->helperSet->get('question');
  214. $question = new Question($question, $default);
  215. $question->setValidator($validator);
  216. $question->setMaxAttempts($attempts);
  217. return $helper->ask($this->input, $output, $question);
  218. }
  219. /**
  220. * {@inheritDoc}
  221. */
  222. public function askAndHideAnswer($question)
  223. {
  224. // handle windows
  225. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  226. $finder = new ExecutableFinder();
  227. // use bash if it's present
  228. if ($finder->find('bash') && $finder->find('stty')) {
  229. $this->writeError($question, false);
  230. $value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"'));
  231. $this->writeError('');
  232. return $value;
  233. }
  234. // fallback to hiddeninput executable
  235. $exe = __DIR__.'\\hiddeninput.exe';
  236. // handle code running from a phar
  237. if ('phar:' === substr(__FILE__, 0, 5)) {
  238. $tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
  239. // use stream_copy_to_stream instead of copy
  240. // to work around https://bugs.php.net/bug.php?id=64634
  241. $source = fopen(__DIR__.'\\hiddeninput.exe', 'r');
  242. $target = fopen($tmpExe, 'w+');
  243. stream_copy_to_stream($source, $target);
  244. fclose($source);
  245. fclose($target);
  246. unset($source, $target);
  247. $exe = $tmpExe;
  248. }
  249. $this->writeError($question, false);
  250. $value = rtrim(shell_exec($exe));
  251. $this->writeError('');
  252. // clean up
  253. if (isset($tmpExe)) {
  254. unlink($tmpExe);
  255. }
  256. return $value;
  257. }
  258. if (file_exists('/usr/bin/env')) {
  259. // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
  260. $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
  261. foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
  262. if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
  263. $shell = $sh;
  264. break;
  265. }
  266. }
  267. if (isset($shell)) {
  268. $this->writeError($question, false);
  269. $readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';
  270. $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
  271. $value = rtrim(shell_exec($command));
  272. $this->writeError('');
  273. return $value;
  274. }
  275. }
  276. // not able to hide the answer, proceed with normal question handling
  277. return $this->ask($question);
  278. }
  279. }