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

Skip to content

Commit f4f09d1

Browse files
committed
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
2 parents 23286bc + 3a5a525 commit f4f09d1

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class QuestionHelper extends Helper
2828
private static $shell;
2929
private static $stty;
3030

31-
public function __construct()
32-
{
33-
$this->inputStream = STDIN;
34-
}
35-
3631
/**
3732
* Asks a question to the user.
3833
*
@@ -114,6 +109,8 @@ public function getName()
114109
*/
115110
public function doAsk(OutputInterface $output, Question $question)
116111
{
112+
$inputStream = $this->inputStream ?: STDIN;
113+
117114
$message = $question->getQuestion();
118115
if ($question instanceof ChoiceQuestion) {
119116
$width = max(array_map('strlen', array_keys($question->getChoices())));
@@ -135,7 +132,7 @@ public function doAsk(OutputInterface $output, Question $question)
135132
$ret = false;
136133
if ($question->isHidden()) {
137134
try {
138-
$ret = trim($this->getHiddenResponse($output));
135+
$ret = trim($this->getHiddenResponse($output, $inputStream));
139136
} catch (\RuntimeException $e) {
140137
if (!$question->isHiddenFallback()) {
141138
throw $e;
@@ -144,14 +141,14 @@ public function doAsk(OutputInterface $output, Question $question)
144141
}
145142

146143
if (false === $ret) {
147-
$ret = fgets($this->inputStream, 4096);
144+
$ret = fgets($inputStream, 4096);
148145
if (false === $ret) {
149146
throw new \RuntimeException('Aborted');
150147
}
151148
$ret = trim($ret);
152149
}
153150
} else {
154-
$ret = trim($this->autocomplete($output, $question));
151+
$ret = trim($this->autocomplete($output, $question, $inputStream));
155152
}
156153

157154
$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
@@ -171,7 +168,7 @@ public function doAsk(OutputInterface $output, Question $question)
171168
*
172169
* @return string
173170
*/
174-
private function autocomplete(OutputInterface $output, Question $question)
171+
private function autocomplete(OutputInterface $output, Question $question, $inputStream)
175172
{
176173
$autocomplete = $question->getAutocompleterValues();
177174
$ret = '';
@@ -190,8 +187,8 @@ private function autocomplete(OutputInterface $output, Question $question)
190187
$output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
191188

192189
// Read a keypress
193-
while (!feof($this->inputStream)) {
194-
$c = fread($this->inputStream, 1);
190+
while (!feof($inputStream)) {
191+
$c = fread($inputStream, 1);
195192

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

217214
// A = Up Arrow. B = Down Arrow
218215
if ('A' === $c[2] || 'B' === $c[2]) {
@@ -289,7 +286,7 @@ private function autocomplete(OutputInterface $output, Question $question)
289286
*
290287
* @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
291288
*/
292-
private function getHiddenResponse(OutputInterface $output)
289+
private function getHiddenResponse(OutputInterface $output, $inputStream)
293290
{
294291
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
295292
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
@@ -315,7 +312,7 @@ private function getHiddenResponse(OutputInterface $output)
315312
$sttyMode = shell_exec('stty -g');
316313

317314
shell_exec('stty -echo');
318-
$value = fgets($this->inputStream, 4096);
315+
$value = fgets($inputStream, 4096);
319316
shell_exec(sprintf('stty %s', $sttyMode));
320317

321318
if (false === $value) {

0 commit comments

Comments
 (0)