AsyncContext.php 5.5 KB

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