|
2 | 2 |
|
3 | 3 | namespace Http\Discovery\Strategy;
|
4 | 4 |
|
| 5 | +use Amp\Http\Client\HttpClientBuilder as AmphpHttpClientBuilder; |
| 6 | +use Amp\Http\Client\Psr7\PsrHttpClient as AmphpPsrHttpClient; |
| 7 | +use Amp\Http\Client\Psr7\PsrAdapter as AmphpPsrAdapter; |
5 | 8 | use GuzzleHttp\Client as GuzzleHttp;
|
6 | 9 | use GuzzleHttp\Promise\Promise;
|
7 | 10 | use GuzzleHttp\Psr7\Request as GuzzleRequest;
|
|
35 | 38 | use Nyholm\Psr7\Factory\HttplugFactory as NyholmHttplugFactory;
|
36 | 39 | use Psr\Http\Client\ClientInterface as Psr18Client;
|
37 | 40 | use Psr\Http\Message\RequestFactoryInterface as Psr17RequestFactory;
|
| 41 | +use Psr\Http\Message\ResponseFactoryInterface as Psr17ResponseFactory; |
38 | 42 | use Slim\Http\Request as SlimRequest;
|
39 | 43 | use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplug;
|
40 | 44 | use Symfony\Component\HttpClient\Psr18Client as SymfonyPsr18;
|
@@ -98,6 +102,10 @@ final class CommonClassesStrategy implements DiscoveryStrategy
|
98 | 102 | 'class' => [self::class, 'symfonyPsr18Instantiate'],
|
99 | 103 | 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class],
|
100 | 104 | ],
|
| 105 | + [ |
| 106 | + 'class' => [self::class, 'amphpPsr18Instantiate'], |
| 107 | + 'condition' => [[self::class, 'isAmphpImplementingPsr18'], AmphpPsrHttpClient::class, Psr17RequestFactory::class, Psr17ResponseFactory::class], |
| 108 | + ], |
101 | 109 | [
|
102 | 110 | 'class' => GuzzleHttp::class,
|
103 | 111 | 'condition' => [self::class, 'isGuzzleImplementingPsr18'],
|
@@ -143,6 +151,20 @@ private static function getPsr18Candidates()
|
143 | 151 | return $candidates;
|
144 | 152 | }
|
145 | 153 |
|
| 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 | + |
146 | 168 | public static function buzzInstantiate()
|
147 | 169 | {
|
148 | 170 | return new \Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory());
|
|
0 commit comments