Skip to content

Commit 925d5ba

Browse files
authored
Merge pull request #99 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 51e40f5 + f57e547 commit 925d5ba

File tree

10 files changed

+50
-59
lines changed

10 files changed

+50
-59
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ Let's take these projects to the next level together! 🚀
5151
Once [installed](#install), you can use the following code to present a prompt in a CLI program:
5252

5353
```php
54-
$loop = React\EventLoop\Factory::create();
55-
$stdio = new Stdio($loop);
54+
$stdio = new Stdio();
5655

5756
$stdio->setPrompt('Input > ');
5857

@@ -64,8 +63,6 @@ $stdio->on('data', function ($line) use ($stdio) {
6463
$stdio->end();
6564
}
6665
});
67-
68-
$loop->run();
6966
```
7067

7168
See also the [examples](examples).
@@ -77,13 +74,17 @@ See also the [examples](examples).
7774
The `Stdio` is the main interface to this library.
7875
It is responsible for orchestrating the input and output streams
7976
by registering and forwarding the corresponding events.
80-
It also registers everything with the main [EventLoop](https://github.com/reactphp/event-loop#usage).
8177

8278
```php
83-
$loop = React\EventLoop\Factory::create();
84-
$stdio = new Stdio($loop);
79+
$stdio = new Stdio();
8580
```
8681

82+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
83+
pass the event loop instance to use for this object. You can use a `null` value
84+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
85+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
86+
given event loop instance.
87+
8788
See below for waiting for user input and writing output.
8889
The `Stdio` class is a well-behaving duplex stream
8990
(implementing ReactPHP's `DuplexStreamInterface`) that emits each complete

composer.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
"php": ">=5.3",
1515
"clue/term-react": "^1.0 || ^0.1.1",
1616
"clue/utf8-react": "^1.0 || ^0.1",
17-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
18-
"react/stream": "^1.0 || ^0.7 || ^0.6"
17+
"react/event-loop": "^1.2",
18+
"react/stream": "^1.2"
19+
},
20+
"require-dev": {
21+
"clue/arguments": "^2.0",
22+
"clue/commander": "^1.2",
23+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
1924
},
2025
"suggest": {
2126
"ext-mbstring": "Using ext-mbstring should provide slightly better performance for handling I/O"
2227
},
28+
"config": {
29+
"sort-packages": true
30+
},
2331
"autoload": {
2432
"psr-4": { "Clue\\React\\Stdio\\": "src/" }
2533
},
2634
"autoload-dev": {
2735
"psr-4": { "Clue\\Tests\\React\\Stdio\\": "tests/" }
28-
},
29-
"require-dev": {
30-
"clue/arguments": "^2.0",
31-
"clue/commander": "^1.2",
32-
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
33-
},
34-
"config": {
35-
"sort-packages": true
3636
}
3737
}

examples/01-periodic.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
<?php
22

33
use Clue\React\Stdio\Stdio;
4+
use React\EventLoop\Loop;
45

56
require __DIR__ . '/../vendor/autoload.php';
67

7-
$loop = React\EventLoop\Factory::create();
8-
9-
$stdio = new Stdio($loop);
8+
$stdio = new Stdio();
109

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

1312
// add some periodic noise
14-
$timer = $loop->addPeriodicTimer(0.5, function () use ($stdio) {
13+
$timer = Loop::addPeriodicTimer(0.5, function () use ($stdio) {
1514
$stdio->write(date('Y-m-d H:i:s') . ' hello' . PHP_EOL);
1615
});
1716

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

22-
$loop->cancelTimer($timer);
21+
Loop::cancelTimer($timer);
2322
$stdio->end();
2423
});
2524

2625
// cancel periodic timer if STDIN closed
27-
$stdio->on('end', function () use ($stdio, $loop, $timer) {
28-
$loop->cancelTimer($timer);
26+
$stdio->on('end', function () use ($stdio, $timer) {
27+
Loop::cancelTimer($timer);
2928
$stdio->end();
3029
});
3130

3231
// input already closed on program start, exit immediately
3332
if (!$stdio->isReadable()) {
34-
$loop->cancelTimer($timer);
33+
Loop::cancelTimer($timer);
3534
$stdio->end();
3635
}
37-
38-
$loop->run();

examples/02-interactive.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$loop = React\EventLoop\Factory::create();
8-
9-
$stdio = new Stdio($loop);
7+
$stdio = new Stdio();
108

119
$stdio->setPrompt('> ');
1210

@@ -44,5 +42,3 @@
4442
$stdio->end();
4543
}
4644
});
47-
48-
$loop->run();

examples/03-commander.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
require __DIR__ . '/../vendor/autoload.php';
99

10-
$loop = React\EventLoop\Factory::create();
11-
12-
$stdio = new Stdio($loop);
10+
$stdio = new Stdio();
1311
$stdio->setPrompt('> ');
1412

1513
// limit history to HISTSIZE env
@@ -73,5 +71,3 @@
7371
$stdio->write('Error: Invalid command usage' . PHP_EOL);
7472
}
7573
});
76-
77-
$loop->run();

examples/04-bindings.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$loop = React\EventLoop\Factory::create();
8-
9-
$stdio = new Stdio($loop);
7+
$stdio = new Stdio();
108
$stdio->setPrompt('> ');
119

1210
// add some special key bindings
@@ -40,5 +38,3 @@
4038
$line = rtrim($line, "\r\n");
4139
$stdio->end('you just said: ' . $line . ' (' . strlen($line) . ')' . PHP_EOL);
4240
});
43-
44-
$loop->run();

examples/05-cursor.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$loop = React\EventLoop\Factory::create();
8-
9-
$stdio = new Stdio($loop);
7+
$stdio = new Stdio();
108

119
$value = 10;
1210
$stdio->on("\033[A", function () use (&$value, $stdio) {
@@ -40,5 +38,3 @@
4038
4139
Use "q" to quit
4240
');
43-
44-
$loop->run();

examples/11-login.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$loop = React\EventLoop\Factory::create();
8-
9-
$stdio = new Stdio($loop);
7+
$stdio = new Stdio();
108
$stdio->setPrompt('Username: ');
119

1210
$first = true;
@@ -33,5 +31,3 @@
3331
);
3432
}
3533
});
36-
37-
$loop->run();

src/Stdio.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,20 @@ class Stdio extends EventEmitter implements DuplexStreamInterface
2222
private $incompleteLine = '';
2323
private $originalTtyMode = null;
2424

25-
public function __construct(LoopInterface $loop, ReadableStreamInterface $input = null, WritableStreamInterface $output = null, Readline $readline = null)
25+
/**
26+
*
27+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
28+
* pass the event loop instance to use for this object. You can use a `null` value
29+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
30+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
31+
* given event loop instance.
32+
*
33+
* @param ?LoopInterface $loop
34+
* @param ?ReadableStreamInterface $input
35+
* @param ?WritableStreamInterface $output
36+
* @param ?Readline $readline
37+
*/
38+
public function __construct(LoopInterface $loop = null, ReadableStreamInterface $input = null, WritableStreamInterface $output = null, Readline $readline = null)
2639
{
2740
if ($input === null) {
2841
$input = $this->createStdin($loop); // @codeCoverageIgnore
@@ -529,11 +542,11 @@ private function restoreTtyMode()
529542
}
530543

531544
/**
532-
* @param LoopInterface $loop
545+
* @param ?LoopInterface $loop
533546
* @return ReadableStreamInterface
534547
* @codeCoverageIgnore this is covered by functional tests with/without ext-readline
535548
*/
536-
private function createStdin(LoopInterface $loop)
549+
private function createStdin(LoopInterface $loop = null)
537550
{
538551
// STDIN not defined ("php -a") or already closed (`fclose(STDIN)`)
539552
// also support starting program with closed STDIN ("example.php 0<&-")
@@ -569,11 +582,11 @@ private function createStdin(LoopInterface $loop)
569582
}
570583

571584
/**
572-
* @param LoopInterface $loop
585+
* @param ?LoopInterface $loop
573586
* @return WritableStreamInterface
574587
* @codeCoverageIgnore this is covered by functional tests
575588
*/
576-
private function createStdout(LoopInterface $loop)
589+
private function createStdout(LoopInterface $loop = null)
577590
{
578591
// STDOUT not defined ("php -a") or already closed (`fclose(STDOUT)`)
579592
// also support starting program with closed STDOUT ("example.php >&-")

tests/StdioTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUpLoop()
2424
*/
2525
public function testCtorDefaultArgs()
2626
{
27-
$stdio = new Stdio($this->loop);
27+
$stdio = new Stdio();
2828

2929
// Closing STDIN/STDOUT is not a good idea for reproducible tests
3030
// $stdio->close();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy