AsyncContext.php 4.9 KB

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