bug #10798 [Console] Fix #10795: Allow instancing Console Application when STDIN is not declared (romainneutron)
This PR was merged into the 2.5-dev branch.
Discussion
----------
[Console] Fix #10795: Allow instancing Console Application when STDIN is not declared
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #10795
| License | MIT
Commits
-------
3a5a525
[Console] Fix #10795: Allow instancing Console Application when STDIN is not declared
This commit is contained in:
commit
f4f09d176b
@ -28,11 +28,6 @@ class QuestionHelper extends Helper
|
|||||||
private static $shell;
|
private static $shell;
|
||||||
private static $stty;
|
private static $stty;
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->inputStream = STDIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asks a question to the user.
|
* Asks a question to the user.
|
||||||
*
|
*
|
||||||
@ -114,6 +109,8 @@ class QuestionHelper extends Helper
|
|||||||
*/
|
*/
|
||||||
public function doAsk(OutputInterface $output, Question $question)
|
public function doAsk(OutputInterface $output, Question $question)
|
||||||
{
|
{
|
||||||
|
$inputStream = $this->inputStream ?: STDIN;
|
||||||
|
|
||||||
$message = $question->getQuestion();
|
$message = $question->getQuestion();
|
||||||
if ($question instanceof ChoiceQuestion) {
|
if ($question instanceof ChoiceQuestion) {
|
||||||
$width = max(array_map('strlen', array_keys($question->getChoices())));
|
$width = max(array_map('strlen', array_keys($question->getChoices())));
|
||||||
@ -135,7 +132,7 @@ class QuestionHelper extends Helper
|
|||||||
$ret = false;
|
$ret = false;
|
||||||
if ($question->isHidden()) {
|
if ($question->isHidden()) {
|
||||||
try {
|
try {
|
||||||
$ret = trim($this->getHiddenResponse($output));
|
$ret = trim($this->getHiddenResponse($output, $inputStream));
|
||||||
} catch (\RuntimeException $e) {
|
} catch (\RuntimeException $e) {
|
||||||
if (!$question->isHiddenFallback()) {
|
if (!$question->isHiddenFallback()) {
|
||||||
throw $e;
|
throw $e;
|
||||||
@ -144,14 +141,14 @@ class QuestionHelper extends Helper
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (false === $ret) {
|
if (false === $ret) {
|
||||||
$ret = fgets($this->inputStream, 4096);
|
$ret = fgets($inputStream, 4096);
|
||||||
if (false === $ret) {
|
if (false === $ret) {
|
||||||
throw new \RuntimeException('Aborted');
|
throw new \RuntimeException('Aborted');
|
||||||
}
|
}
|
||||||
$ret = trim($ret);
|
$ret = trim($ret);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$ret = trim($this->autocomplete($output, $question));
|
$ret = trim($this->autocomplete($output, $question, $inputStream));
|
||||||
}
|
}
|
||||||
|
|
||||||
$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
|
$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
|
||||||
@ -171,7 +168,7 @@ class QuestionHelper extends Helper
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function autocomplete(OutputInterface $output, Question $question)
|
private function autocomplete(OutputInterface $output, Question $question, $inputStream)
|
||||||
{
|
{
|
||||||
$autocomplete = $question->getAutocompleterValues();
|
$autocomplete = $question->getAutocompleterValues();
|
||||||
$ret = '';
|
$ret = '';
|
||||||
@ -190,8 +187,8 @@ class QuestionHelper extends Helper
|
|||||||
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
|
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
|
||||||
|
|
||||||
// Read a keypress
|
// Read a keypress
|
||||||
while (!feof($this->inputStream)) {
|
while (!feof($inputStream)) {
|
||||||
$c = fread($this->inputStream, 1);
|
$c = fread($inputStream, 1);
|
||||||
|
|
||||||
// Backspace Character
|
// Backspace Character
|
||||||
if ("\177" === $c) {
|
if ("\177" === $c) {
|
||||||
@ -212,7 +209,7 @@ class QuestionHelper extends Helper
|
|||||||
// Pop the last character off the end of our string
|
// Pop the last character off the end of our string
|
||||||
$ret = substr($ret, 0, $i);
|
$ret = substr($ret, 0, $i);
|
||||||
} elseif ("\033" === $c) { // Did we read an escape sequence?
|
} elseif ("\033" === $c) { // Did we read an escape sequence?
|
||||||
$c .= fread($this->inputStream, 2);
|
$c .= fread($inputStream, 2);
|
||||||
|
|
||||||
// A = Up Arrow. B = Down Arrow
|
// A = Up Arrow. B = Down Arrow
|
||||||
if ('A' === $c[2] || 'B' === $c[2]) {
|
if ('A' === $c[2] || 'B' === $c[2]) {
|
||||||
@ -289,7 +286,7 @@ class QuestionHelper extends Helper
|
|||||||
*
|
*
|
||||||
* @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
|
* @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
|
||||||
*/
|
*/
|
||||||
private function getHiddenResponse(OutputInterface $output)
|
private function getHiddenResponse(OutputInterface $output, $inputStream)
|
||||||
{
|
{
|
||||||
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
||||||
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
|
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
|
||||||
@ -315,7 +312,7 @@ class QuestionHelper extends Helper
|
|||||||
$sttyMode = shell_exec('stty -g');
|
$sttyMode = shell_exec('stty -g');
|
||||||
|
|
||||||
shell_exec('stty -echo');
|
shell_exec('stty -echo');
|
||||||
$value = fgets($this->inputStream, 4096);
|
$value = fgets($inputStream, 4096);
|
||||||
shell_exec(sprintf('stty %s', $sttyMode));
|
shell_exec(sprintf('stty %s', $sttyMode));
|
||||||
|
|
||||||
if (false === $value) {
|
if (false === $value) {
|
||||||
|
Reference in New Issue
Block a user