Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Fix #10795: Allow instancing Console Application when STDIN is not declared #10798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class QuestionHelper extends Helper
private static $shell;
private static $stty;

public function __construct()
{
$this->inputStream = STDIN;
}

/**
* Asks a question to the user.
*
Expand Down Expand Up @@ -114,6 +109,8 @@ public function getName()
*/
public function doAsk(OutputInterface $output, Question $question)
{
$inputStream = $this->inputStream ?: STDIN;

$message = $question->getQuestion();
if ($question instanceof ChoiceQuestion) {
$width = max(array_map('strlen', array_keys($question->getChoices())));
Expand All @@ -135,7 +132,7 @@ public function doAsk(OutputInterface $output, Question $question)
$ret = false;
if ($question->isHidden()) {
try {
$ret = trim($this->getHiddenResponse($output));
$ret = trim($this->getHiddenResponse($output, $inputStream));
} catch (\RuntimeException $e) {
if (!$question->isHiddenFallback()) {
throw $e;
Expand All @@ -144,14 +141,14 @@ public function doAsk(OutputInterface $output, Question $question)
}

if (false === $ret) {
$ret = fgets($this->inputStream, 4096);
$ret = fgets($inputStream, 4096);
if (false === $ret) {
throw new \RuntimeException('Aborted');
}
$ret = trim($ret);
}
} else {
$ret = trim($this->autocomplete($output, $question));
$ret = trim($this->autocomplete($output, $question, $inputStream));
}

$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
Expand All @@ -171,7 +168,7 @@ public function doAsk(OutputInterface $output, Question $question)
*
* @return string
*/
private function autocomplete(OutputInterface $output, Question $question)
private function autocomplete(OutputInterface $output, Question $question, $inputStream)
{
$autocomplete = $question->getAutocompleterValues();
$ret = '';
Expand All @@ -190,8 +187,8 @@ private function autocomplete(OutputInterface $output, Question $question)
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));

// Read a keypress
while (!feof($this->inputStream)) {
$c = fread($this->inputStream, 1);
while (!feof($inputStream)) {
$c = fread($inputStream, 1);

// Backspace Character
if ("\177" === $c) {
Expand All @@ -212,7 +209,7 @@ private function autocomplete(OutputInterface $output, Question $question)
// Pop the last character off the end of our string
$ret = substr($ret, 0, $i);
} elseif ("\033" === $c) { // Did we read an escape sequence?
$c .= fread($this->inputStream, 2);
$c .= fread($inputStream, 2);

// A = Up Arrow. B = Down Arrow
if ('A' === $c[2] || 'B' === $c[2]) {
Expand Down Expand Up @@ -289,7 +286,7 @@ private function autocomplete(OutputInterface $output, Question $question)
*
* @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')) {
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
Expand All @@ -315,7 +312,7 @@ private function getHiddenResponse(OutputInterface $output)
$sttyMode = shell_exec('stty -g');

shell_exec('stty -echo');
$value = fgets($this->inputStream, 4096);
$value = fgets($inputStream, 4096);
shell_exec(sprintf('stty %s', $sttyMode));

if (false === $value) {
Expand Down