ConsoleOutput.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Console\Output;
  12. use Symfony\Component\Console\Output\ConsoleOutput as BaseConsoleOutput;
  13. /**
  14. * ConsoleOutput is the default class for all CLI output.
  15. *
  16. * @author François Pluchino <francois.pluchino@opendisplay.com>
  17. */
  18. class ConsoleOutput extends BaseConsoleOutput
  19. {
  20. /**
  21. * Overwrites a previous message to the output.
  22. *
  23. * @param string|array $messages The message as an array of lines of a single string
  24. * @param integer $size The size of line
  25. * @param Boolean $newline Whether to add a newline or not
  26. * @param integer $type The type of output
  27. */
  28. public function overwrite($messages, $size = 80, $newline = false, $type = 0)
  29. {
  30. for ($place = $size; $place > 0; $place--) {
  31. $this->write("\x08");
  32. }
  33. $this->write($messages, false, $type);
  34. for ($place = ($size - strlen($line)); $place > 0; $place--) {
  35. $this->write(' ');
  36. }
  37. // clean up the end line
  38. for ($place = ($size - strlen($messages)); $place > 0; $place--) {
  39. $this->write("\x08");
  40. }
  41. if ($newline) {
  42. $this->writeln('');
  43. }
  44. }
  45. /**
  46. * Overwrites a previous message to the output and adds a newline at the end.
  47. *
  48. * @param string|array $messages The message as an array of lines of a single string
  49. * @param integer $size The size of line
  50. * @param integer $type The type of output
  51. */
  52. public function overwriteln($messages, $size = 80, $type = 0)
  53. {
  54. $this->write($messages, $size, true, $type);
  55. }
  56. /**
  57. * Interactively prompts for input without echoing to the terminal.
  58. * Requires a bash shell or Windows and won't work with safe_mode
  59. * settings (Uses `shell_exec`).
  60. *
  61. * @param string $title The title of prompt (only for windows)
  62. *
  63. * @return string The value
  64. */
  65. public function promptSilent($title = '')
  66. {
  67. if (preg_match('/^win/i', PHP_OS)) {
  68. $vbscript = sys_get_temp_dir() . '/prompt_password.vbs';
  69. file_put_contents($vbscript,
  70. 'wscript.echo(Inputbox("' . addslashes($title) . '","'
  71. . addslashes($title) . '", ""))');
  72. $command = "cscript //nologo " . escapeshellarg($vbscript);
  73. $value = rtrim(shell_exec($command));
  74. unlink($vbscript);
  75. $this->writeln('');
  76. return $value;
  77. } else {
  78. $command = "/usr/bin/env bash -c 'echo OK'";
  79. if (rtrim(shell_exec($command)) !== 'OK') {
  80. trigger_error("Can't invoke bash");
  81. return;
  82. }
  83. $command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'";
  84. $value = rtrim(shell_exec($command));
  85. $this->writeln('');
  86. return $value;
  87. }
  88. }
  89. }