Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/Response/AsyncContext.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\Component\HttpClient\Chunk\DataChunk;
15 use Symfony\Component\HttpClient\Chunk\LastChunk;
16 use Symfony\Component\HttpClient\Exception\TransportException;
17 use Symfony\Contracts\HttpClient\ChunkInterface;
18 use Symfony\Contracts\HttpClient\HttpClientInterface;
19 use Symfony\Contracts\HttpClient\ResponseInterface;
20
21 /**
22 * A DTO to work with AsyncResponse.
23 *
24 * @author Nicolas Grekas <[email protected]>
25 */
26 final class AsyncContext
27 {
28 /** @var callable|null */
29 private $passthru;
30 private ResponseInterface $response;
31 private array $info = [];
32
33 /**
34 * @param resource|null $content
35 */
36 public function __construct(
37 ?callable &$passthru,
38 private HttpClientInterface $client,
39 ResponseInterface &$response,
40 array &$info,
41 private $content,
42 private int $offset,
43 ) {
44 $this->passthru = &$passthru;
45 $this->response = &$response;
46 $this->info = &$info;
47 }
48
49 /**
50 * Returns the HTTP status without consuming the response.
51 */
52 public function getStatusCode(): int
53 {
54 return $this->response->getInfo('http_code');
55 }
56
57 /**
58 * Returns the headers without consuming the response.
59 */
60 public function getHeaders(): array
61 {
62 $headers = [];
63
64 foreach ($this->response->getInfo('response_headers') as $h) {
65 if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? ([123456789]\d\d)(?: |$)#', $h, $m)) {
66 $headers = [];
67 } elseif (2 === \count($m = explode(':', $h, 2))) {
68 $headers[strtolower($m[0])][] = ltrim($m[1]);
69 }
70 }
71
72 return $headers;
73 }
74
75 /**
76 * @return resource|null The PHP stream resource where the content is buffered, if it is
77 */
78 public function getContent()
79 {
80 return $this->content;
81 }
82
83 /**
84 * Creates a new chunk of content.
85 */
86 public function createChunk(string $data): ChunkInterface
87 {
88 return new DataChunk($this->offset, $data);
89 }
90
91 /**
92 * Pauses the request for the given number of seconds.
93 */
94 public function pause(float $duration): void
95 {
96 if (\is_callable($pause = $this->response->getInfo('pause_handler'))) {
97 $pause($duration);
98 } elseif (0 < $duration) {
99 usleep((int) (1E6 * $duration));
100 }
101 }
102
103 /**
104 * Cancels the request and returns the last chunk to yield.
105 */
106 public function cancel(): ChunkInterface
107 {
108 $this->info['canceled'] = true;
109 $this->info['error'] = 'Response has been canceled.';
110 $this->response->cancel();
111
112 return new LastChunk();
113 }
114
115 /**
116 * Returns the current info of the response.
117 */
118 public function getInfo(?string $type = null): mixed
119 {
120 if (null !== $type) {
121 return $this->info[$type] ?? $this->response->getInfo($type);
122 }
123
124 return $this->info + $this->response->getInfo();
125 }
126
127 /**
128 * Attaches an info to the response.
129 *
130 * @return $this
131 */
132 public function setInfo(string $type, mixed $value): static
133 {
134 if ('canceled' === $type && $value !== $this->info['canceled']) {
135 throw new \LogicException('You cannot set the "canceled" info directly.');
136 }
137
138 if (null === $value) {
139 unset($this->info[$type]);
140 } else {
141 $this->info[$type] = $value;
142 }
143
144 return $this;
145 }
146
147 /**
148 * Returns the currently processed response.
149 */
150 public function getResponse(): ResponseInterface
151 {
152 return $this->response;
153 }
154
155 /**
156 * Replaces the currently processed response by doing a new request.
157 */
158 public function replaceRequest(string $method, string $url, array $options = []): ResponseInterface
159 {
160 $this->info['previous_info'][] = $info = $this->response->getInfo();
161 if (null !== $onProgress = $options['on_progress'] ?? null) {
162 $thisInfo = &$this->info;
163 $options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
164 $onProgress($dlNow, $dlSize, $thisInfo + $info);
165 };
166 }
167 if (0 < ($info['max_duration'] ?? 0) && 0 < ($info['total_time'] ?? 0)) {
168 if (0 >= $options['max_duration'] = $info['max_duration'] - $info['total_time']) {
169 throw new TransportException(\sprintf('Max duration was reached for "%s".', $info['url']));
170 }
171 }
172
173 return $this->response = $this->client->request($method, $url, ['buffer' => false] + $options);
174 }
175
176 /**
177 * Replaces the currently processed response by another one.
178 */
179 public function replaceResponse(ResponseInterface $response): ResponseInterface
180 {
181 $this->info['previous_info'][] = $this->response->getInfo();
182
183 return $this->response = $response;
184 }
185
186 /**
187 * Replaces or removes the chunk filter iterator.
188 *
189 * @param ?callable(ChunkInterface, self): ?\Iterator $passthru
190 */
191 public function passthru(?callable $passthru = null): void
192 {
193 $this->passthru = $passthru ?? static function ($chunk, $context) {
194 $context->passthru = null;
195
196 yield $chunk;
197 };
198 }
199 }