AsyncContext.php 5.6 KB

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