2222 */
2323class StreamWrapper
2424{
25- /** @var resource|string| null */
25+ /** @var resource|null */
2626 public $ context ;
2727
2828 private HttpClientInterface |ResponseInterface $ client ;
2929
3030 private ResponseInterface $ response ;
3131
32- /** @var resource|null */
32+ /** @var resource|string| null */
3333 private $ content ;
3434
3535 /** @var resource|null */
@@ -38,7 +38,7 @@ class StreamWrapper
3838 private bool $ blocking = true ;
3939 private ?float $ timeout = null ;
4040 private bool $ eof = false ;
41- private int $ offset = 0 ;
41+ private ? int $ offset = 0 ;
4242
4343 /**
4444 * Creates a PHP stream resource from a ResponseInterface.
@@ -87,6 +87,7 @@ public function bindHandles(&$handle, &$content): void
8787 {
8888 $ this ->handle = &$ handle ;
8989 $ this ->content = &$ content ;
90+ $ this ->offset = null ;
9091 }
9192
9293 public function stream_open (string $ path , string $ mode , int $ options ): bool
@@ -131,7 +132,7 @@ public function stream_read(int $count): string|false
131132 }
132133 }
133134
134- if (0 !== fseek ($ this ->content , $ this ->offset )) {
135+ if (0 !== fseek ($ this ->content , $ this ->offset ?? 0 )) {
135136 return false ;
136137 }
137138
@@ -160,6 +161,11 @@ public function stream_read(int $count): string|false
160161 try {
161162 $ this ->eof = true ;
162163 $ this ->eof = !$ chunk ->isTimeout ();
164+
165+ if (!$ this ->eof && !$ this ->blocking ) {
166+ return '' ;
167+ }
168+
163169 $ this ->eof = $ chunk ->isLast ();
164170
165171 if ($ chunk ->isFirst ()) {
@@ -202,7 +208,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
202208
203209 public function stream_tell (): int
204210 {
205- return $ this ->offset ;
211+ return $ this ->offset ?? 0 ;
206212 }
207213
208214 public function stream_eof (): bool
@@ -212,14 +218,19 @@ public function stream_eof(): bool
212218
213219 public function stream_seek (int $ offset , int $ whence = \SEEK_SET ): bool
214220 {
221+ if (null === $ this ->content && null === $ this ->offset ) {
222+ $ this ->response ->getStatusCode ();
223+ $ this ->offset = 0 ;
224+ }
225+
215226 if (!\is_resource ($ this ->content ) || 0 !== fseek ($ this ->content , 0 , \SEEK_END )) {
216227 return false ;
217228 }
218229
219230 $ size = ftell ($ this ->content );
220231
221232 if (\SEEK_CUR === $ whence ) {
222- $ offset += $ this ->offset ;
233+ $ offset += $ this ->offset ?? 0 ;
223234 }
224235
225236 if (\SEEK_END === $ whence || $ size < $ offset ) {
0 commit comments