ConsoleIO.php 6.0 KB

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