22
22
*/
23
23
class StreamWrapper
24
24
{
25
- /** @var resource|string| null */
25
+ /** @var resource|null */
26
26
public $ context ;
27
27
28
28
private $ client ;
29
29
30
30
private $ response ;
31
31
32
- /** @var resource|null */
32
+ /** @var resource|string| null */
33
33
private $ content ;
34
34
35
35
/** @var resource|null */
@@ -38,7 +38,7 @@ class StreamWrapper
38
38
private bool $ blocking = true ;
39
39
private ?float $ timeout = null ;
40
40
private bool $ eof = false ;
41
- private int $ offset = 0 ;
41
+ private ? int $ offset = 0 ;
42
42
43
43
/**
44
44
* Creates a PHP stream resource from a ResponseInterface.
@@ -87,6 +87,7 @@ public function bindHandles(&$handle, &$content): void
87
87
{
88
88
$ this ->handle = &$ handle ;
89
89
$ this ->content = &$ content ;
90
+ $ this ->offset = null ;
90
91
}
91
92
92
93
public function stream_open (string $ path , string $ mode , int $ options ): bool
@@ -131,7 +132,7 @@ public function stream_read(int $count)
131
132
}
132
133
}
133
134
134
- if (0 !== fseek ($ this ->content , $ this ->offset )) {
135
+ if (0 !== fseek ($ this ->content , $ this ->offset ?? 0 )) {
135
136
return false ;
136
137
}
137
138
@@ -160,6 +161,11 @@ public function stream_read(int $count)
160
161
try {
161
162
$ this ->eof = true ;
162
163
$ this ->eof = !$ chunk ->isTimeout ();
164
+
165
+ if (!$ this ->eof && !$ this ->blocking ) {
166
+ return '' ;
167
+ }
168
+
163
169
$ this ->eof = $ chunk ->isLast ();
164
170
165
171
if ($ chunk ->isFirst ()) {
@@ -202,7 +208,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
202
208
203
209
public function stream_tell (): int
204
210
{
205
- return $ this ->offset ;
211
+ return $ this ->offset ?? 0 ;
206
212
}
207
213
208
214
public function stream_eof (): bool
@@ -212,14 +218,19 @@ public function stream_eof(): bool
212
218
213
219
public function stream_seek (int $ offset , int $ whence = \SEEK_SET ): bool
214
220
{
221
+ if (null === $ this ->content && null === $ this ->offset ) {
222
+ $ this ->response ->getStatusCode ();
223
+ $ this ->offset = 0 ;
224
+ }
225
+
215
226
if (!\is_resource ($ this ->content ) || 0 !== fseek ($ this ->content , 0 , \SEEK_END )) {
216
227
return false ;
217
228
}
218
229
219
230
$ size = ftell ($ this ->content );
220
231
221
232
if (\SEEK_CUR === $ whence ) {
222
- $ offset += $ this ->offset ;
233
+ $ offset += $ this ->offset ?? 0 ;
223
234
}
224
235
225
236
if (\SEEK_END === $ whence || $ size < $ offset ) {
0 commit comments