Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/Response/StreamWrapper.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient\Response;
13
14 use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
15 use Symfony\Contracts\HttpClient\HttpClientInterface;
16 use Symfony\Contracts\HttpClient\ResponseInterface;
17
18 /**
19 * Allows turning ResponseInterface instances to PHP streams.
20 *
21 * @author Nicolas Grekas <[email protected]>
22 */
23 class StreamWrapper
24 {
25 /** @var resource|null */
26 public $context;
27
28 private HttpClientInterface|ResponseInterface $client;
29
30 private ResponseInterface $response;
31
32 /** @var resource|string|null */
33 private $content;
34
35 /** @var resource|callable|null */
36 private $handle;
37
38 private bool $blocking = true;
39 private ?float $timeout = null;
40 private bool $eof = false;
41 private ?int $offset = 0;
42
43 /**
44 * Creates a PHP stream resource from a ResponseInterface.
45 *
46 * @return resource
47 */
48 public static function createResource(ResponseInterface $response, ?HttpClientInterface $client = null)
49 {
50 if ($response instanceof StreamableInterface) {
51 $stack = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 2);
52
53 if ($response !== ($stack[1]['object'] ?? null)) {
54 return $response->toStream(false);
55 }
56 }
57
58 if (null === $client && !method_exists($response, 'stream')) {
59 throw new \InvalidArgumentException(\sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__));
60 }
61
62 static $registered = false;
63
64 if (!$registered = $registered || stream_wrapper_register(strtr(__CLASS__, '\\', '-'), __CLASS__)) {
65 throw new \RuntimeException(error_get_last()['message'] ?? 'Registering the "symfony" stream wrapper failed.');
66 }
67
68 $context = [
69 'client' => $client ?? $response,
70 'response' => $response,
71 ];
72
73 return fopen(strtr(__CLASS__, '\\', '-').'://'.$response->getInfo('url'), 'r', false, stream_context_create(['symfony' => $context]));
74 }
75
76 public function getResponse(): ResponseInterface
77 {
78 return $this->response;
79 }
80
81 /**
82 * @param resource|callable|null $handle The resource handle that should be monitored when
83 * stream_select() is used on the created stream
84 * @param resource|null $content The seekable resource where the response body is buffered
85 */
86 public function bindHandles(&$handle, &$content): void
87 {
88 $this->handle = &$handle;
89 $this->content = &$content;
90 $this->offset = null;
91 }
92
93 public function stream_open(string $path, string $mode, int $options): bool
94 {
95 if ('r' !== $mode) {
96 if ($options & \STREAM_REPORT_ERRORS) {
97 trigger_error(\sprintf('Invalid mode "%s": only "r" is supported.', $mode), \E_USER_WARNING);
98 }
99
100 return false;
101 }
102
103 $context = stream_context_get_options($this->context)['symfony'] ?? null;
104 $this->client = $context['client'] ?? null;
105 $this->response = $context['response'] ?? null;
106 $this->context = null;
107
108 if (null !== $this->client && null !== $this->response) {
109 return true;
110 }
111
112 if ($options & \STREAM_REPORT_ERRORS) {
113 trigger_error('Missing options "client" or "response" in "symfony" stream context.', \E_USER_WARNING);
114 }
115
116 return false;
117 }
118
119 public function stream_read(int $count): string|false
120 {
121 if (\is_resource($this->content)) {
122 // Empty the internal activity list
123 foreach ($this->client->stream([$this->response], 0) as $chunk) {
124 try {
125 if (!$chunk->isTimeout() && $chunk->isFirst()) {
126 $this->response->getStatusCode(); // ignore 3/4/5xx
127 }
128 } catch (ExceptionInterface $e) {
129 trigger_error($e->getMessage(), \E_USER_WARNING);
130
131 return false;
132 }
133 }
134
135 if (0 !== fseek($this->content, $this->offset ?? 0)) {
136 return false;
137 }
138
139 if ('' !== $data = fread($this->content, $count)) {
140 fseek($this->content, 0, \SEEK_END);
141 $this->offset += \strlen($data);
142
143 return $data;
144 }
145 }
146
147 if (\is_string($this->content)) {
148 if (\strlen($this->content) <= $count) {
149 $data = $this->content;
150 $this->content = null;
151 } else {
152 $data = substr($this->content, 0, $count);
153 $this->content = substr($this->content, $count);
154 }
155 $this->offset += \strlen($data);
156
157 return $data;
158 }
159
160 foreach ($this->client->stream([$this->response], $this->blocking ? $this->timeout : 0) as $chunk) {
161 try {
162 $this->eof = true;
163 $this->eof = !$chunk->isTimeout();
164
165 if (!$this->eof && !$this->blocking) {
166 return '';
167 }
168
169 $this->eof = $chunk->isLast();
170
171 if ($chunk->isFirst()) {
172 $this->response->getStatusCode(); // ignore 3/4/5xx
173 }
174
175 if ('' !== $data = $chunk->getContent()) {
176 if (\strlen($data) > $count) {
177 $this->content ??= substr($data, $count);
178 $data = substr($data, 0, $count);
179 }
180 $this->offset += \strlen($data);
181
182 return $data;
183 }
184 } catch (ExceptionInterface $e) {
185 trigger_error($e->getMessage(), \E_USER_WARNING);
186
187 return false;
188 }
189 }
190
191 return '';
192 }
193
194 public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
195 {
196 if (\STREAM_OPTION_BLOCKING === $option) {
197 $this->blocking = (bool) $arg1;
198 } elseif (\STREAM_OPTION_READ_TIMEOUT === $option) {
199 $this->timeout = $arg1 + $arg2 / 1e6;
200 } else {
201 return false;
202 }
203
204 return true;
205 }
206
207 public function stream_tell(): int
208 {
209 return $this->offset ?? 0;
210 }
211
212 public function stream_eof(): bool
213 {
214 return $this->eof && !\is_string($this->content);
215 }
216
217 public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
218 {
219 if (null === $this->content && null === $this->offset) {
220 $this->response->getStatusCode();
221 $this->offset = 0;
222 }
223
224 if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, \SEEK_END)) {
225 return false;
226 }
227
228 $size = ftell($this->content);
229
230 if (\SEEK_CUR === $whence) {
231 $offset += $this->offset ?? 0;
232 }
233
234 if (\SEEK_END === $whence || $size < $offset) {
235 foreach ($this->client->stream([$this->response]) as $chunk) {
236 try {
237 if ($chunk->isFirst()) {
238 $this->response->getStatusCode(); // ignore 3/4/5xx
239 }
240
241 // Chunks are buffered in $this->content already
242 $size += \strlen($chunk->getContent());
243
244 if (\SEEK_END !== $whence && $offset <= $size) {
245 break;
246 }
247 } catch (ExceptionInterface $e) {
248 trigger_error($e->getMessage(), \E_USER_WARNING);
249
250 return false;
251 }
252 }
253
254 if (\SEEK_END === $whence) {
255 $offset += $size;
256 }
257 }
258
259 if (0 <= $offset && $offset <= $size) {
260 $this->eof = false;
261 $this->offset = $offset;
262
263 return true;
264 }
265
266 return false;
267 }
268
269 /**
270 * @return resource|false
271 */
272 public function stream_cast(int $castAs)
273 {
274 if (\STREAM_CAST_FOR_SELECT === $castAs) {
275 $this->response->getHeaders(false);
276
277 return (\is_callable($this->handle) ? ($this->handle)() : $this->handle) ?? false;
278 }
279
280 return false;
281 }
282
283 public function stream_stat(): array
284 {
285 try {
286 $headers = $this->response->getHeaders(false);
287 } catch (ExceptionInterface $e) {
288 trigger_error($e->getMessage(), \E_USER_WARNING);
289 $headers = [];
290 }
291
292 return [
293 'dev' => 0,
294 'ino' => 0,
295 'mode' => 33060,
296 'nlink' => 0,
297 'uid' => 0,
298 'gid' => 0,
299 'rdev' => 0,
300 'size' => (int) ($headers['content-length'][0] ?? -1),
301 'atime' => 0,
302 'mtime' => strtotime($headers['last-modified'][0] ?? '') ?: 0,
303 'ctime' => 0,
304 'blksize' => 0,
305 'blocks' => 0,
306 ];
307 }
308
309 private function __construct()
310 {
311 }
312 }