@@ -8,11 +8,11 @@ for [ReactPHP](https://reactphp.org/).
8
8
** Table of Contents**
9
9
10
10
* [ Usage] ( #usage )
11
- * [ buffer()] ( #buffer )
12
- * [ first()] ( #first )
13
- * [ all()] ( #all )
14
- * [ unwrapReadable()] ( #unwrapreadable )
15
- * [ unwrapWritable()] ( #unwrapwritable )
11
+ * [ buffer()] ( #buffer )
12
+ * [ first()] ( #first )
13
+ * [ all()] ( #all )
14
+ * [ unwrapReadable()] ( #unwrapreadable )
15
+ * [ unwrapWritable()] ( #unwrapwritable )
16
16
* [ Install] ( #install )
17
17
* [ Tests] ( #tests )
18
18
* [ License] ( #license )
@@ -22,49 +22,57 @@ for [ReactPHP](https://reactphp.org/).
22
22
This lightweight library consists only of a few simple functions.
23
23
All functions reside under the ` React\Promise\Stream ` namespace.
24
24
25
- The below examples assume you use an import statement similar to this:
25
+ The below examples assume refer to them with their fully-qualified names like this:
26
26
27
27
``` php
28
- use React\Promise\Stream;
28
+ React\Promise\Stream\buffer(…);
29
+ ```
29
30
30
- Stream\buffer(…);
31
+ As of PHP 5.6+ you can also import each required function into your code like this:
32
+
33
+ ``` php
34
+ use function React\Promise\Stream\buffer;
35
+
36
+ buffer(…);
31
37
```
32
38
33
- Alternatively, you can also refer to them with their fully-qualified name :
39
+ Alternatively, you can also use an import statement similar to this :
34
40
35
41
``` php
36
- \React\Promise\Stream\buffer(…);
42
+ use React\Promise\Stream;
43
+
44
+ Stream\buffer(…);
37
45
```
38
46
39
47
### buffer()
40
48
41
- The ` buffer(ReadableStreamInterface<string> $stream, ?int $maxLength = null): PromiseInterface<string,Exception > ` function can be used to
42
- create a ` Promise ` which resolves with the stream data buffer.
49
+ The ` buffer(ReadableStreamInterface<string> $stream, ?int $maxLength = null): PromiseInterface<string,RuntimeException > ` function can be used to
50
+ create a ` Promise ` which will be fulfilled with the stream data buffer.
43
51
44
52
``` php
45
53
$stream = accessSomeJsonStream();
46
54
47
- Stream\buffer($stream)->then(function ($contents) {
55
+ React\Promise\ Stream\buffer($stream)->then(function (string $contents) {
48
56
var_dump(json_decode($contents));
49
57
});
50
58
```
51
59
52
- The promise will resolve with all data chunks concatenated once the stream closes.
60
+ The promise will be fulfilled with a ` string ` of all data chunks concatenated once the stream closes.
53
61
54
- The promise will resolve with an empty string if the stream is already closed.
62
+ The promise will be fulfilled with an empty ` string ` if the stream is already closed.
55
63
56
- The promise will reject if the stream emits an error.
64
+ The promise will be rejected with a ` RuntimeException ` if the stream emits an error.
57
65
58
- The promise will reject if it is cancelled.
66
+ The promise will be rejected with a ` RuntimeException ` if it is cancelled.
59
67
60
68
The optional ` $maxLength ` argument defaults to no limit. In case the maximum
61
69
length is given and the stream emits more data before the end, the promise
62
- will be rejected with an ` \ OverflowException` .
70
+ will be rejected with an ` OverflowException ` .
63
71
64
72
``` php
65
73
$stream = accessSomeToLargeStream();
66
74
67
- Stream\buffer($stream, 1024)->then(function ($contents) {
75
+ React\Promise\ Stream\buffer($stream, 1024)->then(function ($contents) {
68
76
var_dump(json_decode($contents));
69
77
}, function ($error) {
70
78
// Reaching here when the stream buffer goes above the max size,
@@ -75,75 +83,77 @@ Stream\buffer($stream, 1024)->then(function ($contents) {
75
83
76
84
### first()
77
85
78
- The ` first(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<mixed,Exception > ` function can be used to
79
- create a ` Promise ` which resolves once the given event triggers for the first time.
86
+ The ` first(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<mixed,RuntimeException > ` function can be used to
87
+ create a ` Promise ` which will be fulfilled once the given event triggers for the first time.
80
88
81
89
``` php
82
90
$stream = accessSomeJsonStream();
83
91
84
- Stream\first($stream)->then(function ($chunk) {
92
+ React\Promise\ Stream\first($stream)->then(function (string $chunk) {
85
93
echo 'The first chunk arrived: ' . $chunk;
86
94
});
87
95
```
88
96
89
- The promise will resolve with whatever the first event emitted or ` null ` if the
90
- event does not pass any data.
97
+ The promise will be fulfilled with a ` mixed ` value of whatever the first event
98
+ emitted or ` null ` if the event does not pass any data.
91
99
If you do not pass a custom event name, then it will wait for the first "data"
92
- event and resolve with a string containing the first data chunk.
100
+ event.
101
+ For common streams of type ` ReadableStreamInterface<string> ` , this means it will be
102
+ fulfilled with a ` string ` containing the first data chunk.
93
103
94
- The promise will reject if the stream emits an error – unless you're waiting for
95
- the "error" event, in which case it will resolve .
104
+ The promise will be rejected with a ` RuntimeException ` if the stream emits an error
105
+ – unless you're waiting for the "error" event, in which case it will be fulfilled .
96
106
97
- The promise will reject once the stream closes – unless you're waiting for the
98
- "close" event, in which case it will resolve .
107
+ The promise will be rejected with a ` RuntimeException ` once the stream closes
108
+ – unless you're waiting for the "close" event, in which case it will be fulfilled .
99
109
100
- The promise will reject if the stream is already closed.
110
+ The promise will be rejected with a ` RuntimeException ` if the stream is already closed.
101
111
102
- The promise will reject if it is cancelled.
112
+ The promise will be rejected with a ` RuntimeException ` if it is cancelled.
103
113
104
114
### all()
105
115
106
- The ` all(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<array,Exception > ` function can be used to
107
- create a ` Promise ` which resolves with an array of all the event data.
116
+ The ` all(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<array,RuntimeException > ` function can be used to
117
+ create a ` Promise ` which will be fulfilled with an array of all the event data.
108
118
109
119
``` php
110
120
$stream = accessSomeJsonStream();
111
121
112
- Stream\all($stream)->then(function ($chunks) {
122
+ React\Promise\ Stream\all($stream)->then(function (array $chunks) {
113
123
echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
114
124
});
115
125
```
116
126
117
- The promise will resolve with an array of whatever all events emitted or ` null ` if the
118
- events do not pass any data.
127
+ The promise will be fulfilled with an ` array ` once the stream closes. The array
128
+ will contain whatever all events emitted or ` null ` values if the events do not pass any data.
119
129
If you do not pass a custom event name, then it will wait for all the "data"
120
- events and resolve with an array containing all the data chunks .
121
-
122
- The promise will resolve with an array once the stream closes .
130
+ events.
131
+ For common streams of type ` ReadableStreamInterface<string> ` , this means it will be
132
+ fulfilled with a ` string[] ` array containing all the data chunk .
123
133
124
- The promise will resolve with an empty array if the stream is already closed.
134
+ The promise will be fulfilled with an empty ` array ` if the stream is already closed.
125
135
126
- The promise will reject if the stream emits an error.
136
+ The promise will be rejected with a ` RuntimeException ` if the stream emits an error.
127
137
128
- The promise will reject if it is cancelled.
138
+ The promise will be rejected with a ` RuntimeException ` if it is cancelled.
129
139
130
140
### unwrapReadable()
131
141
132
- The ` unwrapReadable(PromiseInterface<ReadableStreamInterface,Exception> $promise): ReadableStreamInterface ` function can be used to
133
- unwrap a ` Promise ` which resolves with a ` ReadableStreamInterface ` .
142
+ The ` unwrapReadable(PromiseInterface<ReadableStreamInterface<T> ,Exception> $promise): ReadableStreamInterface<T> ` function can be used to
143
+ unwrap a ` Promise ` which will be fulfilled with a ` ReadableStreamInterface<T> ` .
134
144
135
- This function returns a readable stream instance (implementing ` ReadableStreamInterface ` )
145
+ This function returns a readable stream instance (implementing ` ReadableStreamInterface<T> ` )
136
146
right away which acts as a proxy for the future promise resolution.
137
- Once the given Promise resolves with a ` ReadableStreamInterface ` , its data will
138
- be piped to the output stream.
147
+ Once the given Promise will be fulfilled with a ` ReadableStreamInterface<T> ` , its
148
+ data will be piped to the output stream.
139
149
140
150
``` php
141
151
//$promise = someFunctionWhichResolvesWithAStream();
142
152
$promise = startDownloadStream($uri);
143
153
144
- $stream = Stream\unwrapReadable($promise);
154
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
145
155
146
- $stream->on('data', function ($data) {
156
+ $stream->on('data', function (string $data) {
147
157
echo $data;
148
158
});
149
159
@@ -159,7 +169,7 @@ an `error` event and close:
159
169
``` php
160
170
$promise = startDownloadStream($invalidUri);
161
171
162
- $stream = Stream\unwrapReadable($promise);
172
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
163
173
164
174
$stream->on('error', function (Exception $error) {
165
175
echo 'Error: ' . $error->getMessage();
@@ -168,17 +178,16 @@ $stream->on('error', function (Exception $error) {
168
178
169
179
The given ` $promise ` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
170
180
at the time of invoking this function.
171
- If the given promise is already settled and does not resolve with an
172
- instance of ` ReadableStreamInterface ` , then you will not be able to receive
173
- the ` error ` event.
181
+ If the given promise is already settled and does not fulfill with an instance of
182
+ ` ReadableStreamInterface ` , then you will not be able to receive the ` error ` event.
174
183
175
184
You can ` close() ` the resulting stream at any time, which will either try to
176
185
` cancel() ` the pending promise or try to ` close() ` the underlying stream.
177
186
178
187
``` php
179
188
$promise = startDownloadStream($uri);
180
189
181
- $stream = Stream\unwrapReadable($promise);
190
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
182
191
183
192
$loop->addTimer(2.0, function () use ($stream) {
184
193
$stream->close();
@@ -187,20 +196,22 @@ $loop->addTimer(2.0, function () use ($stream) {
187
196
188
197
### unwrapWritable()
189
198
190
- The ` unwrapWritable(PromiseInterface<WritableStreamInterface,Exception> $promise): WritableStreamInterface ` function can be used to
191
- unwrap a ` Promise ` which resolves with a ` WritableStreamInterface ` .
199
+ The ` unwrapWritable(PromiseInterface<WritableStreamInterface<T> ,Exception> $promise): WritableStreamInterface<T> ` function can be used to
200
+ unwrap a ` Promise ` which will be fulfilled with a ` WritableStreamInterface<T> ` .
192
201
193
- This function returns a writable stream instance (implementing ` WritableStreamInterface ` )
202
+ This function returns a writable stream instance (implementing ` WritableStreamInterface<T> ` )
194
203
right away which acts as a proxy for the future promise resolution.
195
- Any writes to this instance will be buffered in memory for when the promise resolves.
196
- Once the given Promise resolves with a ` WritableStreamInterface ` , any data you
197
- have written to the proxy will be forwarded transparently to the inner stream.
204
+ Any writes to this instance will be buffered in memory for when the promise will
205
+ be fulfilled.
206
+ Once the given Promise will be fulfilled with a ` WritableStreamInterface<T> ` , any
207
+ data you have written to the proxy will be forwarded transparently to the inner
208
+ stream.
198
209
199
210
``` php
200
211
//$promise = someFunctionWhichResolvesWithAStream();
201
212
$promise = startUploadStream($uri);
202
213
203
- $stream = Stream\unwrapWritable($promise);
214
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
204
215
205
216
$stream->write('hello');
206
217
$stream->end('world');
@@ -217,7 +228,7 @@ an `error` event and close:
217
228
``` php
218
229
$promise = startUploadStream($invalidUri);
219
230
220
- $stream = Stream\unwrapWritable($promise);
231
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
221
232
222
233
$stream->on('error', function (Exception $error) {
223
234
echo 'Error: ' . $error->getMessage();
@@ -226,17 +237,16 @@ $stream->on('error', function (Exception $error) {
226
237
227
238
The given ` $promise ` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
228
239
at the time of invoking this function.
229
- If the given promise is already settled and does not resolve with an
230
- instance of ` WritableStreamInterface ` , then you will not be able to receive
231
- the ` error ` event.
240
+ If the given promise is already settled and does not fulfill with an instance of
241
+ ` WritableStreamInterface ` , then you will not be able to receive the ` error ` event.
232
242
233
243
You can ` close() ` the resulting stream at any time, which will either try to
234
244
` cancel() ` the pending promise or try to ` close() ` the underlying stream.
235
245
236
246
``` php
237
247
$promise = startUploadStream($uri);
238
248
239
- $stream = Stream\unwrapWritable($promise);
249
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
240
250
241
251
$loop->addTimer(2.0, function () use ($stream) {
242
252
$stream->close();
@@ -245,7 +255,7 @@ $loop->addTimer(2.0, function () use ($stream) {
245
255
246
256
## Install
247
257
248
- The recommended way to install this library is [ through Composer] ( https://getcomposer.org ) .
258
+ The recommended way to install this library is [ through Composer] ( https://getcomposer.org/ ) .
249
259
[ New to Composer?] ( https://getcomposer.org/doc/00-intro.md )
250
260
251
261
This project follows [ SemVer] ( https://semver.org/ ) .
@@ -260,12 +270,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
260
270
This project aims to run on any platform and thus does not require any PHP
261
271
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
262
272
HHVM.
263
- It's * highly recommended to use PHP 7+ * for this project.
273
+ It's * highly recommended to use the latest supported PHP version * for this project.
264
274
265
275
## Tests
266
276
267
277
To run the test suite, you first need to clone this repo and then install all
268
- dependencies [ through Composer] ( https://getcomposer.org ) :
278
+ dependencies [ through Composer] ( https://getcomposer.org/ ) :
269
279
270
280
``` bash
271
281
$ composer install
@@ -274,7 +284,7 @@ $ composer install
274
284
To run the test suite, go to the project root and run:
275
285
276
286
``` bash
277
- $ php vendor/bin/phpunit
287
+ $ vendor/bin/phpunit
278
288
```
279
289
280
290
## License
0 commit comments