2222 */
2323class StreamWrapper
2424{
25- /** @var resource|string| null */
25+ /** @var resource|null */
2626 public $ context ;
2727
2828 /** @var HttpClientInterface */
@@ -31,7 +31,7 @@ class StreamWrapper
3131 /** @var ResponseInterface */
3232 private $ response ;
3333
34- /** @var resource|null */
34+ /** @var resource|string| null */
3535 private $ content ;
3636
3737 /** @var resource|null */
@@ -89,6 +89,7 @@ public function bindHandles(&$handle, &$content): void
8989 {
9090 $ this ->handle = &$ handle ;
9191 $ this ->content = &$ content ;
92+ $ this ->offset = null ;
9293 }
9394
9495 public function stream_open (string $ path , string $ mode , int $ options ): bool
@@ -133,7 +134,7 @@ public function stream_read(int $count)
133134 }
134135 }
135136
136- if (0 !== fseek ($ this ->content , $ this ->offset )) {
137+ if (0 !== fseek ($ this ->content , $ this ->offset ?? 0 )) {
137138 return false ;
138139 }
139140
@@ -162,6 +163,11 @@ public function stream_read(int $count)
162163 try {
163164 $ this ->eof = true ;
164165 $ this ->eof = !$ chunk ->isTimeout ();
166+
167+ if (!$ this ->eof && !$ this ->blocking ) {
168+ return '' ;
169+ }
170+
165171 $ this ->eof = $ chunk ->isLast ();
166172
167173 if ($ chunk ->isFirst ()) {
@@ -204,7 +210,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
204210
205211 public function stream_tell (): int
206212 {
207- return $ this ->offset ;
213+ return $ this ->offset ?? 0 ;
208214 }
209215
210216 public function stream_eof (): bool
@@ -214,14 +220,19 @@ public function stream_eof(): bool
214220
215221 public function stream_seek (int $ offset , int $ whence = \SEEK_SET ): bool
216222 {
223+ if (null === $ this ->content && null === $ this ->offset ) {
224+ $ this ->response ->getStatusCode ();
225+ $ this ->offset = 0 ;
226+ }
227+
217228 if (!\is_resource ($ this ->content ) || 0 !== fseek ($ this ->content , 0 , \SEEK_END )) {
218229 return false ;
219230 }
220231
221232 $ size = ftell ($ this ->content );
222233
223234 if (\SEEK_CUR === $ whence ) {
224- $ offset += $ this ->offset ;
235+ $ offset += $ this ->offset ?? 0 ;
225236 }
226237
227238 if (\SEEK_END === $ whence || $ size < $ offset ) {
0 commit comments