You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #50225 [HttpClient] Favor php-http/discovery instead of nyholm/psr7 (nicolas-grekas)
This PR was merged into the 6.3 branch.
Discussion
----------
[HttpClient] Favor php-http/discovery instead of nyholm/psr7
| Q | A
| ------------- | ---
| Branch? | 6.3
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
This PR fine-tunes the DX related to using `HttplugClient` and `Psr18Client`.
By favoring `php-http/discovery`, we build on [its latest capabilities](https://github.com/php-http/discovery/releases) to use whatever implementation might be installed and preferred by the app.
Of course, we keep `nyholm/psr7` as a privileged PSR-7 implementation so that when it's installed but `php-http/discovery` is not, things keep working as usual.
The deprecated interfaces from `php-http/message-factory` are also moved to a new conditional and internal `LegacyHttplugInterface`, so that we can make this dependency optional.
Commits
-------
5733ff7 [HttpClient] Favor php-http/discovery instead of nyholm/psr7
thrownew \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/httplug" package is not installed. Try running "composer require php-http/httplug".');
47
-
}
48
-
49
-
if (!interface_exists(RequestFactory::class)) {
50
-
thrownew \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/message-factory" package is not installed. Try running "composer require php-http/message-factory".');
42
+
if (!interface_exists(HttpAsyncClient::class)) {
43
+
thrownew \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/httplug" package is not installed. Try running "php-http/discovery php-http/async-client-implementation:*".');
51
44
}
52
45
53
46
if (!interface_exists(RequestFactoryInterface::class)) {
54
-
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".');
55
-
}
56
-
57
-
if (!interface_exists(ClientInterface::class)) {
58
-
thrownew \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "psr/http-client" package is not installed. Try running "composer require psr/http-client".');
47
+
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as the "psr/http-factory" package is not installed. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
59
48
}
60
49
61
50
/**
62
51
* An adapter to turn a Symfony HttpClientInterface into an Httplug client.
63
52
*
64
-
* Run "composer require nyholm/psr7" to install an efficient implementation of response
65
-
* and stream factories with flex-provided autowiring aliases.
53
+
* In comparison to Psr18Client, this client supports asynchronous requests.
54
+
*
55
+
* Run "composer require php-http/discovery php-http/async-client-implementation:*"
@@ -86,17 +77,16 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
86
77
$this->promisePool = class_exists(Utils::class) ? new \SplObjectStorage() : null;
87
78
88
79
if (null === $responseFactory || null === $streamFactory) {
89
-
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
90
-
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been found. Try running "composer require nyholm/psr7".', 0, $e);
99
-
}
88
+
$responseFactory ??= $psr17Factory;
89
+
$streamFactory ??= $psr17Factory;
100
90
}
101
91
102
92
$this->responseFactory = $responseFactory;
@@ -170,12 +160,12 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
170
160
}
171
161
if ($this->responseFactoryinstanceof RequestFactoryInterface) {
thrownew \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
168
+
thrownew \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
179
169
}
180
170
181
171
$request = $request
@@ -245,15 +235,15 @@ public function createUri($uri = ''): UriInterface
thrownew \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
242
+
if (class_exists(Uri::class)) {
243
+
returnnewUri($uri);
244
+
}
245
+
246
+
thrownew \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
if (!interface_exists(RequestFactoryInterface::class)) {
37
-
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".');
36
+
if (!interface_exists(ClientInterface::class)) {
37
+
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-client" package is not installed. Try running "composer require php-http/discovery psr/http-client-implementation:*".');
38
38
}
39
39
40
-
if (!interface_exists(ClientInterface::class)) {
41
-
thrownew \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".');
40
+
if (!interface_exists(RequestFactoryInterface::class)) {
41
+
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
42
42
}
43
43
44
44
/**
45
45
* An adapter to turn a Symfony HttpClientInterface into a PSR-18 ClientInterface.
46
46
*
47
-
* Run "composer require psr/http-client" to install the base ClientInterface. Run
48
-
* "composer require nyholm/psr7" to install an efficient implementation of response
49
-
* and stream factories with flex-provided autowiring aliases.
47
+
* Run "composer require php-http/discovery psr/http-client-implementation:*"
48
+
* to get the required dependencies.
50
49
*
51
50
* @author Nicolas Grekas <p@tchwork.com>
52
51
*/
@@ -62,17 +61,16 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
if (null === $responseFactory || null === $streamFactory) {
65
-
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
66
-
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require php-http/discovery psr/http-factory-implementation:*".');
thrownew \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been found. Try running "composer require nyholm/psr7".', 0, $e);
75
-
}
72
+
$responseFactory ??= $psr17Factory;
73
+
$streamFactory ??= $psr17Factory;
76
74
}
77
75
78
76
$this->responseFactory = $responseFactory;
@@ -142,15 +140,15 @@ public function createRequest(string $method, $uri): RequestInterface
thrownew \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
147
+
if (class_exists(Request::class)) {
148
+
returnnewRequest($method, $uri);
149
+
}
150
+
151
+
thrownew \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
thrownew \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
185
+
if (class_exists(Uri::class)) {
186
+
returnnewUri($uri);
187
+
}
188
+
189
+
thrownew \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
0 commit comments