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
/** @var HttpClientInterface */
@@ -31,7 +31,7 @@ class StreamWrapper
31
31
/** @var ResponseInterface */
32
32
private $ response ;
33
33
34
- /** @var resource|null */
34
+ /** @var resource|string| null */
35
35
private $ content ;
36
36
37
37
/** @var resource|null */
@@ -81,6 +81,7 @@ public function bindHandles(&$handle, &$content): void
81
81
{
82
82
$ this ->handle = &$ handle ;
83
83
$ this ->content = &$ content ;
84
+ $ this ->offset = null ;
84
85
}
85
86
86
87
public function stream_open (string $ path , string $ mode , int $ options ): bool
@@ -125,7 +126,7 @@ public function stream_read(int $count)
125
126
}
126
127
}
127
128
128
- if (0 !== fseek ($ this ->content , $ this ->offset )) {
129
+ if (0 !== fseek ($ this ->content , $ this ->offset ?? 0 )) {
129
130
return false ;
130
131
}
131
132
@@ -154,6 +155,11 @@ public function stream_read(int $count)
154
155
try {
155
156
$ this ->eof = true ;
156
157
$ this ->eof = !$ chunk ->isTimeout ();
158
+
159
+ if (!$ this ->eof && !$ this ->blocking ) {
160
+ return '' ;
161
+ }
162
+
157
163
$ this ->eof = $ chunk ->isLast ();
158
164
159
165
if ($ chunk ->isFirst ()) {
@@ -196,7 +202,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
196
202
197
203
public function stream_tell (): int
198
204
{
199
- return $ this ->offset ;
205
+ return $ this ->offset ?? 0 ;
200
206
}
201
207
202
208
public function stream_eof (): bool
@@ -206,14 +212,19 @@ public function stream_eof(): bool
206
212
207
213
public function stream_seek (int $ offset , int $ whence = \SEEK_SET ): bool
208
214
{
215
+ if (null === $ this ->content && null === $ this ->offset ) {
216
+ $ this ->response ->getStatusCode ();
217
+ $ this ->offset = 0 ;
218
+ }
219
+
209
220
if (!\is_resource ($ this ->content ) || 0 !== fseek ($ this ->content , 0 , \SEEK_END )) {
210
221
return false ;
211
222
}
212
223
213
224
$ size = ftell ($ this ->content );
214
225
215
226
if (\SEEK_CUR === $ whence ) {
216
- $ offset += $ this ->offset ;
227
+ $ offset += $ this ->offset ?? 0 ;
217
228
}
218
229
219
230
if (\SEEK_END === $ whence || $ size < $ offset ) {
0 commit comments