Skip to content

Commit 19c28ed

Browse files
authored
Merge pull request #143 from php-http/patch-psr18
Make sure we can use PSR-18 clients in version 1.x
2 parents 955d057 + 90e7aea commit 19c28ed

7 files changed

+36
-17
lines changed

src/BatchClient.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Exception;
66
use Http\Client\HttpClient;
77
use Http\Client\Common\Exception\BatchException;
8+
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\RequestInterface;
910

1011
/**
@@ -17,15 +18,19 @@
1718
class BatchClient implements HttpClient
1819
{
1920
/**
20-
* @var HttpClient
21+
* @var HttpClient|ClientInterface
2122
*/
2223
private $client;
2324

2425
/**
25-
* @param HttpClient $client
26+
* @param HttpClient|ClientInterface $client
2627
*/
27-
public function __construct(HttpClient $client)
28+
public function __construct($client)
2829
{
30+
if (!($client instanceof HttpClient) && !($client instanceof ClientInterface)) {
31+
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
32+
}
33+
2934
$this->client = $client;
3035
}
3136

src/EmulatedHttpAsyncClient.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
7+
use Psr\Http\Client\ClientInterface;
78

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

2021
/**
21-
* @param HttpClient $httpClient
22+
* @param HttpClient|ClientInterface $httpClient
2223
*/
23-
public function __construct(HttpClient $httpClient)
24+
public function __construct($httpClient)
2425
{
26+
if (!($httpClient instanceof HttpClient) && !($httpClient instanceof ClientInterface)) {
27+
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
28+
}
29+
2530
$this->httpClient = $httpClient;
2631
}
2732
}

src/FlexibleHttpClient.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
7+
use Psr\Http\Client\ClientInterface;
78

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

1920
/**
20-
* @param HttpClient|HttpAsyncClient $client
21+
* @param HttpClient|HttpAsyncClient|ClientInterface $client
2122
*/
2223
public function __construct($client)
2324
{
24-
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) {
25+
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient) && !($client instanceof ClientInterface)) {
2526
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
2627
}
2728

2829
$this->httpClient = $client;
2930
$this->httpAsyncClient = $client;
3031

31-
if (!($this->httpClient instanceof HttpClient)) {
32+
if (!($this->httpClient instanceof HttpClient) && !($client instanceof ClientInterface)) {
3233
$this->httpClient = new EmulatedHttpClient($this->httpClient);
3334
}
3435

src/HttpClientDecorator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Client\Common;
44

55
use Http\Client\HttpClient;
6+
use Psr\Http\Client\ClientInterface;
67
use Psr\Http\Message\RequestInterface;
78

89
/**
@@ -13,7 +14,7 @@
1314
trait HttpClientDecorator
1415
{
1516
/**
16-
* @var HttpClient
17+
* @var HttpClient|ClientInterface
1718
*/
1819
protected $httpClient;
1920

src/HttpMethodsClient.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Exception;
66
use Http\Client\HttpClient;
77
use Http\Message\RequestFactory;
8+
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011
use Psr\Http\Message\StreamInterface;
@@ -27,7 +28,7 @@
2728
class HttpMethodsClient implements HttpClient
2829
{
2930
/**
30-
* @var HttpClient
31+
* @var HttpClient|ClientInterface
3132
*/
3233
private $httpClient;
3334

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

3940
/**
40-
* @param HttpClient $httpClient The client to send requests with
41-
* @param RequestFactory $requestFactory The message factory to create requests
41+
* @param HttpClient|ClientInterface $httpClient The client to send requests with
42+
* @param RequestFactory $requestFactory The message factory to create requests
4243
*/
43-
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory)
44+
public function __construct($httpClient, RequestFactory $requestFactory)
4445
{
46+
if (!($httpClient instanceof HttpClient) && !($httpClient instanceof ClientInterface)) {
47+
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
48+
}
49+
4550
$this->httpClient = $httpClient;
4651
$this->requestFactory = $requestFactory;
4752
}

src/PluginClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Http\Client\HttpClient;
99
use Http\Client\Promise\HttpFulfilledPromise;
1010
use Http\Client\Promise\HttpRejectedPromise;
11+
use Psr\Http\Client\ClientInterface;
1112
use Psr\Http\Message\RequestInterface;
1213
use Symfony\Component\OptionsResolver\OptionsResolver;
1314

@@ -54,7 +55,7 @@ public function __construct($client, array $plugins = [], array $options = [])
5455
{
5556
if ($client instanceof HttpAsyncClient) {
5657
$this->client = $client;
57-
} elseif ($client instanceof HttpClient) {
58+
} elseif ($client instanceof HttpClient || $client instanceof ClientInterface) {
5859
$this->client = new EmulatedHttpAsyncClient($client);
5960
} else {
6061
throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');

src/PluginClientFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
7+
use Psr\Http\Client\ClientInterface;
78

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

3738
/**
38-
* @param HttpClient|HttpAsyncClient $client
39-
* @param Plugin[] $plugins
40-
* @param array $options {
39+
* @param HttpClient|HttpAsyncClient|ClientInterface $client
40+
* @param Plugin[] $plugins
41+
* @param array $options {
4142
*
4243
* @var string $client_name to give client a name which may be used when displaying client information like in
4344
* the HTTPlugBundle profiler.

0 commit comments

Comments
 (0)