@@ -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
@@ -145,10 +154,11 @@ $readline = $stdio->getReadline();
145154```
146155
147156See above for waiting for user input.
157+
148158Alternatively, the ` Readline ` is also a well-behaving readable stream
149159(implementing ReactPHP's ` ReadableStreamInterface ` ) that emits each complete
150- line as a ` data ` event (without the trailing newline). This is considered
151- advanced usage.
160+ line as a ` data ` event (without the trailing newline).
161+ This is considered advanced usage.
152162
153163#### Prompt
154164
@@ -303,11 +313,12 @@ If you want to automatically add everything from the user input to the history,
303313you may want to use something like this:
304314
305315``` php
306- $readline->on('data', function ($line) use ($readline) {
316+ $stdio->on('data', function ($line) use ($readline) {
317+ $line = rtrim($line);
307318 $all = $readline->listHistory();
308319
309320 // skip empty line and duplicate of previous line
310- if (trim( $line) !== '' && $line !== end($all)) {
321+ if ($line !== '' && $line !== end($all)) {
311322 $readline->addHistory($line);
312323 }
313324});
@@ -498,14 +509,15 @@ $stdout = $stdio->getOutput();
498509
499510#### Stdin
500511
501- The ` Stdin ` represents a ` ReadableStream ` and is responsible for handling console input.
512+ [ Deprecated ] The ` Stdin ` represents a ` ReadableStream ` and is responsible for handling console input.
502513
503514Interfacing with it directly is * not recommended* and considered * advanced usage* .
504515
505516If you want to read a line from console input, use the [ ` Stdio::on() ` ] ( #input ) instead:
506517
507518``` php
508- $stdio->on('line', function ($line) use ($stdio) {
519+ $stdio->on('data', function ($line) use ($stdio) {
520+ $line = rtrim($line, "\r\n");
509521 $stdio->write('You said "' . $line . '"' . PHP_EOL);
510522});
511523```
@@ -515,6 +527,7 @@ Should you need to interface with the `Stdin`, you can access the current instan
515527You can access the current instance through the [ ` Stdio ` ] ( #stdio ) :
516528
517529``` php
530+ // deprecated
518531$stdin = $stdio->getInput();
519532```
520533
0 commit comments