22
22
*/
23
23
class StreamWrapper
24
24
{
25
- /** @var resource */
25
+ /** @var resource|string|null */
26
26
public $ context ;
27
27
28
28
/** @var HttpClientInterface */
@@ -103,7 +103,7 @@ public function stream_open(string $path, string $mode, int $options): bool
103
103
104
104
public function stream_read (int $ count )
105
105
{
106
- if (null !== $ this ->content ) {
106
+ if (\is_resource ( $ this ->content ) ) {
107
107
// Empty the internal activity list
108
108
foreach ($ this ->client ->stream ([$ this ->response ], 0 ) as $ chunk ) {
109
109
try {
@@ -127,13 +127,32 @@ public function stream_read(int $count)
127
127
}
128
128
}
129
129
130
+ if (\is_string ($ this ->content )) {
131
+ if (\strlen ($ this ->content ) <= $ count ) {
132
+ $ data = $ this ->content ;
133
+ $ this ->content = null ;
134
+ } else {
135
+ $ data = substr ($ this ->content , 0 , $ count );
136
+ $ this ->content = substr ($ this ->content , $ count );
137
+ }
138
+ $ this ->offset += \strlen ($ data );
139
+
140
+ return $ data ;
141
+ }
142
+
130
143
foreach ($ this ->client ->stream ([$ this ->response ]) as $ chunk ) {
131
144
try {
132
145
$ this ->eof = true ;
133
146
$ this ->eof = !$ chunk ->isTimeout ();
134
147
$ this ->eof = $ chunk ->isLast ();
135
148
136
149
if ('' !== $ data = $ chunk ->getContent ()) {
150
+ if (\strlen ($ data ) > $ count ) {
151
+ if (null === $ this ->content ) {
152
+ $ this ->content = substr ($ data , $ count );
153
+ }
154
+ $ data = substr ($ data , 0 , $ count );
155
+ }
137
156
$ this ->offset += \strlen ($ data );
138
157
139
158
return $ data ;
@@ -155,12 +174,12 @@ public function stream_tell(): int
155
174
156
175
public function stream_eof (): bool
157
176
{
158
- return $ this ->eof ;
177
+ return $ this ->eof && ! \is_string ( $ this -> content ) ;
159
178
}
160
179
161
180
public function stream_seek (int $ offset , int $ whence = SEEK_SET ): bool
162
181
{
163
- if (null === $ this ->content || 0 !== fseek ($ this ->content , 0 , SEEK_END )) {
182
+ if (! \is_resource ( $ this ->content ) || 0 !== fseek ($ this ->content , 0 , SEEK_END )) {
164
183
return false ;
165
184
}
166
185
0 commit comments