* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpClient; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Response\ResponseStream; use Symfony\Component\HttpClient\Response\TraceableResponse; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseStreamInterface; use Symfony\Contracts\Service\ResetInterface; /** * @author Jérémy Romey */ final class TraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface { /** * @var \Symfony\Contracts\HttpClient\HttpClientInterface */ private $client; /** * @var \Symfony\Component\Stopwatch\Stopwatch|null */ private $stopwatch; /** * @var \ArrayObject */ private $tracedRequests; /** * @param \Symfony\Contracts\HttpClient\HttpClientInterface $client * @param \Symfony\Component\Stopwatch\Stopwatch|null $stopwatch */ public function __construct($client, $stopwatch = null) { $this->client = $client; $this->stopwatch = $stopwatch; $this->tracedRequests = new \ArrayObject(); } /** * @param string $method * @param string $url * @param mixed[] $options */ public function request($method, $url, $options = []) { $content = null; $traceInfo = []; $this->tracedRequests[] = [ 'method' => $method, 'url' => $url, 'options' => $options, 'info' => &$traceInfo, 'content' => &$content, ]; $onProgress = $options['on_progress'] ?? null; if (false === ($options['extra']['trace_content'] ?? true)) { unset($content); $content = false; } $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) { $traceInfo = $info; if (null !== $onProgress) { $onProgress($dlNow, $dlSize, $info); } }; return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content, ($stopwatch = $this->stopwatch) ? $stopwatch->start("$method $url", 'http_client') : null); } /** * @param \Symfony\Contracts\HttpClient\ResponseInterface|mixed[] $responses * @param float|null $timeout */ public function stream($responses, $timeout = null) { if ($responses instanceof TraceableResponse) { $responses = [$responses]; } return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout)); } public function getTracedRequests() { return $this->tracedRequests->getArrayCopy(); } public function reset() { if ($this->client instanceof ResetInterface) { $this->client->reset(); } $this->tracedRequests->exchangeArray([]); } /** * @param \Psr\Log\LoggerInterface $logger */ public function setLogger($logger) { if ($this->client instanceof LoggerAwareInterface) { $this->client->setLogger($logger); } } /** * @return $this * @param mixed[] $options */ public function withOptions($options) { $clone = clone $this; $clone->client = $this->client->withOptions($options); return $clone; } }