Skip to content

Commit 53bddb2

Browse files
Nyholmdbu
authored andcommitted
Support PSR-18 (#135)
Typecheck for PSR-18 ClientInterface instead of Httplug Client interface. Our interface extends the PSR-18 ClientInterface and therefore is still accepted. No changes for the async client as there is no PSR for that (yet)
1 parent 700fbb4 commit 53bddb2

13 files changed

+41
-35
lines changed

src/BatchClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace Http\Client\Common;
44

55
use Http\Client\Exception;
6-
use Http\Client\HttpClient;
76
use Http\Client\Common\Exception\BatchException;
7+
use Psr\Http\Client\ClientInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010

1111
final class BatchClient implements BatchClientInterface
1212
{
1313
/**
14-
* @var HttpClient
14+
* @var ClientInterface
1515
*/
1616
private $client;
1717

18-
public function __construct(HttpClient $client)
18+
public function __construct(ClientInterface $client)
1919
{
2020
$this->client = $client;
2121
}

src/EmulatedHttpAsyncClient.php

Lines changed: 2 additions & 1 deletion
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 with the help of a synchronous client.
@@ -15,7 +16,7 @@ final class EmulatedHttpAsyncClient implements HttpClient, HttpAsyncClient
1516
use HttpAsyncClientEmulator;
1617
use HttpClientDecorator;
1718

18-
public function __construct(HttpClient $httpClient)
19+
public function __construct(ClientInterface $httpClient)
1920
{
2021
$this->httpClient = $httpClient;
2122
}

src/FlexibleHttpClient.php

Lines changed: 5 additions & 4 deletions
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 ClientInterface|HttpAsyncClient $client
2122
*/
2223
public function __construct($client)
2324
{
24-
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) {
25-
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
25+
if (!($client instanceof ClientInterface) && !($client instanceof HttpAsyncClient)) {
26+
throw new \LogicException('Client must be an instance of Psr\\Http\\Client\\ClientInterface 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 ClientInterface)) {
3233
$this->httpClient = new EmulatedHttpClient($this->httpClient);
3334
}
3435

src/HttpClientDecorator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Http\Client\Common;
44

5-
use Http\Client\HttpClient;
5+
use Psr\Http\Client\ClientInterface;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88

@@ -14,14 +14,14 @@
1414
trait HttpClientDecorator
1515
{
1616
/**
17-
* @var HttpClient
17+
* @var ClientInterface
1818
*/
1919
protected $httpClient;
2020

2121
/**
2222
* {@inheritdoc}
2323
*
24-
* @see HttpClient::sendRequest
24+
* @see ClientInterface::sendRequest
2525
*/
2626
public function sendRequest(RequestInterface $request): ResponseInterface
2727
{

src/HttpClientPool.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Common\HttpClientPool\HttpClientPoolItem;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* A http client pool allows to send requests on a pool of different http client using a specific strategy (least used,
@@ -15,7 +16,7 @@ interface HttpClientPool extends HttpAsyncClient, HttpClient
1516
/**
1617
* Add a client to the pool.
1718
*
18-
* @param HttpClient|HttpAsyncClient|HttpClientPoolItem $client
19+
* @param ClientInterface|HttpAsyncClient|HttpClientPoolItem $client
1920
*/
2021
public function addHttpClient($client);
2122
}

src/HttpClientPool/HttpClientPool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Http\Client\Common\Exception\HttpClientNotFoundException;
66
use Http\Client\Common\HttpClientPool as HttpClientPoolInterface;
77
use Http\Client\HttpAsyncClient;
8-
use Http\Client\HttpClient;
8+
use Psr\Http\Client\ClientInterface;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111

@@ -23,7 +23,7 @@ abstract class HttpClientPool implements HttpClientPoolInterface
2323
/**
2424
* Add a client to the pool.
2525
*
26-
* @param HttpClient|HttpAsyncClient|HttpClientPoolItem $client
26+
* @param ClientInterface|HttpAsyncClient|HttpClientPoolItem $client
2727
*/
2828
public function addHttpClient($client)
2929
{

src/HttpClientPool/HttpClientPoolItem.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Common\FlexibleHttpClient;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
8+
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\RequestInterface;
910
use Http\Client\Exception;
1011
use Psr\Http\Message\ResponseInterface;
@@ -51,8 +52,8 @@ class HttpClientPoolItem implements HttpClient, HttpAsyncClient
5152
private $client;
5253

5354
/**
54-
* @param HttpClient|HttpAsyncClient $client
55-
* @param null|int $reenableAfter Number of seconds until this client is enabled again after an error
55+
* @param ClientInterface|HttpAsyncClient $client
56+
* @param null|int $reenableAfter Number of seconds until this client is enabled again after an error
5657
*/
5758
public function __construct($client, $reenableAfter = null)
5859
{

src/HttpClientRouter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
88
use Http\Message\RequestMatcher;
9+
use Psr\Http\Client\ClientInterface;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
1112

@@ -53,7 +54,7 @@ public function addClient($client, RequestMatcher $requestMatcher)
5354
/**
5455
* Choose an HTTP client given a specific request.
5556
*
56-
* @return HttpClient|HttpAsyncClient
57+
* @return ClientInterface|HttpAsyncClient
5758
*/
5859
private function chooseHttpClient(RequestInterface $request)
5960
{

src/HttpClientRouterInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
77
use Http\Message\RequestMatcher;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* Route a request to a specific client in the stack based using a RequestMatcher.
@@ -18,7 +19,7 @@ interface HttpClientRouterInterface extends HttpClient, HttpAsyncClient
1819
/**
1920
* Add a client to the router.
2021
*
21-
* @param HttpClient|HttpAsyncClient $client
22+
* @param ClientInterface|HttpAsyncClient $client
2223
*/
2324
public function addClient($client, RequestMatcher $requestMatcher);
2425
}

src/HttpMethodsClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Http\Client\Common;
44

5-
use Http\Client\HttpClient;
65
use Http\Message\RequestFactory;
6+
use Psr\Http\Client\ClientInterface;
77
use Psr\Http\Message\RequestInterface;
88
use Psr\Http\Message\ResponseInterface;
99

1010
final class HttpMethodsClient implements HttpMethodsClientInterface
1111
{
1212
/**
13-
* @var HttpClient
13+
* @var ClientInterface
1414
*/
1515
private $httpClient;
1616

@@ -20,10 +20,10 @@ final class HttpMethodsClient implements HttpMethodsClientInterface
2020
private $requestFactory;
2121

2222
/**
23-
* @param HttpClient $httpClient The client to send requests with
24-
* @param RequestFactory $requestFactory The message factory to create requests
23+
* @param ClientInterface $httpClient The client to send requests with
24+
* @param RequestFactory $requestFactory The message factory to create requests
2525
*/
26-
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory)
26+
public function __construct(ClientInterface $httpClient, RequestFactory $requestFactory)
2727
{
2828
$this->httpClient = $httpClient;
2929
$this->requestFactory = $requestFactory;

src/Plugin/AddPathPlugin.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
4747
$identifier = spl_object_hash((object) $first);
4848

4949
if (!array_key_exists($identifier, $this->alteredRequests)) {
50-
$request = $request->withUri($request->getUri()
51-
->withPath($this->uri->getPath().$request->getUri()->getPath())
52-
);
50+
$prefixedUrl = $this->uri->getPath().$request->getUri()->getPath();
51+
$request = $request->withUri($request->getUri()->withPath($prefixedUrl));
5352
$this->alteredRequests[$identifier] = $identifier;
5453
}
5554

src/PluginClient.php

Lines changed: 6 additions & 5 deletions
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 Psr\Http\Message\ResponseInterface;
1314
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -41,9 +42,9 @@ final class PluginClient implements HttpClient, HttpAsyncClient
4142
private $options;
4243

4344
/**
44-
* @param HttpClient|HttpAsyncClient $client
45-
* @param Plugin[] $plugins
46-
* @param array $options {
45+
* @param ClientInterface|HttpAsyncClient $client
46+
* @param Plugin[] $plugins
47+
* @param array $options {
4748
*
4849
* @var int $max_restarts
4950
* }
@@ -54,10 +55,10 @@ 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 ClientInterface) {
5859
$this->client = new EmulatedHttpAsyncClient($client);
5960
} else {
60-
throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
61+
throw new \RuntimeException('Client must be an instance of Psr\\Http\\Client\\ClientInterface or Http\\Client\\HttpAsyncClient');
6162
}
6263

6364
$this->plugins = $plugins;

src/PluginClientFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Http\Client\Common;
44

55
use Http\Client\HttpAsyncClient;
6-
use Http\Client\HttpClient;
6+
use Psr\Http\Client\ClientInterface;
77

88
/**
99
* Factory to create PluginClient instances. Using this factory instead of calling PluginClient constructor will enable
@@ -33,9 +33,9 @@ public static function setFactory(callable $factory)
3333
}
3434

3535
/**
36-
* @param HttpClient|HttpAsyncClient $client
37-
* @param Plugin[] $plugins
38-
* @param array $options {
36+
* @param ClientInterface|HttpAsyncClient $client
37+
* @param Plugin[] $plugins
38+
* @param array $options {
3939
*
4040
* @var string $client_name to give client a name which may be used when displaying client information like in
4141
* the HTTPlugBundle profiler.

0 commit comments

Comments
 (0)