Skip to content

Commit a969286

Browse files
authored
Merge pull request #59 from clue-labs/deprecate-output
Deprecate output helpers, use `write()` instead
2 parents f574375 + fdd9e72 commit a969286

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ advanced usage.
7474
The `Stdio` is a well-behaving writable stream
7575
implementing ReactPHP's `WritableStreamInterface`.
7676

77-
The `writeln($line)` method can be used to print a line to console output.
78-
A trailing newline will be added automatically.
79-
80-
```php
81-
$stdio->writeln('hello world');
82-
```
83-
8477
The `write($text)` method can be used to print the given text characters to console output.
8578
This is useful if you need more control or want to output individual bytes or binary output:
8679

@@ -89,11 +82,20 @@ $stdio->write('hello');
8982
$stdio->write(" world\n");
9083
```
9184

92-
The `overwrite($text)` method can be used to overwrite/replace the last
85+
[Deprecated] The `writeln($line)` method can be used to print a line to console output.
86+
A trailing newline will be added automatically.
87+
88+
```php
89+
// deprecated
90+
$stdio->writeln('hello world');
91+
```
92+
93+
[Deprecated] The `overwrite($text)` method can be used to overwrite/replace the last
9394
incomplete line with the given text:
9495

9596
```php
9697
$stdio->write('Loading…');
98+
// deprecated
9799
$stdio->overwrite('Done!');
98100
```
99101

@@ -477,7 +479,7 @@ $readline->setAutocomplete(null);
477479

478480
#### Stdout
479481

480-
The `Stdout` represents a `WritableStream` and is responsible for handling console output.
482+
[Deprecated] The `Stdout` represents a `WritableStream` and is responsible for handling console output.
481483

482484
Interfacing with it directly is *not recommended* and considered *advanced usage*.
483485

@@ -490,6 +492,7 @@ $stdio->write('hello');
490492
Should you need to interface with the `Stdout`, you can access the current instance through the [`Stdio`](#stdio):
491493

492494
```php
495+
// deprecated
493496
$stdout = $stdio->getOutput();
494497
```
495498

@@ -503,7 +506,7 @@ If you want to read a line from console input, use the [`Stdio::on()`](#input) i
503506

504507
```php
505508
$stdio->on('line', function ($line) use ($stdio) {
506-
$stdio->writeln('You said "' . $line . '"');
509+
$stdio->write('You said "' . $line . '"' . PHP_EOL);
507510
});
508511
```
509512

examples/01-periodic.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
$stdio = new Stdio($loop);
1010

11-
$stdio->writeln('Will print periodic messages until you submit anything');
11+
$stdio->write('Will print periodic messages until you submit anything' . PHP_EOL);
1212

1313
// add some periodic noise
1414
$timer = $loop->addPeriodicTimer(0.5, function () use ($stdio) {
15-
$stdio->writeln(date('Y-m-d H:i:s') . ' hello');
15+
$stdio->write(date('Y-m-d H:i:s') . ' hello' . PHP_EOL);
1616
});
1717

1818
// react to commands the user entered
1919
$stdio->on('data', function ($line) use ($stdio, $loop, $timer) {
20-
$stdio->writeln('you just said: ' . addcslashes($line, "\0..\37") . ' (' . strlen($line) . ')');
20+
$stdio->write('you just said: ' . addcslashes($line, "\0..\37") . ' (' . strlen($line) . ')' . PHP_EOL);
2121

2222
$loop->cancelTimer($timer);
2323
$stdio->end();

examples/02-interactive.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
3737
});
3838

39-
$stdio->writeln('Welcome to this interactive demo');
39+
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4040

4141
// react to commands the user entered
4242
$stdio->on('line', function ($line) use ($stdio) {
43-
$stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
43+
$stdio->write('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
4444

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

examples/03-commander.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@
2929
$stdio->end();
3030
});
3131
$router->add('help', function () use ($stdio) {
32-
$stdio->writeln('Use TAB-completion or use "exit"');
32+
$stdio->write('Use TAB-completion or use "exit"' . PHP_EOL);
3333
});
3434
$router->add('(echo | print) <words>...', function (array $args) use ($stdio) {
35-
$stdio->writeln(implode(' ', $args['words']));
35+
$stdio->write(implode(' ', $args['words']) . PHP_EOL);
3636
});
3737
$router->add('printf <format> <args>...', function (array $args) use ($stdio) {
38-
$stdio->writeln(vsprintf($args['format'],$args['args']));
38+
$stdio->write(vsprintf($args['format'],$args['args']) . PHP_EOL);
3939
});
4040

4141
// autocomplete the following commands (at offset=0/1 only)
4242
$readline->setAutocomplete(function ($_, $offset) {
4343
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
4444
});
4545

46-
$stdio->writeln('Welcome to this interactive demo');
46+
$stdio->write('Welcome to this interactive demo' . PHP_EOL);
4747

4848
// react to commands the user entered
4949
$stdio->on('line', function ($line) use ($router, $stdio, $readline) {
@@ -57,7 +57,7 @@
5757
try {
5858
$args = Arguments\split($line);
5959
} catch (Arguments\UnclosedQuotesException $e) {
60-
$stdio->writeln('Error: Invalid command syntax (unclosed quotes)');
60+
$stdio->write('Error: Invalid command syntax (unclosed quotes)' . PHP_EOL);
6161
return;
6262
}
6363

@@ -69,7 +69,7 @@
6969
try {
7070
$router->handleArgs($args);
7171
} catch (NoRouteFoundException $e) {
72-
$stdio->writeln('Error: Invalid command usage');
72+
$stdio->write('Error: Invalid command usage' . PHP_EOL);
7373
}
7474
});
7575

src/Stdio.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,17 @@ public function write($data)
162162
}
163163
}
164164

165+
/**
166+
* @deprecated
167+
*/
165168
public function writeln($line)
166169
{
167170
$this->write($line . PHP_EOL);
168171
}
169172

173+
/**
174+
* @deprecated
175+
*/
170176
public function overwrite($data = '')
171177
{
172178
if ($this->incompleteLine !== '') {
@@ -216,6 +222,9 @@ public function getInput()
216222
return $this->input;
217223
}
218224

225+
/**
226+
* @deprecated
227+
*/
219228
public function getOutput()
220229
{
221230
return $this->output;

src/Stdout.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use React\Stream\WritableStream;
66

7+
/**
8+
* @deprecated
9+
*/
710
class Stdout extends WritableStream
811
{
912
public function __construct()

0 commit comments

Comments
 (0)