Skip to content

Commit a75475a

Browse files
committed
Deprecated Stdio "line" event, use "data" event instead
1 parent fdd9e72 commit a75475a

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6667
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
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.
107107
The `Stdio` is a well-behaving readable stream
108108
implementing 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.
112112
You 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+
122126
You can control various aspects of the console input through the [`Readline`](#readline),
123127
so 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
505514
If 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
```

examples/02-interactive.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4040

4141
// react to commands the user entered
42-
$stdio->on('line', function ($line) use ($stdio) {
43-
$stdio->write('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
42+
$stdio->on('data', function ($line) use ($stdio) {
43+
$stdio->write('you just said: ' . addcslashes($line, "\0..\037") . ' (' . strlen($line) . ')' . PHP_EOL);
4444

4545
if (in_array(trim($line), array('quit', 'exit'))) {
4646
$stdio->end();

examples/03-commander.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@
4646
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4747

4848
// react to commands the user entered
49-
$stdio->on('line', function ($line) use ($router, $stdio, $readline) {
49+
$stdio->on('data', function ($line) use ($router, $stdio, $readline) {
50+
$line = rtrim($line, "\r\n");
51+
5052
// add all lines from input to history
5153
// skip empty line and duplicate of previous line
5254
$all = $readline->listHistory();
53-
if (trim($line) !== '' && $line !== end($all)) {
55+
if ($line !== '' && $line !== end($all)) {
5456
$readline->addHistory($line);
5557
}
5658

examples/11-login.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
$username = null;
1515
$password = null;
1616

17-
$stdio->on('line', function ($line) use ($stdio, &$first, &$username, &$password) {
17+
$stdio->on('data', function ($line) use ($stdio, &$first, &$username, &$password) {
18+
$line = rtrim($line, "\r\n");
1819
if ($first) {
1920
$stdio->getReadline()->setPrompt('Password: ');
2021
$stdio->getReadline()->setEcho('*');

0 commit comments

Comments
 (0)