ConsoleIO.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /**
  31. * Constructor.
  32. *
  33. * @param InputInterface $input The input instance
  34. * @param OutputInterface $output The output instance
  35. * @param HelperSet $helperSet The helperSet instance
  36. */
  37. public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
  38. {
  39. $this->input = $input;
  40. $this->output = $output;
  41. $this->helperSet = $helperSet;
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function isInteractive()
  47. {
  48. return $this->input->isInteractive();
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function write($messages, $newline = true)
  54. {
  55. $this->output->write($messages, $newline);
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function overwrite($messages, $newline = true, $size = 80)
  61. {
  62. for ($place = $size; $place > 0; $place--) {
  63. $this->write("\x08", false);
  64. }
  65. $this->write($messages, false);
  66. for ($place = ($size - strlen($messages)); $place > 0; $place--) {
  67. $this->write(' ', false);
  68. }
  69. // clean up the end line
  70. for ($place = ($size - strlen($messages)); $place > 0; $place--) {
  71. $this->write("\x08", false);
  72. }
  73. if ($newline) {
  74. $this->write('');
  75. }
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function ask($question, $default = null)
  81. {
  82. return $this->helperSet->get('dialog')->ask($this->output, $question, $default);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function askConfirmation($question, $default = true)
  88. {
  89. return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function askAndValidate($question, $validator, $attempts = false, $default = null)
  95. {
  96. return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default);
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function askAndHideAnswer($question)
  102. {
  103. // for windows OS (does not hide the answer in the popup, but it never appears in the STDIN history)
  104. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  105. $vbscript = sys_get_temp_dir() . '/prompt_password.vbs';
  106. file_put_contents($vbscript,
  107. 'wscript.echo(Inputbox("' . addslashes($question) . '","'
  108. . addslashes($question) . '", ""))');
  109. $command = "cscript //nologo " . escapeshellarg($vbscript);
  110. $this->write($question, false);
  111. $value = rtrim(shell_exec($command));
  112. unlink($vbscript);
  113. $this->write('***');
  114. return $value;
  115. }
  116. // for other OS with shell_exec (hide the answer)
  117. $command = "/usr/bin/env bash -c 'echo OK'";
  118. if (rtrim(shell_exec($command)) === 'OK') {
  119. $this->write($question, false);
  120. $command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'";
  121. $value = rtrim(shell_exec($command));
  122. for ($i = 0; $i < strlen($value); ++$i) {
  123. $this->write('*', false);
  124. }
  125. $this->write('');
  126. return $value;
  127. }
  128. // for other OS without shell_exec (does not hide the answer)
  129. $this->write('');
  130. return $this->ask($question);
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getLastUsername()
  136. {
  137. return $this->lastUsername;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function getLastPassword()
  143. {
  144. return $this->lastPassword;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getAuthorizations()
  150. {
  151. return $this->authorizations;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function hasAuthorization($repositoryName)
  157. {
  158. $auths = $this->getAuthorizations();
  159. return isset($auths[$repositoryName]);
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function getAuthorization($repositoryName)
  165. {
  166. $auths = $this->getAuthorizations();
  167. return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null);
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. public function setAuthorization($repositoryName, $username, $password = null)
  173. {
  174. $auths = $this->getAuthorizations();
  175. $auths[$repositoryName] = array('username' => $username, 'password' => $password);
  176. $this->authorizations = $auths;
  177. $this->lastUsername = $username;
  178. $this->lastPassword = $password;
  179. }
  180. }