Skip to content

[Console] clone stream on multiline questions so EOT doesn't close input #38351

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
Sep 30, 2020
Merged
Show file tree
Hide file tree
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
44 changes: 43 additions & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,53 @@ private function readInput($inputStream, Question $question)
return fgets($inputStream, 4096);
}

$multiLineStreamReader = $this->cloneInputStream($inputStream);
if (null === $multiLineStreamReader) {
return false;
}

$ret = '';
while (false !== ($char = fgetc($inputStream))) {
while (false !== ($char = fgetc($multiLineStreamReader))) {
if (\PHP_EOL === "{$ret}{$char}") {
break;
}
$ret .= $char;
}

return $ret;
}

/**
* Clones an input stream in order to act on one instance of the same
* stream without affecting the other instance.
*
* @param resource $inputStream The handler resource
*
* @return resource|null The cloned resource, null in case it could not be cloned
*/
private function cloneInputStream($inputStream)
{
$streamMetaData = stream_get_meta_data($inputStream);
$seekable = $streamMetaData['seekable'] ?? false;
$mode = $streamMetaData['mode'] ?? 'rb';
$uri = $streamMetaData['uri'] ?? null;

if (null === $uri) {
return null;
}

$cloneStream = fopen($uri, $mode);

// For seekable and writable streams, add all the same data to the
// cloned stream and then seek to the same offset.
if (true === $seekable && !\in_array($mode, ['r', 'rb', 'rt'])) {
$offset = ftell($inputStream);
rewind($inputStream);
stream_copy_to_stream($inputStream, $cloneStream);
fseek($inputStream, $offset);
fseek($cloneStream, $offset);
}

return $cloneStream;
}
}
51 changes: 48 additions & 3 deletions src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,19 +461,64 @@ public function testAskMultilineResponseWithEOF()
$question = new Question('Write an essay');
$question->setMultiline(true);

$this->assertEquals($essay, $dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
$this->assertSame($essay, $dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithSingleNewline()
{
$response = $this->getInputStream("\n");
$response = $this->getInputStream(\PHP_EOL);

$dialog = new QuestionHelper();

$question = new Question('Write an essay');
$question->setMultiline(true);

$this->assertEquals('', $dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
$this->assertNull($dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithDataAfterNewline()
{
$response = $this->getInputStream(\PHP_EOL.'this is text');

$dialog = new QuestionHelper();

$question = new Question('Write an essay');
$question->setMultiline(true);

$this->assertNull($dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithMultipleNewlinesAtEnd()
{
$typedText = 'This is a body'.\PHP_EOL.\PHP_EOL;
$response = $this->getInputStream($typedText);

$dialog = new QuestionHelper();

$question = new Question('Write an essay');
$question->setMultiline(true);

$this->assertSame('This is a body', $dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithWithCursorInMiddleOfSeekableInputStream()
{
$input = <<<EOD
This
is
some
input
EOD;
$response = $this->getInputStream($input);
fseek($response, 8);

$dialog = new QuestionHelper();

$question = new Question('Write an essay');
$question->setMultiline(true);

$this->assertSame("some\ninput", $dialog->ask($this->createStreamableInputInterfaceMock($response), $this->createOutputInterface(), $question));
$this->assertSame(8, ftell($response));
}

/**
Expand Down