From f2222e4bf5ed676f426aa1a4dcbce3aeb3892dd3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 28 Mar 2019 16:19:14 +0100 Subject: [PATCH] [HttpClient] use "nyholm/psr7" by default in Psr18Client --- .../Component/HttpClient/Psr18Client.php | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpClient/Psr18Client.php b/src/Symfony/Component/HttpClient/Psr18Client.php index 9c00940339..17a3d3bd11 100644 --- a/src/Symfony/Component/HttpClient/Psr18Client.php +++ b/src/Symfony/Component/HttpClient/Psr18Client.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpClient; +use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Client\ClientInterface; use Psr\Http\Client\NetworkExceptionInterface; use Psr\Http\Client\RequestExceptionInterface; @@ -21,6 +22,10 @@ use Psr\Http\Message\StreamFactoryInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +if (!interface_exists(ClientInterface::class)) { + throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-client" package is not installed. Try running "composer require psr/http-client".'); +} + /** * An adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface. * @@ -38,11 +43,23 @@ final class Psr18Client implements ClientInterface private $responseFactory; private $streamFactory; - public function __construct(HttpClientInterface $client, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) + public function __construct(HttpClientInterface $client = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null) { - $this->client = $client; + $this->client = $client ?? HttpClient::create(); $this->responseFactory = $responseFactory; - $this->streamFactory = $streamFactory; + $this->streamFactory = $streamFactory ?? ($responseFactory instanceof StreamFactoryInterface ? $responseFactory : null); + + if (null !== $this->responseFactory && null !== $this->streamFactory) { + return; + } + + if (!class_exists(Psr17Factory::class)) { + throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".'); + } + + $psr17Factory = new Psr17Factory(); + $this->responseFactory = $this->responseFactory ?? $psr17Factory; + $this->streamFactory = $this->streamFactory ?? $psr17Factory; } public function sendRequest(RequestInterface $request): ResponseInterface