TraceableHttpClient.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 $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($responses, float $timeout = null): ResponseStreamInterface
  65. {
  66. if ($responses instanceof TraceableResponse) {
  67. $responses = [$responses];
  68. } elseif (!is_iterable($responses)) {
  69. throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
  70. }
  71. return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout));
  72. }
  73. public function getTracedRequests(): array
  74. {
  75. return $this->tracedRequests->getArrayCopy();
  76. }
  77. public function reset()
  78. {
  79. if ($this->client instanceof ResetInterface) {
  80. $this->client->reset();
  81. }
  82. $this->tracedRequests->exchangeArray([]);
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function setLogger(LoggerInterface $logger): void
  88. {
  89. if ($this->client instanceof LoggerAwareInterface) {
  90. $this->client->setLogger($logger);
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function withOptions(array $options): self
  97. {
  98. $clone = clone $this;
  99. $clone->client = $this->client->withOptions($options);
  100. return $clone;
  101. }
  102. }