ConsoleIO.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\OutputInterface;
  14. use Symfony\Component\Console\Helper\HelperSet;
  15. use Symfony\Component\Process\ExecutableFinder;
  16. /**
  17. * The Input/Output helper.
  18. *
  19. * @author François Pluchino <francois.pluchino@opendisplay.com>
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class ConsoleIO extends BaseIO
  23. {
  24. protected $input;
  25. protected $output;
  26. protected $helperSet;
  27. protected $lastMessage;
  28. private $startTime;
  29. /**
  30. * Constructor.
  31. *
  32. * @param InputInterface $input The input instance
  33. * @param OutputInterface $output The output instance
  34. * @param HelperSet $helperSet The helperSet instance
  35. */
  36. public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
  37. {
  38. $this->input = $input;
  39. $this->output = $output;
  40. $this->helperSet = $helperSet;
  41. }
  42. public function enableDebugging($startTime)
  43. {
  44. $this->startTime = $startTime;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function isInteractive()
  50. {
  51. return $this->input->isInteractive();
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function isDecorated()
  57. {
  58. return $this->output->isDecorated();
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function isVerbose()
  64. {
  65. return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function isVeryVerbose()
  71. {
  72. return $this->output->getVerbosity() >= 3; // OutputInterface::VERSOBITY_VERY_VERBOSE
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function isDebug()
  78. {
  79. return $this->output->getVerbosity() >= 4; // OutputInterface::VERBOSITY_DEBUG
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function write($messages, $newline = true)
  85. {
  86. if (null !== $this->startTime) {
  87. $messages = (array) $messages;
  88. $startTime = $this->startTime;
  89. $messages = array_map(function ($message) use ($startTime) {
  90. return sprintf(
  91. '[%.1fMB/%.2fs] %s',
  92. memory_get_usage() / 1024 / 1024,
  93. microtime(true) - $startTime,
  94. $message
  95. );
  96. }, $messages);
  97. }
  98. $this->output->write($messages, $newline);
  99. $this->lastMessage = join($newline ? "\n" : '', (array) $messages);
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function overwrite($messages, $newline = true, $size = null)
  105. {
  106. if (!$this->output->isDecorated()) {
  107. if (!$messages) {
  108. return;
  109. }
  110. return $this->write($messages, count($messages) === 1 || $newline);
  111. }
  112. // messages can be an array, let's convert it to string anyway
  113. $messages = join($newline ? "\n" : '', (array) $messages);
  114. // since overwrite is supposed to overwrite last message...
  115. if (!isset($size)) {
  116. // removing possible formatting of lastMessage with strip_tags
  117. $size = strlen(strip_tags($this->lastMessage));
  118. }
  119. // ...let's fill its length with backspaces
  120. $this->write(str_repeat("\x08", $size), false);
  121. // write the new message
  122. $this->write($messages, false);
  123. $fill = $size - strlen(strip_tags($messages));
  124. if ($fill > 0) {
  125. // whitespace whatever has left
  126. $this->write(str_repeat(' ', $fill), false);
  127. // move the cursor back
  128. $this->write(str_repeat("\x08", $fill), false);
  129. }
  130. if ($newline) {
  131. $this->write('');
  132. }
  133. $this->lastMessage = $messages;
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function ask($question, $default = null)
  139. {
  140. return $this->helperSet->get('dialog')->ask($this->output, $question, $default);
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public function askConfirmation($question, $default = true)
  146. {
  147. return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default);
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function askAndValidate($question, $validator, $attempts = false, $default = null)
  153. {
  154. return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default);
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function askAndHideAnswer($question)
  160. {
  161. // handle windows
  162. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  163. $finder = new ExecutableFinder();
  164. // use bash if it's present
  165. if ($finder->find('bash') && $finder->find('stty')) {
  166. $this->write($question, false);
  167. $value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"'));
  168. $this->write('');
  169. return $value;
  170. }
  171. // fallback to hiddeninput executable
  172. $exe = __DIR__.'\\hiddeninput.exe';
  173. // handle code running from a phar
  174. if ('phar:' === substr(__FILE__, 0, 5)) {
  175. $tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
  176. // use stream_copy_to_stream instead of copy
  177. // to work around https://bugs.php.net/bug.php?id=64634
  178. $source = fopen(__DIR__.'\\hiddeninput.exe', 'r');
  179. $target = fopen($tmpExe, 'w+');
  180. stream_copy_to_stream($source, $target);
  181. fclose($source);
  182. fclose($target);
  183. unset($source, $target);
  184. $exe = $tmpExe;
  185. }
  186. $this->write($question, false);
  187. $value = rtrim(shell_exec($exe));
  188. $this->write('');
  189. // clean up
  190. if (isset($tmpExe)) {
  191. unlink($tmpExe);
  192. }
  193. return $value;
  194. }
  195. if (file_exists('/usr/bin/env')) {
  196. // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
  197. $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
  198. foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
  199. if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
  200. $shell = $sh;
  201. break;
  202. }
  203. }
  204. if (isset($shell)) {
  205. $this->write($question, false);
  206. $readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';
  207. $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
  208. $value = rtrim(shell_exec($command));
  209. $this->write('');
  210. return $value;
  211. }
  212. }
  213. // not able to hide the answer, proceed with normal question handling
  214. return $this->ask($question);
  215. }
  216. }