-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Open
Description
Description
Using pipes and redirection in terminal allows to create more flexible CLI tools. Symfony doesn't offer a standard way to deal with this.
Example
Here is a simple implementation that does most of the job.
function readStdin(InputInterface $input): string {
$stdin = $input instanceof StreamableInputInterface ? $input->getStream() : null;
$stdin ??= fopen('php://stdin', 'r');
$reads = [$stdin];
$writes = [];
$excepts = [];
if (!stream_select($reads, $writes, $excepts, seconds: 0)) {
return '';
}
return stream_get_contents($stdin) ?: '';
}
This implementation is impossible to test correctly under Symfony standard facilities: stdin will block if it is empty, a memory based stream is not even selectable.
dsentker