Skip to content

Commit 6a3f996

Browse files
committed
Use php-http/adapter for HTTP client abstraction
Remove custom adapters Update to PSR-7, Guzzle 6 and php-http/adapter Remove PHP 5.3 from build matrix Adopt changes in http-adapter and use discovery Remove dependencies on Guzzle (components) We use Guzzle 6 for testing, so it needs to stay in require-dev. Use php-http/message-decorator Change branch alias to 2.0.x Add docs Allow failures on hhvm Fix connection timing issues Remove all Guzzle-specifics from the tests, too Change documentation links to stable Try to run HHVM on Guzzle 5 Remove Bash file Add version notice to docs Fix setPurgeLocation docs Don't mock exceptions * It’s easier not to * Mocking exceptions causes HHVM to fail with "Call to a member function mockery_getName() on a non-object (NULL)" Extract createUri() method Remove allow_failures for HHVM Add response code to cache hit/miss assertion message Throw exceptions for error status codes
1 parent 2fd42b7 commit 6a3f996

37 files changed

+1012
-826
lines changed

.travis.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
54
- 5.4
65
- 5.5
76
- 5.6
@@ -13,20 +12,21 @@ env:
1312

1413
matrix:
1514
include:
16-
- php: 5.5
17-
env: VARNISH_VERSION=3.0
18-
- php: 5.3
19-
env: SYMFONY_VERSION=2.3.* VARNISH_VERSION=4.0 COMPOSER_FLAGS="--prefer-lowest"
15+
- php: 5.4
16+
env: SYMFONY_VERSION=2.3.* VARNISH_VERSION=3.0 COMPOSER_FLAGS="--prefer-lowest"
2017

2118
branches:
2219
only:
2320
- master
2421
# Build maintenance branches for older releases if needed. such branches should be named like "1.2"
2522
- '/^\d+\.\d+$/'
2623

24+
install:
25+
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" || "$TRAVIS_PHP_VERSION" == "hhvm" ]]; then composer remove "php-http/guzzle6-adapter" --dev --no-update; fi
26+
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" || "$TRAVIS_PHP_VERSION" == "hhvm" ]]; then composer require "php-http/guzzle5-adapter" --dev --no-update; fi
27+
- composer update $COMPOSER_FLAGS --prefer-source --no-interaction
28+
2729
before_script:
28-
# Install deps
29-
- composer update $COMPOSER_FLAGS --dev --prefer-source --no-interaction
3030
# Install Varnish
3131
- curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
3232
- echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-${VARNISH_VERSION}" | sudo tee -a /etc/apt/sources.list
@@ -36,7 +36,6 @@ before_script:
3636
# Install NGINX
3737
- sh ./tests/install-nginx.sh
3838
# Starting webserver
39-
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.3' ]; then ./tests/ci/install-apache.sh; fi"
4039
- sh -c "if [ '$TRAVIS_PHP_VERSION' = 'hhvm' ]; then ./tests/ci/install-apache-hhvm.sh; fi"
4140

4241
script:

CHANGELOG.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.0.0 (unreleased)
7+
------------------
8+
9+
* Replace hard coupling on Guzzle HTTP client with HTTP adapter.
10+
* The NGINX purge location is no longer passed as constructor argument but by
11+
calling `setPurgeLocation()`.
12+
* In ProxyTestCase, `getHttpClient()` has been replaced with `getHttpAdapter()`;
13+
added HTTP method parameter to `getResponse()`.
14+
615
1.4.0
716
-----
817

9-
* Added symfony/http-kernel [HttpCache client](http://foshttpcache.readthedocs.org/en/latest/proxy-clients.html#symfony-client).
10-
* Added [SymfonyTestCase](http://foshttpcache.readthedocs.org/en/latest/testing-your-application.html#symfonytestcase).
18+
* Added symfony/http-kernel [HttpCache client](http://foshttpcache.readthedocs.org/en/stable/proxy-clients.html#symfony-client).
19+
* Added [SymfonyTestCase](http://foshttpcache.readthedocs.org/en/stable/testing-your-application.html#symfonytestcase).
1120
* Removed unneeded files from dist packages.
1221

1322
1.3.2
@@ -19,17 +28,17 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC
1928
-----
2029

2130
* Added authentication support to user context subscribe.
22-
* Fixed usage of deprecated Guzzle subtree splits.
31+
* Fixed usage of deprecated Guzzle subtree splits.
2332
* Fixed exposed cache tags.
2433

2534
1.3.0
2635
-----
2736

28-
* Added [TagHandler](http://foshttpcache.readthedocs.org/en/latest/invalidation-handlers.html#tag-handler).
37+
* Added [TagHandler](http://foshttpcache.readthedocs.org/en/stable/invalidation-handlers.html#tag-handler).
2938
* It is no longer possible to change the event dispatcher of the
30-
CacheInvalidator once its instantiated. If you need a custom dispatcher, set
31-
it right after creating the invalidator instance.
32-
* Deprecated `CacheInvalidator::addSubscriber` in favor of either using the event
39+
CacheInvalidator once its instantiated. If you need a custom dispatcher, set
40+
it right after creating the invalidator instance.
41+
* Deprecated `CacheInvalidator::addSubscriber` in favor of either using the event
3342
dispatcher instance you inject or doing `getEventDispatcher()->addSubscriber($subscriber)`.
3443

3544
1.2.0

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ Symfony2-specific features to help with caching and caching proxies.
2020
Features
2121
--------
2222

23-
* Send [cache invalidation requests](http://foshttpcache.readthedocs.org/en/latest/cache-invalidator.html)
23+
* Send [cache invalidation requests](http://foshttpcache.readthedocs.org/en/stable/cache-invalidator.html)
2424
with minimal impact on performance.
25-
* Use the built-in support for [Varnish](http://foshttpcache.readthedocs.org/en/latest/varnish-configuration.html)
26-
3 and 4, [NGINX](http://foshttpcache.readthedocs.org/en/latest/nginx-configuration.html), the
27-
[Symfony reverse proxy from the http-kernel component](http://foshttpcache.readthedocs.org/en/latest/symfony-cache-configuration.html)
25+
* Use the built-in support for [Varnish](http://foshttpcache.readthedocs.org/en/stable/varnish-configuration.html)
26+
3 and 4, [NGINX](http://foshttpcache.readthedocs.org/en/stable/nginx-configuration.html), the
27+
[Symfony reverse proxy from the http-kernel component](http://foshttpcache.readthedocs.org/en/stable/symfony-cache-configuration.html)
2828
or easily implement your own caching proxy client.
29-
* [Test your application](http://foshttpcache.readthedocs.org/en/latest/testing-your-application.html)
29+
* [Test your application](http://foshttpcache.readthedocs.org/en/stable/testing-your-application.html)
3030
against your Varnish or NGINX setup.
3131
* This library is fully compatible with [HHVM](http://www.hhvm.com).
3232

3333
Documentation
3434
-------------
3535

36-
For more information, see [the documentation](http://foshttpcache.readthedocs.org/en/latest/).
36+
For more information, see [the documentation](http://foshttpcache.readthedocs.org/en/stable/).
3737

3838
License
3939
-------

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
}
2222
],
2323
"require": {
24-
"php": ">=5.3.3",
25-
"guzzle/guzzle": "~3.8",
24+
"php": ">=5.4.8",
2625
"symfony/event-dispatcher": "~2.3",
27-
"symfony/options-resolver": "~2.3"
26+
"symfony/options-resolver": "~2.3",
27+
"psr/http-message-implementation": "~1.0",
28+
"php-http/adapter-implementation": "^0.1.0",
29+
"php-http/discovery": "^0.1.1",
30+
"php-http/message-decorator": "^0.1.0"
2831
},
2932
"require-dev": {
3033
"mockery/mockery": "~0.9.1",
3134
"monolog/monolog": "~1.0",
35+
"php-http/guzzle6-adapter": "^0.1.0",
36+
"guzzlehttp/psr7": "^1.0",
3237
"symfony/process": "~2.3",
3338
"symfony/http-kernel": "~2.3"
3439
},
@@ -47,7 +52,7 @@
4752
},
4853
"extra": {
4954
"branch-alias": {
50-
"dev-master": "1.4.x-dev"
55+
"dev-master": "2.0.x-dev"
5156
}
5257
}
5358
}

doc/includes/custom-headers.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
This allows you to pass headers that are different between purge requests.
22
If you want to add a header to all purge requests, such as ``Authorization``,
3-
use a :ref:`custom Guzzle client <custom Guzzle client>` instead.
3+
:ref:`configure the HTTP adapter <HTTP adapter configuration>` to use a
4+
custom HTTP client instead.

doc/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ FOSHttpCache
33

44
This is the documentation for the `FOSHttpCache library <https://github.com/FriendsOfSymfony/FOSHttpCache>`_.
55

6+
.. note::
7+
8+
This documentation is for the (upcoming) 2.0 of the library. For the stable
9+
1.* version, please refer to the `stable documentation`_.
10+
611
This library integrates your PHP applications with HTTP caching proxies such as
712
Varnish, NGINX or the Symfony HttpCache class. Use this library to send
813
invalidation requests from your application to the caching proxy and to test
@@ -28,3 +33,5 @@ Contents:
2833

2934
testing-your-application
3035
contributing
36+
37+
.. _stable documentation: http://foshttpcache.readthedocs.org/en/stable/

doc/proxy-clients.rst

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
Caching Proxy Clients
22
=====================
33

4-
This library ships with clients for the Varnish, NGINX and Symfony built-in caching proxies. You
5-
can use the clients either wrapped by the :doc:`cache invalidator <cache-invalidator>`
6-
(recommended), or directly for low-level access to invalidation functionality.
4+
This library ships with clients for the Varnish and NGINX caching servers and
5+
the Symfony built-in HTTP cache. You can use the clients either wrapped by the
6+
:doc:`cache invalidator <cache-invalidator>` (recommended), or directly for
7+
low-level access to invalidation functionality. Which client you need depends on
8+
which caching solution you use.
79

810
.. _client setup:
911

1012
Setup
1113
-----
1214

15+
HTTP Adapter Installation
16+
~~~~~~~~~~~~~~~~~~~~~~~~~
17+
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:
22+
23+
.. code-block:: bash
24+
25+
$ composer require php-http/guzzle6-adapter
26+
27+
You also need a `PSR-7 message implementation`_. If you use Guzzle 6, Guzzle’s
28+
implementation is already included. If you use another client, install one of
29+
the implementations. Recommended:
30+
31+
.. code-block:: bash
32+
33+
$ composer require guzzlehttp/psr7
34+
35+
Alternatively:
36+
37+
.. code-block:: bash
38+
39+
$ composer require zendframework/zend-diactoros
40+
41+
.. _HTTP adapter configuration:
42+
43+
HTTP Adapter Configuration
44+
~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
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)::
50+
51+
use GuzzleHttp\Client;
52+
53+
$config = [
54+
// For instance, custom middlewares
55+
];
56+
$yourHttpClient = new Client($config);
57+
58+
Take your client and create a HTTP adapter from it::
59+
60+
use Http\Adapter\Guzzle6HttpAdapter;
61+
62+
$adapter = new Guzzle6HttpAdapter($client);
63+
64+
Then pass that adapter to the caching proxy client::
65+
66+
$proxyClient = new Varnish($servers, '/baseUrl', $adapter);
67+
// Varnish as example, but also possible for NGINX and Symfony
68+
1369
Varnish Client
1470
~~~~~~~~~~~~~~
1571

@@ -56,9 +112,13 @@ is available as the second parameter::
56112
$nginx = new Nginx($servers, 'my-cool-app.com');
57113

58114
If you have configured NGINX to support purge requests at a separate location,
59-
supply that location to the class as the third parameter::
115+
call `setPurgeLocation()`::
116+
117+
use FOS\HttpCache\ProxyClient\Nginx;
118+
119+
$nginx = new Nginx($servers, $baseUri);
120+
$nginx->setPurgeLocation('/purge');
60121

61-
$nginx = new Nginx($servers, 'my-cool-app.com', '/purge');
62122

63123
.. note::
64124

@@ -205,25 +265,5 @@ Varnish client::
205265
Make sure to add any headers that you want to ban on to your
206266
:doc:`proxy configuration <proxy-configuration>`.
207267

208-
.. _custom guzzle client:
209-
210-
Custom Guzzle Client
211-
--------------------
212-
213-
By default, the proxy clients instantiate a `Guzzle client`_ to communicate
214-
with the caching proxy. If you need to customize the requests, for example to
215-
send a basic authentication header, you can inject a custom Guzzle client::
216-
217-
use FOS\HttpCache\ProxyClient\Varnish;
218-
use Guzzle\Http\Client;
219-
220-
$client = new Client();
221-
$client->setDefaultOption('auth', array('username', 'password', 'Digest'));
222-
223-
$servers = array('10.0.0.1');
224-
$varnish = new Varnish($servers, '/baseUrl', $client);
225-
226-
The Symfony client accepts a guzzle client as the 3rd parameter as well, NGINX
227-
accepts it as 4th parameter.
228-
229-
.. _Guzzle client: http://guzzle3.readthedocs.org/
268+
.. _HTTP Adapter: http://php-http.readthedocs.org/en/latest/
269+
.. _PSR-7 message implementation: https://packagist.org/providers/psr/http-message-implementation

doc/spelling_word_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ hostnames
88
http
99
invalidator
1010
localhost
11+
middleware
12+
middlewares
1113
nginx
1214
roundtrip
1315
symfony

doc/testing-your-application.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ By having your test classes extend one of the test case classes, you get:
1616
* an instance of this library’s client that is configured to talk to your
1717
reverse proxy server. See reverse proxy specific sections for details;
1818
* convenience methods for executing HTTP requests to your application:
19-
``$this->getHttpClient()`` and ``$this->getResponse()``;
19+
``$this->getHttpAdapter()`` and ``$this->getResponse()``;
2020
* custom assertions ``assertHit`` and ``assertMiss`` for validating a cache
2121
hit/miss.
2222

src/Exception/ExceptionCollection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
* A collection of exceptions that might occur during the flush operation of a
1616
* ProxyClientInterface implementation
1717
*/
18-
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
18+
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
1919
{
2020
private $exceptions = array();
21+
22+
public function __construct(array $exceptions = array())
23+
{
24+
foreach ($exceptions as $exception) {
25+
$this->add($exception);
26+
}
27+
}
2128

2229
/**
2330
* Add an exception to the collection

src/Exception/InvalidUrlException.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,30 @@ class InvalidUrlException extends InvalidArgumentException implements HttpCacheE
2020
* @param string $url The invalid URL.
2121
* @param string $reason Further explanation why the URL was invalid (optional)
2222
*
23-
* @return InvalidUrlException
23+
* @return self
2424
*/
2525
public static function invalidUrl($url, $reason = null)
2626
{
2727
$msg = sprintf('URL "%s" is invalid.', $url);
2828
if ($reason) {
29-
$msg .= sprintf('Reason: %s', $reason);
29+
$msg .= sprintf(' Reason: %s', $reason);
3030
}
3131

32-
return new InvalidUrlException($msg);
32+
return new self($msg);
3333
}
3434

3535
/**
3636
* @param string $server Invalid server
3737
* @param array $allowed Allowed URL parts
3838
*
39-
* @return InvalidUrlException
39+
* @return self
4040
*/
4141
public static function invalidUrlParts($server, array $allowed)
4242
{
43-
return new InvalidUrlException(sprintf(
43+
return new self(sprintf(
4444
'Server "%s" is invalid. Only %s URL parts are allowed.',
4545
$server,
4646
implode(', ', $allowed)
4747
));
4848
}
49-
50-
/**
51-
* @param string $url Requested full URL
52-
* @param string $scheme Requested URL scheme
53-
* @param array $allowed Supported URL schemes
54-
*
55-
* @return InvalidUrlException
56-
*/
57-
public static function invalidUrlScheme($url, $scheme, array $allowed)
58-
{
59-
return new InvalidUrlException(sprintf(
60-
'Host "%s" with scheme "%s" is invalid. Only schemes "%s" are supported',
61-
$url,
62-
$scheme,
63-
implode(', ', $allowed)
64-
));
65-
}
6649
}

0 commit comments

Comments
 (0)