Skip to content

Commit

Permalink
bug #10798 [Console] Fix #10795: Allow instancing Console Application…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Apr 28, 2014
2 parents 23286bc + 3a5a525 commit f4f09d1
Showing 1 changed file with 11 additions and 14 deletions.
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

0 comments on commit f4f09d1

Please sign in to comment.