TraceableHttpClient.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpClient\Response\ResponseStream;
  14. use Symfony\Component\HttpClient\Response\TraceableResponse;
  15. use Symfony\Component\Stopwatch\Stopwatch;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  19. use Symfony\Contracts\Service\ResetInterface;
  20. /**
  21. * @author Jérémy Romey <jeremy@free-agent.fr>
  22. */
  23. final class TraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface
  24. {
  25. private $client;
  26. private $stopwatch;
  27. private \ArrayObject $tracedRequests;
  28. public function __construct(HttpClientInterface $client, Stopwatch $stopwatch = null)
  29. {
  30. $this->client = $client;
  31. $this->stopwatch = $stopwatch;
  32. $this->tracedRequests = new \ArrayObject();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function request(string $method, string $url, array $options = []): ResponseInterface
  38. {
  39. $content = null;
  40. $traceInfo = [];
  41. $this->tracedRequests[] = [
  42. 'method' => $method,
  43. 'url' => $url,
  44. 'options' => $options,
  45. 'info' => &$traceInfo,
  46. 'content' => &$content,
  47. ];
  48. $onProgress = $options['on_progress'] ?? null;
  49. if (false === ($options['extra']['trace_content'] ?? true)) {
  50. unset($content);
  51. $content = false;
  52. }
  53. $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) {
  54. $traceInfo = $info;
  55. if (null !== $onProgress) {
  56. $onProgress($dlNow, $dlSize, $info);
  57. }
  58. };
  59. return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content, null === $this->stopwatch ? null : $this->stopwatch->start("$method $url", 'http_client'));
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
  65. {
  66. if ($responses instanceof TraceableResponse) {
  67. $responses = [$responses];
  68. }
  69. return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout));
  70. }
  71. public function getTracedRequests(): array
  72. {
  73. return $this->tracedRequests->getArrayCopy();
  74. }
  75. public function reset()
  76. {
  77. if ($this->client instanceof ResetInterface) {
  78. $this->client->reset();
  79. }
  80. $this->tracedRequests->exchangeArray([]);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setLogger(LoggerInterface $logger): void
  86. {
  87. if ($this->client instanceof LoggerAwareInterface) {
  88. $this->client->setLogger($logger);
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function withOptions(array $options): static
  95. {
  96. $clone = clone $this;
  97. $clone->client = $this->client->withOptions($options);
  98. return $clone;
  99. }
  100. }