Skip to content

Make sure we can use PSR-18 clients in version 1.x #143

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

Merged
merged 2 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/BatchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Client\Common\Exception\BatchException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

/**
Expand All @@ -17,15 +18,19 @@
class BatchClient implements HttpClient
{
/**
* @var HttpClient
* @var HttpClient|ClientInterface
*/
private $client;

/**
* @param HttpClient $client
* @param HttpClient|ClientInterface $client
*/
public function __construct(HttpClient $client)
public function __construct($client)
{
if (!($client instanceof HttpClient) && !($client instanceof ClientInterface)) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}

$this->client = $client;
}

Expand Down
9 changes: 7 additions & 2 deletions src/EmulatedHttpAsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
* Emulates an async HTTP client.
Expand All @@ -18,10 +19,14 @@ class EmulatedHttpAsyncClient implements HttpClient, HttpAsyncClient
use HttpClientDecorator;

/**
* @param HttpClient $httpClient
* @param HttpClient|ClientInterface $httpClient
*/
public function __construct(HttpClient $httpClient)
public function __construct($httpClient)
{
if (!($httpClient instanceof HttpClient) && !($httpClient instanceof ClientInterface)) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}

$this->httpClient = $httpClient;
}
}
7 changes: 4 additions & 3 deletions src/FlexibleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
* A flexible http client, which implements both interface and will emulate
Expand All @@ -17,18 +18,18 @@ final class FlexibleHttpClient implements HttpClient, HttpAsyncClient
use HttpAsyncClientDecorator;

/**
* @param HttpClient|HttpAsyncClient $client
* @param HttpClient|HttpAsyncClient|ClientInterface $client
*/
public function __construct($client)
{
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) {
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient) && !($client instanceof ClientInterface)) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
}

$this->httpClient = $client;
$this->httpAsyncClient = $client;

if (!($this->httpClient instanceof HttpClient)) {
if (!($this->httpClient instanceof HttpClient) && !($client instanceof ClientInterface)) {
$this->httpClient = new EmulatedHttpClient($this->httpClient);
}

Expand Down
3 changes: 2 additions & 1 deletion src/HttpClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\Client\Common;

use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

/**
Expand All @@ -13,7 +14,7 @@
trait HttpClientDecorator
{
/**
* @var HttpClient
* @var HttpClient|ClientInterface
*/
protected $httpClient;

Expand Down
13 changes: 9 additions & 4 deletions src/HttpMethodsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -27,7 +28,7 @@
class HttpMethodsClient implements HttpClient
{
/**
* @var HttpClient
* @var HttpClient|ClientInterface
*/
private $httpClient;

Expand All @@ -37,11 +38,15 @@ class HttpMethodsClient implements HttpClient
private $requestFactory;

/**
* @param HttpClient $httpClient The client to send requests with
* @param RequestFactory $requestFactory The message factory to create requests
* @param HttpClient|ClientInterface $httpClient The client to send requests with
* @param RequestFactory $requestFactory The message factory to create requests
*/
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory)
public function __construct($httpClient, RequestFactory $requestFactory)
{
if (!($httpClient instanceof HttpClient) && !($httpClient instanceof ClientInterface)) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}

$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
}
Expand Down
3 changes: 2 additions & 1 deletion src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Http\Client\HttpClient;
use Http\Client\Promise\HttpFulfilledPromise;
use Http\Client\Promise\HttpRejectedPromise;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -54,7 +55,7 @@ public function __construct($client, array $plugins = [], array $options = [])
{
if ($client instanceof HttpAsyncClient) {
$this->client = $client;
} elseif ($client instanceof HttpClient) {
} elseif ($client instanceof HttpClient || $client instanceof ClientInterface) {
$this->client = new EmulatedHttpAsyncClient($client);
} else {
throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
Expand Down
7 changes: 4 additions & 3 deletions src/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
* Factory to create PluginClient instances. Using this factory instead of calling PluginClient constructor will enable
Expand Down Expand Up @@ -35,9 +36,9 @@ public static function setFactory(callable $factory)
}

/**
* @param HttpClient|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param array $options {
* @param HttpClient|HttpAsyncClient|ClientInterface $client
* @param Plugin[] $plugins
* @param array $options {
*
* @var string $client_name to give client a name which may be used when displaying client information like in
* the HTTPlugBundle profiler.
Expand Down