Skip to content

Commit 6e0cb58

Browse files
[HttpClient] try using php-http/discovery when nyholm/psr7 is not installed
1 parent 211c651 commit 6e0cb58

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/Symfony/Component/HttpClient/HttplugClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Http\Client\Exception\RequestException;
1717
use Http\Client\HttpAsyncClient;
1818
use Http\Client\HttpClient as HttplugInterface;
19+
use Http\Discovery\Psr17FactoryDiscovery;
1920
use Http\Message\RequestFactory;
2021
use Http\Message\StreamFactory;
2122
use Http\Message\UriFactory;
@@ -70,13 +71,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
7071
$this->promisePool = \function_exists('GuzzleHttp\Promise\queue') ? new \SplObjectStorage() : null;
7172

7273
if (null === $this->responseFactory || null === $this->streamFactory) {
73-
if (!class_exists(Psr17Factory::class)) {
74+
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
7475
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".');
7576
}
7677

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();
8081
}
8182

8283
$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,
144145
{
145146
if ($this->responseFactory instanceof RequestFactoryInterface) {
146147
$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)) {
150149
$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__));
151154
}
152155

153156
$request = $request
@@ -199,11 +202,15 @@ public function createUri($uri): UriInterface
199202
return $this->responseFactory->createUri($uri);
200203
}
201204

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);
204211
}
205212

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__));
207214
}
208215

209216
public function __destruct()

src/Symfony/Component/HttpClient/Psr18Client.php

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

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Http\Discovery\Psr17FactoryDiscovery;
1415
use Nyholm\Psr7\Factory\Psr17Factory;
1516
use Nyholm\Psr7\Request;
1617
use Nyholm\Psr7\Uri;
@@ -63,13 +64,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
6364
return;
6465
}
6566

66-
if (!class_exists(Psr17Factory::class)) {
67+
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
6768
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".');
6869
}
6970

70-
$psr17Factory = new Psr17Factory();
71-
$this->responseFactory = $this->responseFactory ?? $psr17Factory;
72-
$this->streamFactory = $this->streamFactory ?? $psr17Factory;
71+
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
72+
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
73+
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
7374
}
7475

7576
/**
@@ -124,11 +125,15 @@ public function createRequest(string $method, $uri): RequestInterface
124125
return $this->responseFactory->createRequest($method, $uri);
125126
}
126127

127-
if (!class_exists(Request::class)) {
128-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
128+
if (class_exists(Request::class)) {
129+
return new Request($method, $uri);
129130
}
130131

131-
return new Request($method, $uri);
132+
if (class_exists(Psr17FactoryDiscovery::class)) {
133+
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
134+
}
135+
136+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
132137
}
133138

134139
/**
@@ -170,11 +175,15 @@ public function createUri(string $uri = ''): UriInterface
170175
return $this->responseFactory->createUri($uri);
171176
}
172177

173-
if (!class_exists(Uri::class)) {
174-
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
178+
if (class_exists(Uri::class)) {
179+
return new Uri($uri);
180+
}
181+
182+
if (class_exists(Psr17FactoryDiscovery::class)) {
183+
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
175184
}
176185

177-
return new Uri($uri);
186+
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
178187
}
179188
}
180189

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy