@@ -36,9 +36,10 @@ $stdio = new Stdio($loop);
36
36
37
37
$stdio->getReadline()->setPrompt('Input > ');
38
38
39
- $stdio->on('line', function ($line) use ($stdio) {
39
+ $stdio->on('data', function ($line) use ($stdio) {
40
+ $line = rtrim($line, "\r\n");
40
41
var_dump($line);
41
-
42
+
42
43
if ($line === 'quit') {
43
44
$stdio->end();
44
45
}
@@ -64,10 +65,9 @@ $stdio = new Stdio($loop);
64
65
```
65
66
66
67
See 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
68
69
(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).
71
71
72
72
#### Output
73
73
@@ -107,28 +107,37 @@ You can `pipe()` any readable stream into this stream.
107
107
The ` Stdio ` is a well-behaving readable stream
108
108
implementing ReactPHP's ` ReadableStreamInterface ` .
109
109
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.
112
112
You can register any number of event handlers like this:
113
113
114
114
``` php
115
- $stdio->on('line ', function ($line) {
116
- if ($line === ' start' ) {
115
+ $stdio->on('data ', function ($line) {
116
+ if ($line === " start\n" ) {
117
117
doSomething();
118
118
}
119
119
});
120
120
```
121
121
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
+
122
126
You can control various aspects of the console input through the [ ` Readline ` ] ( #readline ) ,
123
127
so read on..
124
128
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:
128
132
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
+ ```
132
141
133
142
### Readline
134
143
@@ -145,10 +154,11 @@ $readline = $stdio->getReadline();
145
154
```
146
155
147
156
See above for waiting for user input.
157
+
148
158
Alternatively, the ` Readline ` is also a well-behaving readable stream
149
159
(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.
152
162
153
163
#### Prompt
154
164
@@ -303,11 +313,12 @@ If you want to automatically add everything from the user input to the history,
303
313
you may want to use something like this:
304
314
305
315
``` php
306
- $readline->on('data', function ($line) use ($readline) {
316
+ $stdio->on('data', function ($line) use ($readline) {
317
+ $line = rtrim($line);
307
318
$all = $readline->listHistory();
308
319
309
320
// skip empty line and duplicate of previous line
310
- if (trim( $line) !== '' && $line !== end($all)) {
321
+ if ($line !== '' && $line !== end($all)) {
311
322
$readline->addHistory($line);
312
323
}
313
324
});
@@ -498,14 +509,15 @@ $stdout = $stdio->getOutput();
498
509
499
510
#### Stdin
500
511
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.
502
513
503
514
Interfacing with it directly is * not recommended* and considered * advanced usage* .
504
515
505
516
If you want to read a line from console input, use the [ ` Stdio::on() ` ] ( #input ) instead:
506
517
507
518
``` php
508
- $stdio->on('line', function ($line) use ($stdio) {
519
+ $stdio->on('data', function ($line) use ($stdio) {
520
+ $line = rtrim($line, "\r\n");
509
521
$stdio->write('You said "' . $line . '"' . PHP_EOL);
510
522
});
511
523
```
@@ -515,6 +527,7 @@ Should you need to interface with the `Stdin`, you can access the current instan
515
527
You can access the current instance through the [ ` Stdio ` ] ( #stdio ) :
516
528
517
529
``` php
530
+ // deprecated
518
531
$stdin = $stdio->getInput();
519
532
```
520
533
0 commit comments