Skip to content

Commit

Permalink
WIP: Add suport for Amphp PSR18 Client
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thePanz committed Dec 9, 2023
1 parent 61e1a1e commit a232679
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Strategy/CommonClassesStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Http\Discovery\Strategy;

use Amp\Http\Client\HttpClientBuilder as AmphpHttpClientBuilder;
use Amp\Http\Client\Psr7\PsrAdapter as AmphpPsrAdapter;
use Amp\Http\Client\Psr7\PsrHttpClient as AmphpPsrHttpClient;
use GuzzleHttp\Client as GuzzleHttp;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit a232679

Please sign in to comment.