Skip to content

Commit 1cf2db1

Browse files
authored
Merge pull request #62 from clue-labs/deprecate-input
Deprecate input helpers, use Stdio as ReadableStreamInterface instead
2 parents 449b1f6 + 1803cdb commit 1cf2db1

File tree

6 files changed

+56
-36
lines changed

6 files changed

+56
-36
lines changed

README.md

Lines changed: 34 additions & 21 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

@@ -145,10 +154,11 @@ $readline = $stdio->getReadline();
145154
```
146155

147156
See above for waiting for user input.
157+
148158
Alternatively, 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,
303313
you 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

503514
Interfacing with it directly is *not recommended* and considered *advanced usage*.
504515

505516
If 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
515527
You can access the current instance through the [`Stdio`](#stdio):
516528

517529
```php
530+
// deprecated
518531
$stdin = $stdio->getInput();
519532
```
520533

examples/02-interactive.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@
2121
$readline->limitHistory($limit);
2222
}
2323

24-
// add all lines from input to history
25-
$readline->on('data', function ($line) use ($readline) {
26-
$all = $readline->listHistory();
27-
28-
// skip empty line and duplicate of previous line
29-
if (trim($line) !== '' && $line !== end($all)) {
30-
$readline->addHistory($line);
31-
}
32-
});
33-
3424
// autocomplete the following commands (at offset=0/1 only)
3525
$readline->setAutocomplete(function ($_, $offset) {
3626
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
@@ -39,7 +29,16 @@
3929
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4030

4131
// react to commands the user entered
42-
$stdio->on('line', function ($line) use ($stdio) {
32+
$stdio->on('data', function ($line) use ($stdio, $readline) {
33+
$line = rtrim($line, "\r\n");
34+
35+
// add all lines from input to history
36+
// skip empty line and duplicate of previous line
37+
$all = $readline->listHistory();
38+
if ($line !== '' && $line !== end($all)) {
39+
$readline->addHistory($line);
40+
}
41+
4342
$stdio->write('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
4443

4544
if (in_array(trim($line), array('quit', 'exit'))) {

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('*');

src/Stdin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use React\Stream\Stream;
66
use React\EventLoop\LoopInterface;
77

8-
// TODO: only implement ReadableStream
8+
/**
9+
* @deprecated
10+
*/
911
class Stdin extends Stream
1012
{
1113
private $oldMode = null;

src/Stdio.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ public function close()
217217
$this->output->close();
218218
}
219219

220+
/**
221+
* @deprecated
222+
*/
220223
public function getInput()
221224
{
222225
return $this->input;

0 commit comments

Comments
 (0)