Skip to content

Commit a232679

Browse files
committed
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
1 parent 61e1a1e commit a232679

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: src/Strategy/CommonClassesStrategy.php

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Http\Discovery\Strategy;
44

5+
use Amp\Http\Client\HttpClientBuilder as AmphpHttpClientBuilder;
6+
use Amp\Http\Client\Psr7\PsrAdapter as AmphpPsrAdapter;
7+
use Amp\Http\Client\Psr7\PsrHttpClient as AmphpPsrHttpClient;
58
use GuzzleHttp\Client as GuzzleHttp;
69
use GuzzleHttp\Promise\Promise;
710
use GuzzleHttp\Psr7\Request as GuzzleRequest;
@@ -35,6 +38,7 @@
3538
use Nyholm\Psr7\Factory\HttplugFactory as NyholmHttplugFactory;
3639
use Psr\Http\Client\ClientInterface as Psr18Client;
3740
use Psr\Http\Message\RequestFactoryInterface as Psr17RequestFactory;
41+
use Psr\Http\Message\ResponseFactoryInterface as Psr17ResponseFactory;
3842
use Slim\Http\Request as SlimRequest;
3943
use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplug;
4044
use Symfony\Component\HttpClient\Psr18Client as SymfonyPsr18;
@@ -98,6 +102,10 @@ final class CommonClassesStrategy implements DiscoveryStrategy
98102
'class' => [self::class, 'symfonyPsr18Instantiate'],
99103
'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class],
100104
],
105+
[
106+
'class' => [self::class, 'amphpPsr18Instantiate'],
107+
'condition' => [[self::class, 'isAmphpImplementingPsr18'], AmphpPsrHttpClient::class, Psr17RequestFactory::class, Psr17ResponseFactory::class],
108+
],
101109
[
102110
'class' => GuzzleHttp::class,
103111
'condition' => [self::class, 'isGuzzleImplementingPsr18'],
@@ -143,6 +151,20 @@ private static function getPsr18Candidates()
143151
return $candidates;
144152
}
145153

154+
public static function amphpPsr18Instantiate()
155+
{
156+
$httpClient = AmphpHttpClientBuilder::buildDefault();
157+
$psrAdapter = new AmphpPsrAdapter(Psr17FactoryDiscovery::findRequestFactory(), Psr17FactoryDiscovery::findResponseFactory());
158+
159+
return new AmphpPsrHttpClient($httpClient, $psrAdapter);
160+
}
161+
162+
public static function isAmphpImplementingPsr18()
163+
{
164+
// Amphp PsrHttpClient is implementing the interface only on the v2 branch
165+
return is_subclass_of(AmphpPsrHttpClient::class, Psr18Client::class);
166+
}
167+
146168
public static function buzzInstantiate()
147169
{
148170
return new \Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory());

0 commit comments

Comments
 (0)