Skip to content

Commit 091f5e1

Browse files
committed
continued refactoring
1 parent 2ffe5e1 commit 091f5e1

18 files changed

+349
-469
lines changed

doc/proxy-clients.rst

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ which caching solution you use.
1212
Setup
1313
-----
1414

15-
HTTP Adapter Installation
16-
~~~~~~~~~~~~~~~~~~~~~~~~~
15+
HTTP Client Installation
16+
~~~~~~~~~~~~~~~~~~~~~~~~
1717

18-
Because the clients send invalidation requests over HTTP, an `HTTP adapter`_
19-
must be installed. Which one you need depends on the HTTP client library that
20-
you use in your project. For instance, if you use Guzzle 6 in your project,
21-
install the appropriate adapter:
18+
Because the clients send invalidation requests over HTTP, an `HTTP client`_
19+
must be installed. You can choose a client that fits for your project or
20+
an adapter to a client you already use. For instance, if you use Guzzle 6 in
21+
your project, install the appropriate adapter:
2222

2323
.. code-block:: bash
2424
@@ -28,6 +28,8 @@ You also need a `PSR-7 message implementation`_. If you use Guzzle 6, Guzzle’s
2828
implementation is already included. If you use another client, install one of
2929
the implementations. Recommended:
3030

31+
TODO: i think we need the matching adapter for the factory, if guzzle6-adapter is not installed
32+
3133
.. code-block:: bash
3234
3335
$ composer require guzzlehttp/psr7
@@ -40,13 +42,13 @@ Alternatively:
4042
4143
.. _HTTP adapter configuration:
4244

43-
HTTP Adapter Configuration
44-
~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
HTTP Client Configuration
46+
~~~~~~~~~~~~~~~~~~~~~~~~~
4547

46-
By default, the proxy client will find the adapter that you have installed
47-
through Composer. But you can also pass the adapter explicitly. This is most
48-
useful when you have created a HTTP client with custom options or middleware
49-
(such as logging)::
48+
By default, the proxy client will automatically locate an HTTP client that you
49+
have installed through Composer. But you can also pass the adapter explicitly.
50+
This is most useful when you have created a HTTP client with custom options or
51+
middleware (such as logging)::
5052

5153
use GuzzleHttp\Client;
5254

@@ -66,6 +68,13 @@ Then pass that adapter to the caching proxy client::
6668
$proxyClient = new Varnish($servers, '/baseUrl', $adapter);
6769
// Varnish as example, but also possible for NGINX and Symfony
6870

71+
HTTP Message Factory Configuration
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
Similar to the HTTP client, the HTTP message factory is automatically located
75+
by default. You can pass an explicit instance of the message factory if you
76+
need to.
77+
6978
.. _varnish client:
7079

7180
Varnish Client
@@ -267,5 +276,5 @@ Varnish client::
267276
Make sure to add any headers that you want to ban on to your
268277
:doc:`proxy configuration <proxy-configuration>`.
269278

270-
.. _HTTP Adapter: http://php-http.readthedocs.org/en/latest/
279+
.. _HTTP client: http://httplug.io/
271280
.. _PSR-7 message implementation: https://packagist.org/providers/psr/http-message-implementation

src/ProxyClient/AbstractProxyClient.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,58 @@
1111

1212
namespace FOS\HttpCache\ProxyClient;
1313

14-
use FOS\HttpCache\Exception\InvalidArgumentException;
1514
use FOS\HttpCache\ProxyClient\Http\HttpAdapter;
16-
use FOS\HttpCache\ProxyClient\Http\HttpAdapterInterface;
17-
use FOS\HttpCache\ProxyClient\Http\HttpAsyncAdapter;
1815
use Http\Client\HttpAsyncClient;
19-
use Http\Client\HttpClient;
20-
use Http\Discovery\HttpClientDiscovery;
16+
use Http\Discovery\HttpAsyncClientDiscovery;
17+
use Http\Discovery\MessageFactoryDiscovery;
18+
use Http\Message\MessageFactory;
2119

2220
/**
23-
* Abstract caching proxy client
21+
* Abstract HTTP based caching proxy client.
2422
*
2523
* @author David de Boer <[email protected]>
2624
*/
2725
abstract class AbstractProxyClient implements ProxyClientInterface
2826
{
2927
/**
30-
* HTTP client
28+
* HTTP client adapter.
3129
*
32-
* @var HttpAdapterInterface
30+
* @var HttpAdapter
3331
*/
3432
protected $httpAdapter;
3533

34+
/**
35+
* @var MessageFactory
36+
*/
37+
protected $messageFactory;
38+
3639
/**
3740
* Constructor
3841
*
39-
* @param array $servers Caching proxy server hostnames or IP
40-
* addresses, including port if not port 80.
41-
* E.g. ['127.0.0.1:6081']
42-
* @param string $baseUri Default application hostname, optionally
43-
* including base URL, for purge and refresh
44-
* requests (optional). This is required if
45-
* you purge and refresh paths instead of
46-
* absolute URLs.
47-
* @param HttpClient|HttpAsyncClient|null $httpClient If no HTTP client is supplied, a default
48-
* one will be created.
42+
* @param array $servers Caching proxy server hostnames or IP
43+
* addresses, including port if not port 80.
44+
* E.g. ['127.0.0.1:6081']
45+
* @param string $baseUri Default application hostname, optionally
46+
* including base URL, for purge and refresh
47+
* requests (optional). This is required if
48+
* you purge and refresh paths instead of
49+
* absolute URLs.
50+
* @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no
51+
* client is supplied, a default one is created.
52+
* @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied,
53+
* a default one is created.
4954
*/
5055
public function __construct(
5156
array $servers,
5257
$baseUri = null,
53-
$httpClient = null
58+
HttpAsyncClient $httpClient = null,
59+
MessageFactory $messageFactory = null
5460
) {
55-
if ($httpClient instanceof HttpClient) {
56-
$this->httpAdapter = new HttpAdapter($servers, $baseUri, $httpClient);
57-
} elseif ($httpClient instanceof HttpAsyncClient) {
58-
$this->httpAdapter = new HttpAsyncAdapter($servers, $baseUri, $httpClient);
59-
} elseif (null === $httpClient) {
60-
$this->httpAdapter = new HttpAdapter($servers, $baseUri, HttpClientDiscovery::find());
61-
} else {
62-
throw new InvalidArgumentException('client must either be null or implement HttpClient or HttpAsyncClient');
61+
if (!$httpClient) {
62+
$httpClient = HttpAsyncClientDiscovery::find();
6363
}
64+
$this->httpAdapter = new HttpAdapter($servers, $baseUri, $httpClient);
65+
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
6466
}
6567

6668
/**

0 commit comments

Comments
 (0)