2019-03-08 16:59:13 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpClient ;
2020-01-09 07:05:19 +00:00
use Psr\Log\LoggerAwareInterface ;
use Psr\Log\LoggerInterface ;
2020-01-20 18:55:11 +00:00
use Symfony\Component\HttpClient\Response\TraceableResponse ;
2019-03-08 16:59:13 +00:00
use Symfony\Contracts\HttpClient\HttpClientInterface ;
use Symfony\Contracts\HttpClient\ResponseInterface ;
use Symfony\Contracts\HttpClient\ResponseStreamInterface ;
2019-11-23 15:38:53 +00:00
use Symfony\Contracts\Service\ResetInterface ;
2019-03-08 16:59:13 +00:00
/**
* @ author Jérémy Romey < jeremy @ free - agent . fr >
*/
2020-01-09 07:05:19 +00:00
final class TraceableHttpClient implements HttpClientInterface , ResetInterface , LoggerAwareInterface
2019-03-08 16:59:13 +00:00
{
private $client ;
private $tracedRequests = [];
public function __construct ( HttpClientInterface $client )
{
$this -> client = $client ;
}
/**
* { @ inheritdoc }
*/
public function request ( string $method , string $url , array $options = []) : ResponseInterface
{
2020-02-02 17:36:34 +00:00
$content = null ;
2019-03-08 16:59:13 +00:00
$traceInfo = [];
$this -> tracedRequests [] = [
'method' => $method ,
'url' => $url ,
'options' => $options ,
'info' => & $traceInfo ,
2020-01-20 18:55:11 +00:00
'content' => & $content ,
2019-03-08 16:59:13 +00:00
];
$onProgress = $options [ 'on_progress' ] ? ? null ;
$options [ 'on_progress' ] = function ( int $dlNow , int $dlSize , array $info ) use ( & $traceInfo , $onProgress ) {
$traceInfo = $info ;
if ( null !== $onProgress ) {
$onProgress ( $dlNow , $dlSize , $info );
}
};
2020-01-20 18:55:11 +00:00
return new TraceableResponse ( $this -> client , $this -> client -> request ( $method , $url , $options ), $content );
2019-03-08 16:59:13 +00:00
}
/**
* { @ inheritdoc }
*/
public function stream ( $responses , float $timeout = null ) : ResponseStreamInterface
{
2020-01-20 18:55:11 +00:00
if ( $responses instanceof TraceableResponse ) {
$responses = [ $responses ];
} elseif ( ! is_iterable ( $responses )) {
2020-03-25 21:29:06 +00:00
throw new \TypeError ( sprintf ( '"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.' , __METHOD__ , get_debug_type ( $responses )));
2020-01-20 18:55:11 +00:00
}
return $this -> client -> stream ( \Closure :: bind ( static function () use ( $responses ) {
foreach ( $responses as $k => $r ) {
if ( ! $r instanceof TraceableResponse ) {
2020-03-25 21:29:06 +00:00
throw new \TypeError ( sprintf ( '"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.' , __METHOD__ , get_debug_type ( $r )));
2020-01-20 18:55:11 +00:00
}
yield $k => $r -> response ;
}
2020-03-25 21:29:06 +00:00
}, null , TraceableResponse :: class )(), $timeout );
2019-03-08 16:59:13 +00:00
}
public function getTracedRequests () : array
{
return $this -> tracedRequests ;
}
public function reset ()
{
2019-11-23 15:38:53 +00:00
if ( $this -> client instanceof ResetInterface ) {
$this -> client -> reset ();
}
2019-03-08 16:59:13 +00:00
$this -> tracedRequests = [];
}
2020-01-09 07:05:19 +00:00
/**
* { @ inheritdoc }
*/
public function setLogger ( LoggerInterface $logger ) : void
{
if ( $this -> client instanceof LoggerAwareInterface ) {
$this -> client -> setLogger ( $logger );
}
}
2019-03-08 16:59:13 +00:00
}