@@ -23,6 +23,8 @@ class InputOutput
2323 */
2424 private readonly bool $ readlineSupport ;
2525
26+ private bool $ atEnd = false ;
27+
2628 public function __construct ()
2729 {
2830 // Readline is an extension for PHP that makes interactivity with PHP
@@ -51,7 +53,8 @@ public function input(?string $prefix = null): string
5153 self ::fwrite (STDOUT , $ prefix ?? '' );
5254 }
5355
54- $ input = readline ($ prompt );
56+ $ input = readline ($ prompt );
57+ $ this ->atEnd = $ input === false && (! defined ('STDIN ' ) || ! stream_isatty (STDIN ));
5558
5659 return $ input === false ? '' : $ input ;
5760 // @codeCoverageIgnoreEnd
@@ -60,7 +63,9 @@ public function input(?string $prefix = null): string
6063 // self:: skips MockInputOutput's fwrite override, whose filter bookkeeping must not nest inside input().
6164 self ::fwrite (STDOUT , $ prefix ?? '' );
6265
63- $ input = fgets (fopen ('php://stdin ' , 'rb ' ));
66+ $ stdin = fopen ('php://stdin ' , 'rb ' );
67+ $ input = fgets ($ stdin );
68+ $ this ->atEnd = $ input === false && ! stream_isatty ($ stdin );
6469
6570 if ($ input === false ) {
6671 return '' ;
@@ -69,6 +74,16 @@ public function input(?string $prefix = null): string
6974 return $ input ;
7075 }
7176
77+ /**
78+ * Whether the last `input()` found the end of an input other than a terminal,
79+ * so that no later read can bring an answer. At a terminal, `Ctrl+D` ends only
80+ * the current read, and `input()` returns an empty string as for an empty line.
81+ */
82+ public function isAtEnd (): bool
83+ {
84+ return $ this ->atEnd ;
85+ }
86+
7287 /**
7388 * While the library is intended for use on CLI commands,
7489 * commands can be called from controllers and elsewhere
0 commit comments