Skip to content

Commit

Permalink
feature #47950 [HttpClient] Add support for "friendsofphp/well-known-…
Browse files Browse the repository at this point in the history
…implementations" (nicolas-grekas)

This PR was merged into the 6.2 branch.

Discussion
----------

[HttpClient] Add support for "friendsofphp/well-known-implementations"

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

See https://github.com/FriendsOfPHP/well-known-implementations for what this is about.

This provides an alternative auto-discovery mechanism to php-http/discovery.

Commits
-------

b814ce8 [HttpClient] Add support for "friendsofphp/well-known-implementations"
  • Loading branch information
fabpot committed Oct 22, 2022
2 parents d7fe6a0 + b814ce8 commit 9effc28
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Make `HttplugClient` implement `Psr\Http\Message\RequestFactoryInterface`, `StreamFactoryInterface` and `UriFactoryInterface`
* Deprecate implementing `Http\Message\RequestFactory`, `StreamFactory` and `UriFactory` on `HttplugClient`
* Add `withOptions()` to `HttplugClient` and `Psr18Client`
* Add support for "friendsofphp/well-known-implementations"

6.1
---
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/HttpClient/HttplugClient.php
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\HttpClient;

use FriendsOfPHP\WellKnownImplementations\WellKnownPsr17Factory;
use FriendsOfPHP\WellKnownImplementations\WellKnownPsr7Request;
use FriendsOfPHP\WellKnownImplementations\WellKnownPsr7Uri;
use GuzzleHttp\Promise\Promise as GuzzlePromise;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Promise\Utils;
Expand Down Expand Up @@ -81,12 +84,12 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
$this->promisePool = class_exists(Utils::class) ? new \SplObjectStorage() : null;

if (null === $responseFactory || null === $streamFactory) {
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
if (!class_exists(Psr17Factory::class) && !class_exists(WellKnownPsr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
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".');
}

try {
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : (class_exists(WellKnownPsr17Factory::class, false) ? new WellKnownPsr17Factory() : null);
$responseFactory ??= $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
$streamFactory ??= $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
} catch (NotFoundException $e) {
Expand Down Expand Up @@ -167,6 +170,8 @@ public function createRequest($method, $uri, array $headers = [], $body = null,
$request = $this->responseFactory->createRequest($method, $uri);
} elseif (class_exists(Request::class)) {
$request = new Request($method, $uri);
} elseif (class_exists(WellKnownPsr7Request::class)) {
$request = new WellKnownPsr7Request($method, $uri);
} elseif (class_exists(Psr17FactoryDiscovery::class)) {
$request = Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
} else {
Expand Down Expand Up @@ -244,6 +249,10 @@ public function createUri($uri = ''): UriInterface
return new Uri($uri);
}

if (class_exists(WellKnownPsr7Uri::class)) {
return new WellKnownPsr7Uri($uri);
}

if (class_exists(Psr17FactoryDiscovery::class)) {
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
}
Expand Down
15 changes: 13 additions & 2 deletions src/Symfony/Component/HttpClient/Psr18Client.php
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\HttpClient;

use FriendsOfPHP\WellKnownImplementations\WellKnownPsr17Factory;
use FriendsOfPHP\WellKnownImplementations\WellKnownPsr7Request;
use FriendsOfPHP\WellKnownImplementations\WellKnownPsr7Uri;
use Http\Discovery\Exception\NotFoundException;
use Http\Discovery\Psr17FactoryDiscovery;
use Nyholm\Psr7\Factory\Psr17Factory;
Expand Down Expand Up @@ -62,12 +65,12 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
$streamFactory ??= $responseFactory instanceof StreamFactoryInterface ? $responseFactory : null;

if (null === $responseFactory || null === $streamFactory) {
if (!class_exists(Psr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
if (!class_exists(Psr17Factory::class) && !class_exists(WellKnownPsr17Factory::class) && !class_exists(Psr17FactoryDiscovery::class)) {
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".');
}

try {
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : (class_exists(WellKnownPsr17Factory::class, false) ? new WellKnownPsr17Factory() : null);
$responseFactory ??= $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
$streamFactory ??= $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
} catch (NotFoundException $e) {
Expand Down Expand Up @@ -141,6 +144,10 @@ public function createRequest(string $method, $uri): RequestInterface
return new Request($method, $uri);
}

if (class_exists(WellKnownPsr7Request::class)) {
return new WellKnownPsr7Request($method, $uri);
}

if (class_exists(Psr17FactoryDiscovery::class)) {
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
}
Expand Down Expand Up @@ -179,6 +186,10 @@ public function createUri(string $uri = ''): UriInterface
return new Uri($uri);
}

if (class_exists(WellKnownPsr7Uri::class)) {
return new WellKnownPsr7Uri($uri);
}

if (class_exists(Psr17FactoryDiscovery::class)) {
return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
}
Expand Down

0 comments on commit 9effc28

Please sign in to comment.