From d3ef8428902eff86aaac263016b3257fd22aa768 Mon Sep 17 00:00:00 2001 From: thePanz Date: Sat, 9 Dec 2023 13:11:37 +0100 Subject: [PATCH] WIP: Add suport for Amphp PSR18 Client Note: the use Amp\Http\Client\Psr7\PsrHttpClient is still a WIP, and the support for PSR-18 has been added only on the v2 branch Only on that branch the PsrHttpClient is implementing the ClientInterface Note: The library only supports the psr/http-message:v1 contract --- src/Strategy/CommonClassesStrategy.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Strategy/CommonClassesStrategy.php b/src/Strategy/CommonClassesStrategy.php index 0fa4240..a04bf0d 100644 --- a/src/Strategy/CommonClassesStrategy.php +++ b/src/Strategy/CommonClassesStrategy.php @@ -2,6 +2,9 @@ namespace Http\Discovery\Strategy; +use Amp\Http\Client\HttpClientBuilder as AmphpHttpClientBuilder; +use Amp\Http\Client\Psr7\PsrHttpClient as AmphpPsrHttpClient; +use Amp\Http\Client\Psr7\PsrAdapter as AmphpPsrAdapter; use GuzzleHttp\Client as GuzzleHttp; use GuzzleHttp\Promise\Promise; use GuzzleHttp\Psr7\Request as GuzzleRequest; @@ -35,6 +38,7 @@ use Nyholm\Psr7\Factory\HttplugFactory as NyholmHttplugFactory; use Psr\Http\Client\ClientInterface as Psr18Client; use Psr\Http\Message\RequestFactoryInterface as Psr17RequestFactory; +use Psr\Http\Message\ResponseFactoryInterface as Psr17ResponseFactory; use Slim\Http\Request as SlimRequest; use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplug; use Symfony\Component\HttpClient\Psr18Client as SymfonyPsr18; @@ -98,6 +102,10 @@ final class CommonClassesStrategy implements DiscoveryStrategy 'class' => [self::class, 'symfonyPsr18Instantiate'], 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class], ], + [ + 'class' => [self::class, 'amphpPsr18Instantiate'], + 'condition' => [[self::class, 'isAmphpImplementingPsr18'], AmphpPsrHttpClient::class, Psr17RequestFactory::class, Psr17ResponseFactory::class], + ], [ 'class' => GuzzleHttp::class, 'condition' => [self::class, 'isGuzzleImplementingPsr18'], @@ -143,6 +151,20 @@ private static function getPsr18Candidates() return $candidates; } + public static function amphpPsr18Instantiate() + { + $httpClient = AmphpHttpClientBuilder::buildDefault(); + $psrAdapter = new AmphpPsrAdapter(Psr17FactoryDiscovery::findRequestFactory(), Psr17FactoryDiscovery::findResponseFactory()); + + return new AmphpPsrHttpClient($httpClient, $psrAdapter); + } + + public static function isAmphpImplementingPsr18() + { + // Amphp PsrHttpClient is implementing the interface only on the v2 branch + return is_subclass_of(AmphpPsrHttpClient::class, Psr18Client::class); + } + public static function buzzInstantiate() { return new \Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory());