Skip to content

Commit a3a0c8b

Browse files
committed
Deprecate Stdio::writeln(), use write() with PHP_EOL instead
1 parent 266108f commit a3a0c8b

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

README.md

Lines changed: 9 additions & 8 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,6 +82,14 @@ $stdio->write('hello');
8982
$stdio->write(" world\n");
9083
```
9184

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+
9293
[Deprecated] The `overwrite($text)` method can be used to overwrite/replace the last
9394
incomplete line with the given text:
9495

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

505506
```php
506507
$stdio->on('line', function ($line) use ($stdio) {
507-
$stdio->writeln('You said "' . $line . '"');
508+
$stdio->write('You said "' . $line . '"' . PHP_EOL);
508509
});
509510
```
510511

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public function write($data)
162162
}
163163
}
164164

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

0 commit comments

Comments
 (0)