Skip to content

Commit

Permalink
minor #34012 Replace STDIN by php://stdin (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

Replace STDIN by php://stdin

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

`STDIN` is SAPI-dependent.

Commits
-------

365d02b Replace STDIN by php://stdin
  • Loading branch information
chalasr committed Oct 17, 2019
2 parents c4b39b7 + 365d02b commit b9ba0c5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 37 deletions.
14 changes: 2 additions & 12 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$showDeprecations = $input->getOption('show-deprecations');

if (['-'] === $filenames) {
return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]);
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), uniqid('sf_', true))]);
}

if (!$filenames) {
// @deprecated to be removed in 5.0
if (0 === ftell(STDIN)) {
@trigger_error('Piping content from STDIN to the "lint:twig" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED);

return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]);
return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), uniqid('sf_', true))]);
}

$loader = $this->twig->getLoader();
Expand Down Expand Up @@ -132,16 +132,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $this->display($input, $output, $io, $filesInfo);
}

private function getStdin(): string
{
$template = '';
while (!feof(STDIN)) {
$template .= fread(STDIN, 1024);
}

return $template;
}

private function getFilesInfo(array $filenames): array
{
$filesInfo = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function doAsk(OutputInterface $output, Question $question)
{
$this->writePrompt($output, $question);

$inputStream = $this->inputStream ?: STDIN;
$inputStream = $this->inputStream ?: fopen('php://stdin', 'r');
$autocomplete = $question->getAutocompleterCallback();

if (null === $autocomplete || !Terminal::hasSttyAvailable()) {
Expand Down
14 changes: 2 additions & 12 deletions src/Symfony/Component/Translation/Command/XliffLintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->displayCorrectFiles = $output->isVerbose();

if (['-'] === $filenames) {
return $this->display($io, [$this->validate($this->getStdin())]);
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
}

// @deprecated to be removed in 5.0
Expand All @@ -96,7 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

@trigger_error('Piping content from STDIN to the "lint:xliff" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED);

return $this->display($io, [$this->validate($this->getStdin())]);
return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
}

$filesInfo = [];
Expand Down Expand Up @@ -230,16 +230,6 @@ private function getFiles(string $fileOrDirectory)
}
}

private function getStdin(): string
{
$xliff = '';
while (!feof(STDIN)) {
$xliff .= fread(STDIN, 1024);
}

return $xliff;
}

private function getDirectoryIterator(string $directory)
{
$default = function ($directory) {
Expand Down
14 changes: 2 additions & 12 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$flags = $input->getOption('parse-tags') ? Yaml::PARSE_CUSTOM_TAGS : 0;

if (['-'] === $filenames) {
return $this->display($io, [$this->validate($this->getStdin(), $flags)]);
return $this->display($io, [$this->validate(file_get_contents('php://stdin'), $flags)]);
}

// @deprecated to be removed in 5.0
if (!$filenames) {
if (0 === ftell(STDIN)) {
@trigger_error('Piping content from STDIN to the "lint:yaml" command without passing the dash symbol "-" as argument is deprecated since Symfony 4.4.', E_USER_DEPRECATED);

return $this->display($io, [$this->validate($this->getStdin(), $flags)]);
return $this->display($io, [$this->validate(file_get_contents('php://stdin'), $flags)]);
}

throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
Expand Down Expand Up @@ -215,16 +215,6 @@ private function getFiles(string $fileOrDirectory): iterable
}
}

private function getStdin(): string
{
$yaml = '';
while (!feof(STDIN)) {
$yaml .= fread(STDIN, 1024);
}

return $yaml;
}

private function getParser(): Parser
{
if (!$this->parser) {
Expand Down

0 comments on commit b9ba0c5

Please sign in to comment.