ConsoleIO.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. for ($i = 0; $i < strlen($value); ++$i) {
  114. $this->write('*', false);
  115. }
  116. $this->write('');
  117. return $value;
  118. }
  119. // for other OS with shell_exec (hide the answer)
  120. $command = "/usr/bin/env bash -c 'echo OK'";
  121. if (rtrim(shell_exec($command)) === 'OK') {
  122. $this->write($question, false);
  123. $command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'";
  124. $value = rtrim(shell_exec($command));
  125. for ($i = 0; $i < strlen($value); ++$i) {
  126. $this->write('*', false);
  127. }
  128. $this->write('');
  129. return $value;
  130. }
  131. // for other OS without shell_exec (does not hide the answer)
  132. $this->write('');
  133. return $this->ask($question);
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function getLastUsername()
  139. {
  140. return $this->lastUsername;
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public function getLastPassword()
  146. {
  147. return $this->lastPassword;
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function getAuthorizations()
  153. {
  154. return $this->authorizations;
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function hasAuthorization($repositoryName)
  160. {
  161. $auths = $this->getAuthorizations();
  162. return isset($auths[$repositoryName]);
  163. }
  164. /**
  165. * {@inheritDoc}
  166. */
  167. public function getAuthorization($repositoryName)
  168. {
  169. $auths = $this->getAuthorizations();
  170. return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null);
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public function setAuthorization($repositoryName, $username, $password = null)
  176. {
  177. $auths = $this->getAuthorizations();
  178. $auths[$repositoryName] = array('username' => $username, 'password' => $password);
  179. $this->authorizations = $auths;
  180. $this->lastUsername = $username;
  181. $this->lastPassword = $password;
  182. }
  183. }