@@ -22,18 +22,26 @@ 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()
@@ -44,7 +52,7 @@ create a `Promise` which resolves with the stream data buffer.
44
52
``` php
45
53
$stream = accessSomeJsonStream();
46
54
47
- Stream\buffer($stream)->then(function ($contents) {
55
+ React\Promise\ Stream\buffer($stream)->then(function ($contents) {
48
56
var_dump(json_decode($contents));
49
57
});
50
58
```
@@ -64,7 +72,7 @@ will be rejected with an `\OverflowException`.
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,
@@ -81,7 +89,7 @@ create a `Promise` which resolves once the given event triggers for the first ti
81
89
``` php
82
90
$stream = accessSomeJsonStream();
83
91
84
- Stream\first($stream)->then(function ($chunk) {
92
+ React\Promise\ Stream\first($stream)->then(function ($chunk) {
85
93
echo 'The first chunk arrived: ' . $chunk;
86
94
});
87
95
```
@@ -109,7 +117,7 @@ create a `Promise` which resolves with an array of all the event data.
109
117
``` php
110
118
$stream = accessSomeJsonStream();
111
119
112
- Stream\all($stream)->then(function ($chunks) {
120
+ React\Promise\ Stream\all($stream)->then(function ($chunks) {
113
121
echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
114
122
});
115
123
```
@@ -141,7 +149,7 @@ be piped to the output stream.
141
149
//$promise = someFunctionWhichResolvesWithAStream();
142
150
$promise = startDownloadStream($uri);
143
151
144
- $stream = Stream\unwrapReadable($promise);
152
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
145
153
146
154
$stream->on('data', function ($data) {
147
155
echo $data;
@@ -159,7 +167,7 @@ an `error` event and close:
159
167
``` php
160
168
$promise = startDownloadStream($invalidUri);
161
169
162
- $stream = Stream\unwrapReadable($promise);
170
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
163
171
164
172
$stream->on('error', function (Exception $error) {
165
173
echo 'Error: ' . $error->getMessage();
@@ -178,7 +186,7 @@ You can `close()` the resulting stream at any time, which will either try to
178
186
``` php
179
187
$promise = startDownloadStream($uri);
180
188
181
- $stream = Stream\unwrapReadable($promise);
189
+ $stream = React\Promise\ Stream\unwrapReadable($promise);
182
190
183
191
$loop->addTimer(2.0, function () use ($stream) {
184
192
$stream->close();
@@ -200,7 +208,7 @@ have written to the proxy will be forwarded transparently to the inner stream.
200
208
//$promise = someFunctionWhichResolvesWithAStream();
201
209
$promise = startUploadStream($uri);
202
210
203
- $stream = Stream\unwrapWritable($promise);
211
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
204
212
205
213
$stream->write('hello');
206
214
$stream->end('world');
@@ -217,7 +225,7 @@ an `error` event and close:
217
225
``` php
218
226
$promise = startUploadStream($invalidUri);
219
227
220
- $stream = Stream\unwrapWritable($promise);
228
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
221
229
222
230
$stream->on('error', function (Exception $error) {
223
231
echo 'Error: ' . $error->getMessage();
@@ -236,7 +244,7 @@ You can `close()` the resulting stream at any time, which will either try to
236
244
``` php
237
245
$promise = startUploadStream($uri);
238
246
239
- $stream = Stream\unwrapWritable($promise);
247
+ $stream = React\Promise\ Stream\unwrapWritable($promise);
240
248
241
249
$loop->addTimer(2.0, function () use ($stream) {
242
250
$stream->close();
0 commit comments