8000 [HttpClient] use "nyholm/psr7" by default in Psr18Client · symfony/symfony@0d90c47 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d90c47

Browse files
[HttpClient] use "nyholm/psr7" by default in Psr18Client
1 parent c30f462 commit 0d90c47

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Symfony/Component/HttpClient/Psr18Client.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Nyholm\Psr7\Factory\Psr17Factory;
1415
use Psr\Http\Client\ClientInterface;
1516
use Psr\Http\Client\NetworkExceptionInterface;
1617
use Psr\Http\Client\RequestExceptionInterface;
@@ -21,6 +22,10 @@
2122
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
2324

25+
if (!interface_exists(ClientInterface::class)) {
26+
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".');
27+
}
28+
2429
/**
2530
* An adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface.
2631
*
@@ -38,11 +43,23 @@ final class Psr18Client implements ClientInterface
3843
private $responseFactory;
3944
private $streamFactory;
4045

41-
public function __construct(HttpClientInterface $client, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
46+
public function __construct(HttpClientInterface $client, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null)
4247
{
4348
$this->client = $client;
4449
$this->responseFactory = $responseFactory;
45-
$this->streamFactory = $streamFactory;
50+
$this->streamFactory = $streamFactory ?? ($responseFactory instanceof StreamFactoryInterface ? $responseFactory : null);
51+
52+
if (null !== $this->responseFactory && null !== $this->streamFactory) {
53+
return;
54+
}
55+
56+
if (!class_exists(Psr17Factory::class)) {
57+
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".');
58+
}
59+
60+
$psr17Factory = new Psr17Factory();
61+
$this->responseFactory = $this->responseFactory ?? $psr17Factory;
62+
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
4663
}
4764

4865
public function sendRequest(RequestInterface $request): ResponseInterface

0 commit comments

Comments
 (0)
0