Skip to content

Commit 9f7c59c

Browse files
authored
Merge pull request #159 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 2859a8c + e617d63 commit 9f7c59c

14 files changed

+116
-61
lines changed

README.md

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Re-attach the data source after a previous `pause()`.
303303
```php
304304
$stream->pause();
305305

306-
$loop->addTimer(1.0, function () use ($stream) {
306+
Loop::addTimer(1.0, function () use ($stream) {
307307
$stream->resume();
308308
});
309309
```
@@ -737,7 +737,7 @@ stream in order to stop waiting for the stream to flush its final data.
737737

738738
```php
739739
$stream->end();
740-
$loop->addTimer(1.0, function () use ($stream) {
740+
Loop::addTimer(1.0, function () use ($stream) {
741741
$stream->close();
742742
});
743743
```
@@ -821,7 +821,7 @@ This can be used to represent a read-only resource like a file stream opened in
821821
readable mode or a stream such as `STDIN`:
822822

823823
```php
824-
$stream = new ReadableResourceStream(STDIN, $loop);
824+
$stream = new ReadableResourceStream(STDIN);
825825
$stream->on('data', function ($chunk) {
826826
echo $chunk;
827827
});
@@ -838,7 +838,7 @@ Otherwise, it will throw an `InvalidArgumentException`:
838838

839839
```php
840840
// throws InvalidArgumentException
841-
$stream = new ReadableResourceStream(false, $loop);
841+
$stream = new ReadableResourceStream(false);
842842
```
843843

844844
See also the [`DuplexResourceStream`](#readableresourcestream) for read-and-write
@@ -851,14 +851,20 @@ If this fails, it will throw a `RuntimeException`:
851851

852852
```php
853853
// throws RuntimeException on Windows
854-
$stream = new ReadableResourceStream(STDIN, $loop);
854+
$stream = new ReadableResourceStream(STDIN);
855855
```
856856

857857
Once the constructor is called with a valid stream resource, this class will
858858
take care of the underlying stream resource.
859859
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
860860
stream resource manually.
861861

862+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
863+
pass the event loop instance to use for this object. You can use a `null` value
864+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
865+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
866+
given event loop instance.
867+
862868
This class takes an optional `int|null $readChunkSize` parameter that controls
863869
the maximum buffer size in bytes to read at once from the stream.
864870
You can use a `null` value here in order to apply its default value.
@@ -874,7 +880,7 @@ This should read until the stream resource is not readable anymore
874880
mean it reached EOF.
875881

876882
```php
877-
$stream = new ReadableResourceStream(STDIN, $loop, 8192);
883+
$stream = new ReadableResourceStream(STDIN, null, 8192);
878884
```
879885

880886
> PHP bug warning: If the PHP process has explicitly been started without a
@@ -883,6 +889,9 @@ $stream = new ReadableResourceStream(STDIN, $loop, 8192);
883889
stream like `php test.php < /dev/null` instead of `php test.php <&-`.
884890
See [#81](https://github.com/reactphp/stream/issues/81) for more details.
885891

892+
> Changelog: As of v1.2.0 the `$loop` parameter can be omitted (or skipped with a
893+
`null` value) to use the [default loop](https://github.com/reactphp/event-loop#loop).
894+
886895
### WritableResourceStream
887896

888897
The `WritableResourceStream` is a concrete implementation of the
@@ -892,7 +901,7 @@ This can be used to represent a write-only resource like a file stream opened in
892901
writable mode or a stream such as `STDOUT` or `STDERR`:
893902

894903
```php
895-
$stream = new WritableResourceStream(STDOUT, $loop);
904+
$stream = new WritableResourceStream(STDOUT);
896905
$stream->write('hello!');
897906
$stream->end();
898907
```
@@ -905,7 +914,7 @@ Otherwise, it will throw an `InvalidArgumentException`:
905914

906915
```php
907916
// throws InvalidArgumentException
908-
$stream = new WritableResourceStream(false, $loop);
917+
$stream = new WritableResourceStream(false);
909918
```
910919

911920
See also the [`DuplexResourceStream`](#readableresourcestream) for read-and-write
@@ -918,7 +927,7 @@ If this fails, it will throw a `RuntimeException`:
918927

919928
```php
920929
// throws RuntimeException on Windows
921-
$stream = new WritableResourceStream(STDOUT, $loop);
930+
$stream = new WritableResourceStream(STDOUT);
922931
```
923932

924933
Once the constructor is called with a valid stream resource, this class will
@@ -933,13 +942,19 @@ For this, it uses an in-memory buffer string to collect all outstanding writes.
933942
This buffer has a soft-limit applied which defines how much data it is willing
934943
to accept before the caller SHOULD stop sending further data.
935944

945+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
946+
pass the event loop instance to use for this object. You can use a `null` value
947+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
948+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
949+
given event loop instance.
950+
936951
This class takes an optional `int|null $writeBufferSoftLimit` parameter that controls
937952
this maximum buffer size in bytes.
938953
You can use a `null` value here in order to apply its default value.
939954
This value SHOULD NOT be changed unless you know what you're doing.
940955

941956
```php
942-
$stream = new WritableResourceStream(STDOUT, $loop, 8192);
957+
$stream = new WritableResourceStream(STDOUT, null, 8192);
943958
```
944959

945960
This class takes an optional `int|null $writeChunkSize` parameter that controls
@@ -954,11 +969,14 @@ This can be `-1` which means "write everything available" to the
954969
underlying stream resource.
955970

956971
```php
957-
$stream = new WritableResourceStream(STDOUT, $loop, null, 8192);
972+
$stream = new WritableResourceStream(STDOUT, null, null, 8192);
958973
```
959974

960975
See also [`write()`](#write) for more details.
961976

977+
> Changelog: As of v1.2.0 the `$loop` parameter can be omitted (or skipped with a
978+
`null` value) to use the [default loop](https://github.com/reactphp/event-loop#loop).
979+
962980
### DuplexResourceStream
963981

964982
The `DuplexResourceStream` is a concrete implementation of the
@@ -969,7 +987,7 @@ in read and write mode mode or a stream such as a TCP/IP connection:
969987

970988
```php
971989
$conn = stream_socket_client('tcp://google.com:80');
972-
$stream = new DuplexResourceStream($conn, $loop);
990+
$stream = new DuplexResourceStream($conn);
973991
$stream->write('hello!');
974992
$stream->end();
975993
```
@@ -982,7 +1000,7 @@ Otherwise, it will throw an `InvalidArgumentException`:
9821000

9831001
```php
9841002
// throws InvalidArgumentException
985-
$stream = new DuplexResourceStream(false, $loop);
1003+
$stream = new DuplexResourceStream(false);
9861004
```
9871005

9881006
See also the [`ReadableResourceStream`](#readableresourcestream) for read-only
@@ -996,14 +1014,20 @@ If this fails, it will throw a `RuntimeException`:
9961014

9971015
```php
9981016
// throws RuntimeException on Windows
999-
$stream = new DuplexResourceStream(STDOUT, $loop);
1017+
$stream = new DuplexResourceStream(STDOUT);
10001018
```
10011019

10021020
Once the constructor is called with a valid stream resource, this class will
10031021
take care of the underlying stream resource.
10041022
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
10051023
stream resource manually.
10061024

1025+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
1026+
pass the event loop instance to use for this object. You can use a `null` value
1027+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
1028+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
1029+
given event loop instance.
1030+
10071031
This class takes an optional `int|null $readChunkSize` parameter that controls
10081032
the maximum buffer size in bytes to read at once from the stream.
10091033
You can use a `null` value here in order to apply its default value.
@@ -1020,7 +1044,7 @@ mean it reached EOF.
10201044

10211045
```php
10221046
$conn = stream_socket_client('tcp://google.com:80');
1023-
$stream = new DuplexResourceStream($conn, $loop, 8192);
1047+
$stream = new DuplexResourceStream($conn, null, 8192);
10241048
```
10251049

10261050
Any `write()` calls to this class will not be performed instantly, but will
@@ -1040,12 +1064,15 @@ If you want to change the write buffer soft limit, you can pass an instance of
10401064

10411065
```php
10421066
$conn = stream_socket_client('tcp://google.com:80');
1043-
$buffer = new WritableResourceStream($conn, $loop, 8192);
1044-
$stream = new DuplexResourceStream($conn, $loop, null, $buffer);
1067+
$buffer = new WritableResourceStream($conn, null, 8192);
1068+
$stream = new DuplexResourceStream($conn, null, null, $buffer);
10451069
```
10461070

10471071
See also [`WritableResourceStream`](#writableresourcestream) for more details.
10481072

1073+
> Changelog: As of v1.2.0 the `$loop` parameter can be omitted (or skipped with a
1074+
`null` value) to use the [default loop](https://github.com/reactphp/event-loop#loop).
1075+
10491076
### ThroughStream
10501077

10511078
The `ThroughStream` implements the
@@ -1123,8 +1150,8 @@ This is useful for some APIs which may require a single
11231150
more convenient to work with a single stream instance like this:
11241151

11251152
```php
1126-
$stdin = new ReadableResourceStream(STDIN, $loop);
1127-
$stdout = new WritableResourceStream(STDOUT, $loop);
1153+
$stdin = new ReadableResourceStream(STDIN);
1154+
$stdout = new WritableResourceStream(STDOUT);
11281155

11291156
$stdio = new CompositeStream($stdin, $stdout);
11301157

@@ -1154,14 +1181,10 @@ The following example can be used to pipe the contents of a source file into
11541181
a destination file without having to ever read the whole file into memory:
11551182

11561183
```php
1157-
$loop = new React\EventLoop\StreamSelectLoop;
1158-
1159-
$source = new React\Stream\ReadableResourceStream(fopen('source.txt', 'r'), $loop);
1160-
$dest = new React\Stream\WritableResourceStream(fopen('destination.txt', 'w'), $loop);
1184+
$source = new React\Stream\ReadableResourceStream(fopen('source.txt', 'r'));
1185+
$dest = new React\Stream\WritableResourceStream(fopen('destination.txt', 'w'));
11611186

11621187
$source->pipe($dest);
1163-
1164-
$loop->run();
11651188
```
11661189

11671190
> Note that this example uses `fopen()` for illustration purposes only.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"require": {
2929
"php": ">=5.3.8",
30-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
30+
"react/event-loop": "^1.2",
3131
"evenement/evenement": "^3.0 || ^2.0 || ^1.0"
3232
},
3333
"require-dev": {

examples/01-http.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// $ php examples/01-http.php
1212
// $ php examples/01-http.php reactphp.org
1313

14-
use React\EventLoop\Factory;
1514
use React\Stream\DuplexResourceStream;
1615

1716
require __DIR__ . '/../vendor/autoload.php';
@@ -25,8 +24,7 @@
2524
exit(1);
2625
}
2726

28-
$loop = Factory::create();
29-
$stream = new DuplexResourceStream($resource, $loop);
27+
$stream = new DuplexResourceStream($resource);
3028

3129
$stream->on('data', function ($chunk) {
3230
echo $chunk;
@@ -36,5 +34,3 @@
3634
});
3735

3836
$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
39-
40-
$loop->run();

examples/02-https.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// $ php examples/02-https.php
1212
// $ php examples/02-https.php reactphp.org
1313

14-
use React\EventLoop\Factory;
1514
use React\Stream\DuplexResourceStream;
1615

1716
require __DIR__ . '/../vendor/autoload.php';
@@ -25,8 +24,7 @@
2524
exit(1);
2625
}
2726

28-
$loop = Factory::create();
29-
$stream = new DuplexResourceStream($resource, $loop);
27+
$stream = new DuplexResourceStream($resource);
3028

3129
$stream->on('data', function ($chunk) {
3230
echo $chunk;
@@ -36,5 +34,3 @@
3634
});
3735

3836
$stream->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
39-
40-
$loop->run();

examples/11-cat.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// $ php examples/11-cat.php < README.md
99
// $ echo hello | php examples/11-cat.php
1010

11-
use React\EventLoop\Factory;
1211
use React\Stream\ReadableResourceStream;
1312
use React\Stream\WritableResourceStream;
1413

@@ -19,10 +18,6 @@
1918
exit(1);
2019
}
2120

22-
$loop = Factory::create();
23-
24-
$stdout = new WritableResourceStream(STDOUT, $loop);
25-
$stdin = new ReadableResourceStream(STDIN, $loop);
21+
$stdout = new WritableResourceStream(STDOUT);
22+
$stdin = new ReadableResourceStream(STDIN);
2623
$stdin->pipe($stdout);
27-
28-
$loop->run();

examples/91-benchmark-throughput.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// $ php examples/91-benchmark-throughput.php -t 10 -o zero.bin
1212
// $ php examples/91-benchmark-throughput.php -t 60 -i zero.bin
1313

14+
use React\EventLoop\Loop;
15+
1416
require __DIR__ . '/../vendor/autoload.php';
1517

1618
if (DIRECTORY_SEPARATOR === '\\') {
@@ -27,36 +29,32 @@
2729
$if = str_replace('/dev/fd/', 'php://fd/', $if);
2830
$of = str_replace('/dev/fd/', 'php://fd/', $of);
2931

30-
$loop = new React\EventLoop\StreamSelectLoop();
31-
3232
// setup information stream
33-
$info = new React\Stream\WritableResourceStream(STDERR, $loop);
33+
$info = new React\Stream\WritableResourceStream(STDERR);
3434
if (extension_loaded('xdebug')) {
3535
$info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
3636
}
3737
$info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL);
3838

3939
// setup input and output streams and pipe inbetween
4040
$fh = fopen($if, 'r');
41-
$in = new React\Stream\ReadableResourceStream($fh, $loop);
42-
$out = new React\Stream\WritableResourceStream(fopen($of, 'w'), $loop);
41+
$in = new React\Stream\ReadableResourceStream($fh);
42+
$out = new React\Stream\WritableResourceStream(fopen($of, 'w'));
4343
$in->pipe($out);
4444

4545
// stop input stream in $t seconds
4646
$start = microtime(true);
47-
$timeout = $loop->addTimer($t, function () use ($in, &$bytes) {
47+
$timeout = Loop::addTimer($t, function () use ($in) {
4848
$in->close();
4949
});
5050

5151
// print stream position once stream closes
52-
$in->on('close', function () use ($fh, $start, $loop, $timeout, $info) {
52+
$in->on('close', function () use ($fh, $start, $timeout, $info) {
5353
$t = microtime(true) - $start;
54-
$loop->cancelTimer($timeout);
54+
Loop::cancelTimer($timeout);
5555

5656
$bytes = ftell($fh);
5757

5858
$info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
5959
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);
6060
});
61-
62-
$loop->run();

src/DuplexResourceStream.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace React\Stream;
44

55
use Evenement\EventEmitter;
6+
use React\EventLoop\Loop;
67
use React\EventLoop\LoopInterface;
78
use InvalidArgumentException;
89

910
final class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
1011
{
1112
private $stream;
13+
14+
/** @var LoopInterface */
1215
private $loop;
1316

1417
/**
@@ -35,7 +38,7 @@ final class DuplexResourceStream extends EventEmitter implements DuplexStreamInt
3538
private $closing = false;
3639
private $listening = false;
3740

38-
public function __construct($stream, LoopInterface $loop, $readChunkSize = null, WritableStreamInterface $buffer = null)
41+
public function __construct($stream, LoopInterface $loop = null, $readChunkSize = null, WritableStreamInterface $buffer = null)
3942
{
4043
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
4144
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -70,7 +73,7 @@ public function __construct($stream, LoopInterface $loop, $readChunkSize = null,
7073
}
7174

7275
$this->stream = $stream;
73-
$this->loop = $loop;
76+
$this->loop = $loop ?: Loop::get();
7477
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7578
$this->buffer = $buffer;
7679

0 commit comments

Comments
 (0)