Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add suport for Amphp PSR18 Client #257

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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