@@ -64,7 +64,7 @@ $stdio = new Stdio($loop);
64
64
See below for waiting for user input and writing output.
65
65
The ` Stdio ` class is a well-behaving duplex stream
66
66
(implementing ReactPHP's ` DuplexStreamInterface ` ) that emits each complete
67
- line as a ` data ` event ( including the trailing newline) .
67
+ line as a ` data ` event, including the trailing newline.
68
68
69
69
#### Output
70
70
@@ -79,8 +79,12 @@ $stdio->write('hello');
79
79
$stdio->write(" world\n");
80
80
```
81
81
82
- Alternatively, you can also use the ` Stdio ` as a writable stream.
83
- You can ` pipe() ` any readable stream into this stream.
82
+ Because the ` Stdio ` is a well-behaving writable stream,
83
+ you can also ` pipe() ` any readable stream into this stream.
84
+
85
+ ``` php
86
+ $logger->pipe($stdio);
87
+ ```
84
88
85
89
#### Input
86
90
@@ -99,10 +103,34 @@ $stdio->on('data', function ($line) {
99
103
});
100
104
```
101
105
102
- Because the ` Stdio ` is a well-behaving redable stream that will emit incoming
106
+ Note that this class takes care of buffering incomplete lines and will only emit
107
+ complete lines.
108
+ This means that the line will usually end with the trailing newline character.
109
+ If the stream ends without a trailing newline character, it will not be present
110
+ in the ` data ` event.
111
+ As such, it's usually recommended to remove the trailing newline character
112
+ before processing command line input like this:
113
+
114
+ ``` php
115
+ $stdio->on('data', function ($line) {
116
+ $line = rtrim($line, "\r\n");
117
+ if ($line === "start") {
118
+ doSomething();
119
+ }
120
+ });
121
+ ```
122
+
123
+ Similarly, if you copy and paste a larger chunk of text, it will properly emit
124
+ multiple complete lines with a separate ` data ` event for each line.
125
+
126
+ Because the ` Stdio ` is a well-behaving readable stream that will emit incoming
103
127
data as-is, you can also use this to ` pipe() ` this stream into other writable
104
128
streams.
105
129
130
+ ```
131
+ $stdio->pipe($logger);
132
+ ```
133
+
106
134
You can control various aspects of the console input through the [ ` Readline ` ] ( #readline ) ,
107
135
so read on..
108
136
@@ -124,7 +152,7 @@ See above for waiting for user input.
124
152
125
153
Alternatively, the ` Readline ` is also a well-behaving readable stream
126
154
(implementing ReactPHP's ` ReadableStreamInterface ` ) that emits each complete
127
- line as a ` data ` event (without the trailing newline) .
155
+ line as a ` data ` event, including the trailing newline.
128
156
This is considered advanced usage.
129
157
130
158
#### Prompt
0 commit comments