1
0
Fork 0

Use external lib for hidden cli prompting

pull/3991/head
Jordi Boggiano 2015-05-01 15:46:34 +01:00
parent 414cf70008
commit b7c827c2e5
5 changed files with 55 additions and 68 deletions

View File

@ -28,7 +28,8 @@
"symfony/console": "~2.5",
"symfony/finder": "~2.2",
"symfony/process": "~2.1",
"seld/phar-utils": "~1.0"
"seld/phar-utils": "~1.0",
"seld/cli-prompt": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5",

50
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "3b9e15f9521bb0a41897f65f76c1c7de",
"hash": "0a85d5ff8ee6c1e805ef16f2afd87238",
"packages": [
{
"name": "justinrainbow/json-schema",
@ -72,6 +72,54 @@
],
"time": "2015-03-27 16:41:39"
},
{
"name": "seld/cli-prompt",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/cli-prompt.git",
"reference": "fe114c7a6ac5cb0ce76932ae4017024d9842a49c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/cli-prompt/zipball/fe114c7a6ac5cb0ce76932ae4017024d9842a49c",
"reference": "fe114c7a6ac5cb0ce76932ae4017024d9842a49c",
"shasum": ""
},
"require": {
"php": ">=5.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"Seld\\CliPrompt\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be"
}
],
"description": "Allows you to prompt for user input on the command line, and optionally hide the characters they type",
"keywords": [
"cli",
"console",
"hidden",
"input",
"prompt"
],
"time": "2015-04-30 20:24:49"
},
{
"name": "seld/jsonlint",
"version": "1.3.1",

View File

@ -102,7 +102,7 @@ class Compiler
foreach ($finder as $file) {
$this->addFile($phar, $file, false);
}
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../src/Composer/IO/hiddeninput.exe'), false);
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../vendor/seld/cli-prompt/res/hiddeninput.exe'), false);
$finder = new Finder();
$finder->files()
@ -114,6 +114,7 @@ class Compiler
->exclude('docs')
->in(__DIR__.'/../../vendor/symfony/')
->in(__DIR__.'/../../vendor/seld/jsonlint/')
->in(__DIR__.'/../../vendor/seld/cli-prompt/')
->in(__DIR__.'/../../vendor/justinrainbow/json-schema/')
->sort($finderSort)
;

View File

@ -254,71 +254,8 @@ class ConsoleIO extends BaseIO
*/
public function askAndHideAnswer($question)
{
// handle windows
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$finder = new ExecutableFinder();
// use bash if it's present
if ($finder->find('bash') && $finder->find('stty')) {
$this->writeError($question, false);
$value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"'));
$this->writeError('');
return $value;
}
// fallback to hiddeninput executable
$exe = __DIR__.'\\hiddeninput.exe';
// handle code running from a phar
if ('phar:' === substr(__FILE__, 0, 5)) {
$tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
// use stream_copy_to_stream instead of copy
// to work around https://bugs.php.net/bug.php?id=64634
$source = fopen(__DIR__.'\\hiddeninput.exe', 'r');
$target = fopen($tmpExe, 'w+');
stream_copy_to_stream($source, $target);
fclose($source);
fclose($target);
unset($source, $target);
$exe = $tmpExe;
}
$this->writeError($question, false);
$value = rtrim(shell_exec($exe));
$this->writeError('');
// clean up
if (isset($tmpExe)) {
unlink($tmpExe);
}
return $value;
}
if (file_exists('/usr/bin/env')) {
// handle other OSs with bash/zsh/ksh/csh if available to hide the answer
$test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
$shell = $sh;
break;
}
}
if (isset($shell)) {
$this->writeError($question, false);
$readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
$value = rtrim(shell_exec($command));
$this->writeError('');
return $value;
}
}
// not able to hide the answer, proceed with normal question handling
return $this->ask($question);
return \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
}
}

Binary file not shown.