@@ -36,9 +36,10 @@ $stdio = new Stdio($loop);
3636
3737$stdio->getReadline()->setPrompt('Input > ');
3838
39- $stdio->on('line', function ($line) use ($stdio) {
39+ $stdio->on('data', function ($line) use ($stdio) {
40+ $line = rtrim($line, "\r\n");
4041 var_dump($line);
41-
42+
4243 if ($line === 'quit') {
4344 $stdio->end();
4445 }
@@ -64,10 +65,9 @@ $stdio = new Stdio($loop);
6465```
6566
6667See below for waiting for user input and writing output.
67- Alternatively, the ` Stdio ` is also a well-behaving duplex stream
68+ The ` Stdio ` class is a well-behaving duplex stream
6869(implementing ReactPHP's ` DuplexStreamInterface ` ) that emits each complete
69- line as a ` data ` event (including the trailing newline). This is considered
70- advanced usage.
70+ line as a ` data ` event (including the trailing newline).
7171
7272#### Output
7373
@@ -107,28 +107,37 @@ You can `pipe()` any readable stream into this stream.
107107The ` Stdio ` is a well-behaving readable stream
108108implementing ReactPHP's ` ReadableStreamInterface ` .
109109
110- It will emit a ` line ` event for every line read from console input.
111- The event will contain the input buffer as-is, without the trailing newline.
110+ It will emit a ` data ` event for every line read from console input.
111+ The event will contain the input buffer as-is, including the trailing newline.
112112You can register any number of event handlers like this:
113113
114114``` php
115- $stdio->on('line ', function ($line) {
116- if ($line === ' start' ) {
115+ $stdio->on('data ', function ($line) {
116+ if ($line === " start\n" ) {
117117 doSomething();
118118 }
119119});
120120```
121121
122+ Because the ` Stdio ` is a well-behaving redable stream that will emit incoming
123+ data as-is, you can also use this to ` pipe() ` this stream into other writable
124+ streams.
125+
122126You can control various aspects of the console input through the [ ` Readline ` ] ( #readline ) ,
123127so read on..
124128
125- Using the ` line ` event is the recommended way to wait for user input.
126- Alternatively, using the ` Readline ` as a readable stream is considered advanced
127- usage.
129+ [ Deprecated ] It will emit a ` line ` event for every line read from console input.
130+ The event will contain the input buffer as-is, without the trailing newline.
131+ You can register any number of event handlers like this:
128132
129- Alternatively, you can also use the ` Stdio ` as a readable stream, which emits
130- each complete line as a ` data ` event (including the trailing newline).
131- This can be used to ` pipe() ` this stream into other writable streams.
133+ ``` php
134+ // deprecated
135+ $stdio->on('line', function ($line) {
136+ if ($line === 'start') {
137+ doSomething();
138+ }
139+ });
140+ ```
132141
133142### Readline
134143
@@ -505,7 +514,8 @@ Interfacing with it directly is *not recommended* and considered *advanced usage
505514If you want to read a line from console input, use the [ ` Stdio::on() ` ] ( #input ) instead:
506515
507516``` php
508- $stdio->on('line', function ($line) use ($stdio) {
517+ $stdio->on('data', function ($line) use ($stdio) {
518+ $line = rtrim($line, "\r\n");
509519 $stdio->write('You said "' . $line . '"' . PHP_EOL);
510520});
511521```
0 commit comments