Skip to content

Commit

Permalink
continued refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Dec 7, 2015
1 parent 79a29c1 commit 728dbad
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 472 deletions.
36 changes: 22 additions & 14 deletions doc/proxy-clients.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ which caching solution you use.
Setup
-----

HTTP Adapter Installation
~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP Client Installation
~~~~~~~~~~~~~~~~~~~~~~~~

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

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

TODO: i think we need the matching adapter for the factory, if guzzle6-adapter is not installed

.. code-block:: bash
$ composer require guzzlehttp/psr7
Expand All @@ -40,13 +42,13 @@ Alternatively:
.. _HTTP adapter configuration:

HTTP Adapter Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP Client Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~

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

use GuzzleHttp\Client;

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

HTTP Message Factory Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Similar to the HTTP client, the HTTP message factory is automatically located
by default. You can pass an explicit instance of the message factory if you
need to.

.. _varnish client:

Varnish Client
Expand Down Expand Up @@ -276,6 +285,5 @@ Varnish client::
Make sure to add any headers that you want to ban on to your
:doc:`proxy configuration <proxy-configuration>`.

.. _header: http://php.net/header
.. _HTTP Adapter: http://php-http.readthedocs.org/en/latest/
.. _HTTP client: http://httplug.io/
.. _PSR-7 message implementation: https://packagist.org/providers/psr/http-message-implementation
56 changes: 29 additions & 27 deletions src/ProxyClient/AbstractProxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,58 @@

namespace FOS\HttpCache\ProxyClient;

use FOS\HttpCache\Exception\InvalidArgumentException;
use FOS\HttpCache\ProxyClient\Http\HttpAdapter;
use FOS\HttpCache\ProxyClient\Http\HttpAdapterInterface;
use FOS\HttpCache\ProxyClient\Http\HttpAsyncAdapter;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;

/**
* Abstract caching proxy client
* Abstract HTTP based caching proxy client.
*
* @author David de Boer <david@driebit.nl>
*/
abstract class AbstractProxyClient implements ProxyClientInterface
{
/**
* HTTP client
* HTTP client adapter.
*
* @var HttpAdapterInterface
* @var HttpAdapter
*/
protected $httpAdapter;

/**
* @var MessageFactory
*/
protected $messageFactory;

/**
* Constructor
*
* @param array $servers Caching proxy server hostnames or IP
* addresses, including port if not port 80.
* E.g. ['127.0.0.1:6081']
* @param string $baseUri Default application hostname, optionally
* including base URL, for purge and refresh
* requests (optional). This is required if
* you purge and refresh paths instead of
* absolute URLs.
* @param HttpClient|HttpAsyncClient|null $httpClient If no HTTP client is supplied, a default
* one will be created.
* @param array $servers Caching proxy server hostnames or IP
* addresses, including port if not port 80.
* E.g. ['127.0.0.1:6081']
* @param string $baseUri Default application hostname, optionally
* including base URL, for purge and refresh
* requests (optional). This is required if
* you purge and refresh paths instead of
* absolute URLs.
* @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no
* client is supplied, a default one is created.
* @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied,
* a default one is created.
*/
public function __construct(
array $servers,
$baseUri = null,
$httpClient = null
HttpAsyncClient $httpClient = null,
MessageFactory $messageFactory = null
) {
if ($httpClient instanceof HttpClient) {
$this->httpAdapter = new HttpAdapter($servers, $baseUri, $httpClient);
} elseif ($httpClient instanceof HttpAsyncClient) {
$this->httpAdapter = new HttpAsyncAdapter($servers, $baseUri, $httpClient);
} elseif (null === $httpClient) {
$this->httpAdapter = new HttpAdapter($servers, $baseUri, HttpClientDiscovery::find());
} else {
throw new InvalidArgumentException('client must either be null or implement HttpClient or HttpAsyncClient');
if (!$httpClient) {
$httpClient = HttpAsyncClientDiscovery::find();
}
$this->httpAdapter = new HttpAdapter($servers, $baseUri, $httpClient);
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
}

/**
Expand Down
Loading

0 comments on commit 728dbad

Please sign in to comment.