Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 162d5a8

Browse files
feature #30752 [HttpClient] use "nyholm/psr7" by default in Psr18Client (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [HttpClient] use "nyholm/psr7" by default in Psr18Client | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This makes the factory arguments of `Psr18Client` optional, with a fallback to using `Nyholm\Psr7\Factory\Psr17Factory` when no factories are provided. Commits ------- f2222e4 [HttpClient] use "nyholm/psr7" by default in Psr18Client
2 parents efb6414 + f2222e4 commit 162d5a8

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Symfony/Component/HttpClient/Psr18Client.php

Lines changed: 20 additions & 3 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 = null, ResponseFactoryInterface $responseFactory = null, StreamFactoryInterface $streamFactory = null)
4247
{
43-
$this->client = $client;
48+
$this->client = $client ?? HttpClient::create();
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)