Skip to content

Commit

Permalink
Merge pull request #59 from clue-labs/deprecate-output
Browse files Browse the repository at this point in the history
Deprecate output helpers, use `write()` instead
  • Loading branch information
clue authored Nov 2, 2017
2 parents f574375 + fdd9e72 commit a969286
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ advanced usage.
The `Stdio` is a well-behaving writable stream
implementing ReactPHP's `WritableStreamInterface`.

The `writeln($line)` method can be used to print a line to console output.
A trailing newline will be added automatically.

```php
$stdio->writeln('hello world');
```

The `write($text)` method can be used to print the given text characters to console output.
This is useful if you need more control or want to output individual bytes or binary output:

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

The `overwrite($text)` method can be used to overwrite/replace the last
[Deprecated] The `writeln($line)` method can be used to print a line to console output.
A trailing newline will be added automatically.

```php
// deprecated
$stdio->writeln('hello world');
```

[Deprecated] The `overwrite($text)` method can be used to overwrite/replace the last
incomplete line with the given text:

```php
$stdio->write('Loading…');
// deprecated
$stdio->overwrite('Done!');
```

Expand Down Expand Up @@ -477,7 +479,7 @@ $readline->setAutocomplete(null);

#### Stdout

The `Stdout` represents a `WritableStream` and is responsible for handling console output.
[Deprecated] The `Stdout` represents a `WritableStream` and is responsible for handling console output.

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

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

```php
// deprecated
$stdout = $stdio->getOutput();
```

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

```php
$stdio->on('line', function ($line) use ($stdio) {
$stdio->writeln('You said "' . $line . '"');
$stdio->write('You said "' . $line . '"' . PHP_EOL);
});
```

Expand Down
6 changes: 3 additions & 3 deletions examples/01-periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

$stdio = new Stdio($loop);

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

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

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

$loop->cancelTimer($timer);
$stdio->end();
Expand Down
4 changes: 2 additions & 2 deletions examples/02-interactive.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
});

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

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

if (in_array(trim($line), array('quit', 'exit'))) {
$stdio->end();
Expand Down
12 changes: 6 additions & 6 deletions examples/03-commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
$stdio->end();
});
$router->add('help', function () use ($stdio) {
$stdio->writeln('Use TAB-completion or use "exit"');
$stdio->write('Use TAB-completion or use "exit"' . PHP_EOL);
});
$router->add('(echo | print) <words>...', function (array $args) use ($stdio) {
$stdio->writeln(implode(' ', $args['words']));
$stdio->write(implode(' ', $args['words']) . PHP_EOL);
});
$router->add('printf <format> <args>...', function (array $args) use ($stdio) {
$stdio->writeln(vsprintf($args['format'],$args['args']));
$stdio->write(vsprintf($args['format'],$args['args']) . PHP_EOL);
});

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

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

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

Expand All @@ -69,7 +69,7 @@
try {
$router->handleArgs($args);
} catch (NoRouteFoundException $e) {
$stdio->writeln('Error: Invalid command usage');
$stdio->write('Error: Invalid command usage' . PHP_EOL);
}
});

Expand Down
9 changes: 9 additions & 0 deletions src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ public function write($data)
}
}

/**
* @deprecated
*/
public function writeln($line)
{
$this->write($line . PHP_EOL);
}

/**
* @deprecated
*/
public function overwrite($data = '')
{
if ($this->incompleteLine !== '') {
Expand Down Expand Up @@ -216,6 +222,9 @@ public function getInput()
return $this->input;
}

/**
* @deprecated
*/
public function getOutput()
{
return $this->output;
Expand Down
3 changes: 3 additions & 0 deletions src/Stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use React\Stream\WritableStream;

/**
* @deprecated
*/
class Stdout extends WritableStream
{
public function __construct()
Expand Down

0 comments on commit a969286

Please sign in to comment.