| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Psr\Log\LoggerAwareInterface; |
| 15 |
use Psr\Log\LoggerInterface; |
| 16 |
use Symfony\Component\HttpClient\Response\ResponseStream; |
| 17 |
use Symfony\Component\HttpClient\Response\TraceableResponse; |
| 18 |
use Symfony\Component\Stopwatch\Stopwatch; |
| 19 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 20 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 21 |
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 22 |
use Symfony\Contracts\Service\ResetInterface; |
| 23 |
|
| 24 |
/** |
| 25 |
* @author Jérémy Romey <[email protected]> |
| 26 |
*/ |
| 27 |
final class TraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface |
| 28 |
{ |
| 29 |
private $client; |
| 30 |
private $stopwatch; |
| 31 |
private $tracedRequests; |
| 32 |
|
| 33 |
public function __construct(HttpClientInterface $client, Stopwatch $stopwatch = null) |
| 34 |
{ |
| 35 |
$this->client = $client; |
| 36 |
$this->stopwatch = $stopwatch; |
| 37 |
$this->tracedRequests = new \ArrayObject(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritdoc} |
| 42 |
*/ |
| 43 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 44 |
{ |
| 45 |
$content = null; |
| 46 |
$traceInfo = []; |
| 47 |
$this->tracedRequests[] = [ |
| 48 |
'method' => $method, |
| 49 |
'url' => $url, |
| 50 |
'options' => $options, |
| 51 |
'info' => &$traceInfo, |
| 52 |
'content' => &$content, |
| 53 |
]; |
| 54 |
$onProgress = $options['on_progress'] ?? null; |
| 55 |
|
| 56 |
if (false === ($options['extra']['trace_content'] ?? true)) { |
| 57 |
unset($content); |
| 58 |
$content = false; |
| 59 |
} |
| 60 |
|
| 61 |
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) { |
| 62 |
$traceInfo = $info; |
| 63 |
|
| 64 |
if (null !== $onProgress) { |
| 65 |
$onProgress($dlNow, $dlSize, $info); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content, null === $this->stopwatch ? null : $this->stopwatch->start("$method $url", 'http_client')); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* {@inheritdoc} |
| 74 |
*/ |
| 75 |
public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 76 |
{ |
| 77 |
if ($responses instanceof TraceableResponse) { |
| 78 |
$responses = [$responses]; |
| 79 |
} elseif (!is_iterable($responses)) { |
| 80 |
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses))); |
| 81 |
} |
| 82 |
|
| 83 |
return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout)); |
| 84 |
} |
| 85 |
|
| 86 |
public function getTracedRequests(): array |
| 87 |
{ |
| 88 |
return $this->tracedRequests->getArrayCopy(); |
| 89 |
} |
| 90 |
|
| 91 |
public function reset() |
| 92 |
{ |
| 93 |
if ($this->client instanceof ResetInterface) { |
| 94 |
$this->client->reset(); |
| 95 |
} |
| 96 |
|
| 97 |
$this->tracedRequests->exchangeArray([]); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* {@inheritdoc} |
| 102 |
*/ |
| 103 |
public function setLogger(LoggerInterface $logger): void |
| 104 |
{ |
| 105 |
if ($this->client instanceof LoggerAwareInterface) { |
| 106 |
$this->client->setLogger($logger); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* {@inheritdoc} |
| 112 |
*/ |
| 113 |
public function withOptions(array $options): self |
| 114 |
{ |
| 115 |
$clone = clone $this; |
| 116 |
$clone->client = $this->client->withOptions($options); |
| 117 |
|
| 118 |
return $clone; |
| 119 |
} |
| 120 |
} |
| 121 |
|