|
16 | 16 | use Http\Client\Exception\RequestException;
|
17 | 17 | use Http\Client\HttpAsyncClient;
|
18 | 18 | use Http\Client\HttpClient as HttplugInterface;
|
| 19 | +use Http\Discovery\Psr17FactoryDiscovery; |
19 | 20 | use Http\Message\RequestFactory;
|
20 | 21 | use Http\Message\StreamFactory;
|
21 | 22 | use Http\Message\UriFactory;
|
@@ -70,13 +71,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
|
70 | 71 | $this->promisePool = \function_exists('GuzzleHttp\Promise\queue') ? new \SplObjectStorage() : null;
|
71 | 72 |
|
72 | 73 | if (null === $this->responseFactory || null === $this->streamFactory) {
|
73 |
| - if (!class_exists(Psr17Factory::class)) { |
| 74 | + if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) { |
74 | 75 | throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
|
75 | 76 | }
|
76 | 77 |
|
77 |
| - $psr17Factory = new Psr17Factory(); |
78 |
| - $this->responseFactory = $this->responseFactory ?? $psr17Factory; |
79 |
| - $this->streamFactory = $this->streamFactory ?? $psr17Factory; |
| 78 | + $psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null; |
| 79 | + $this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory(); |
| 80 | + $this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory(); |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | $this->waitLoop = new HttplugWaitLoop($this->client, $this->promisePool, $this->responseFactory, $this->streamFactory);
|
@@ -144,10 +145,12 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
|
144 | 145 | {
|
145 | 146 | if ($this->responseFactory instanceof RequestFactoryInterface) {
|
146 | 147 | $request = $this->responseFactory->createRequest($method, $uri);
|
147 |
| - } elseif (!class_exists(Request::class)) { |
148 |
| - throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
149 |
| - } else { |
| 148 | + } elseif (class_exists(Request::class)) { |
150 | 149 | $request = new Request($method, $uri);
|
| 150 | + } elseif (class_exists(Psr17FactoryDiscovery::class)) { |
| 151 | + $request = Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri); |
| 152 | + } else { |
| 153 | + throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
151 | 154 | }
|
152 | 155 |
|
153 | 156 | $request = $request
|
@@ -199,11 +202,15 @@ public function createUri($uri): UriInterface
|
199 | 202 | return $this->responseFactory->createUri($uri);
|
200 | 203 | }
|
201 | 204 |
|
202 |
| - if (!class_exists(Uri::class)) { |
203 |
| - throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
| 205 | + if (class_exists(Uri::class)) { |
| 206 | + return new Uri($uri); |
| 207 | + } |
| 208 | + |
| 209 | + if (class_exists(Psr17FactoryDiscovery::class)) { |
| 210 | + return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri); |
204 | 211 | }
|
205 | 212 |
|
206 |
| - return new Uri($uri); |
| 213 | + throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__)); |
207 | 214 | }
|
208 | 215 |
|
209 | 216 | public function __destruct()
|
|
0 commit comments