ConsoleIO.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /**
  16. * The Input/Output helper.
  17. *
  18. * @author François Pluchino <francois.pluchino@opendisplay.com>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class ConsoleIO implements IOInterface
  22. {
  23. protected $input;
  24. protected $output;
  25. protected $helperSet;
  26. protected $authorizations = array();
  27. protected $lastMessage;
  28. /**
  29. * Constructor.
  30. *
  31. * @param InputInterface $input The input instance
  32. * @param OutputInterface $output The output instance
  33. * @param HelperSet $helperSet The helperSet instance
  34. */
  35. public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
  36. {
  37. $this->input = $input;
  38. $this->output = $output;
  39. $this->helperSet = $helperSet;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function isInteractive()
  45. {
  46. return $this->input->isInteractive();
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function isDecorated()
  52. {
  53. return $this->output->isDecorated();
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function isVerbose()
  59. {
  60. return (bool) $this->input->getOption('verbose');
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function write($messages, $newline = true)
  66. {
  67. $this->output->write($messages, $newline);
  68. $this->lastMessage = join($newline ? "\n" : '', (array) $messages);
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function overwrite($messages, $newline = true, $size = null)
  74. {
  75. // messages can be an array, let's convert it to string anyway
  76. $messages = join($newline ? "\n" : '', (array) $messages);
  77. // since overwrite is supposed to overwrite last message...
  78. if (!isset($size)) {
  79. // removing possible formatting of lastMessage with strip_tags
  80. $size = strlen(strip_tags($this->lastMessage));
  81. }
  82. // ...let's fill its length with backspaces
  83. $this->write(str_repeat("\x08", $size), false);
  84. // write the new message
  85. $this->write($messages, false);
  86. $fill = $size - strlen(strip_tags($messages));
  87. if ($fill > 0) {
  88. // whitespace whatever has left
  89. $this->write(str_repeat(' ', $fill), false);
  90. // move the cursor back
  91. $this->write(str_repeat("\x08", $fill), false);
  92. }
  93. if ($newline) {
  94. $this->write('');
  95. }
  96. $this->lastMessage = $messages;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function ask($question, $default = null)
  102. {
  103. return $this->helperSet->get('dialog')->ask($this->output, $question, $default);
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function askConfirmation($question, $default = true)
  109. {
  110. return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default);
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function askAndValidate($question, $validator, $attempts = false, $default = null)
  116. {
  117. return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default);
  118. }
  119. /**
  120. * {@inheritDoc}
  121. */
  122. public function askAndHideAnswer($question)
  123. {
  124. // handle windows
  125. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  126. $exe = __DIR__.'\\hiddeninput.exe';
  127. // handle code running from a phar
  128. if ('phar:' === substr(__FILE__, 0, 5)) {
  129. $tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
  130. copy($exe, $tmpExe);
  131. $exe = $tmpExe;
  132. }
  133. $this->write($question, false);
  134. $value = rtrim(shell_exec($exe));
  135. $this->write('');
  136. // clean up
  137. if (isset($tmpExe)) {
  138. unlink($tmpExe);
  139. }
  140. return $value;
  141. }
  142. // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
  143. $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
  144. foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
  145. if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
  146. $shell = $sh;
  147. break;
  148. }
  149. }
  150. if (isset($shell)) {
  151. $this->write($question, false);
  152. $readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read mypassword';
  153. $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
  154. $value = rtrim(shell_exec($command));
  155. $this->write('');
  156. return $value;
  157. }
  158. // not able to hide the answer, proceed with normal question handling
  159. return $this->ask($question);
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function getAuthorizations()
  165. {
  166. return $this->authorizations;
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function hasAuthorization($repositoryName)
  172. {
  173. $auths = $this->getAuthorizations();
  174. return isset($auths[$repositoryName]);
  175. }
  176. /**
  177. * {@inheritDoc}
  178. */
  179. public function getAuthorization($repositoryName)
  180. {
  181. $auths = $this->getAuthorizations();
  182. return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null);
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. public function setAuthorization($repositoryName, $username, $password = null)
  188. {
  189. $this->authorizations[$repositoryName] = array('username' => $username, 'password' => $password);
  190. }
  191. }