Skip to content

Decouple from HTTP client library #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we use Guzzle 6 as our testing client, which requires PHP 5.5, we can no longer test 5.4 (we’re dropping 5.3 support anyway). Is this reason enough to switch to Guzzle 5? It’s, of course, a temporary problem: PHP 5.4 will be EOL in 3 months.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being EOL doesn't mean you have to drop 5.4. Since in the HTTP interfaces nothing indicates that you need 5.5, I would say try to test against 5.4 as well, even if it means you need a lower version. I would do the following: If the travis php version is 5.4 then remove guzzle6-adapter and install guzzle5-adapter. The guesser will always find the class for you. This way you can test against 5.4. See how it is done in php-http/discovery

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that in this case you have to require a message implementation as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, going to do that.

- 5.6
Expand All @@ -13,20 +12,21 @@ env:

matrix:
include:
- php: 5.5
env: VARNISH_VERSION=3.0
- php: 5.3
env: SYMFONY_VERSION=2.3.* VARNISH_VERSION=4.0 COMPOSER_FLAGS="--prefer-lowest"
- php: 5.4
env: SYMFONY_VERSION=2.3.* VARNISH_VERSION=3.0 COMPOSER_FLAGS="--prefer-lowest"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we completely drop varnish 3 support? otherwise we should keep this. maybe as a second --prefer-lowest run.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see Varnish 3 became THE lowest supported one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

install:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Composer setup should be done in install, not before_script.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

- if [[ "$TRAVIS_PHP_VERSION" == "5.4" || "$TRAVIS_PHP_VERSION" == "hhvm" ]]; then composer remove "php-http/guzzle6-adapter" --dev --no-update; fi
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" || "$TRAVIS_PHP_VERSION" == "hhvm" ]]; then composer require "php-http/guzzle5-adapter" --dev --no-update; fi
- composer update $COMPOSER_FLAGS --prefer-source --no-interaction

before_script:
# Install deps
- composer update $COMPOSER_FLAGS --dev --prefer-source --no-interaction
# Install Varnish
- curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
- echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-${VARNISH_VERSION}" | sudo tee -a /etc/apt/sources.list
Expand All @@ -36,7 +36,6 @@ before_script:
# Install NGINX
- sh ./tests/install-nginx.sh
# Starting webserver
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.3' ]; then ./tests/ci/install-apache.sh; fi"
- sh -c "if [ '$TRAVIS_PHP_VERSION' = 'hhvm' ]; then ./tests/ci/install-apache-hhvm.sh; fi"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets also delete the bash file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


script:
Expand Down
23 changes: 16 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ Changelog

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

2.0.0 (unreleased)
------------------

* Replace hard coupling on Guzzle HTTP client with HTTP adapter.
* The NGINX purge location is no longer passed as constructor argument but by
calling `setPurgeLocation()`.
* In ProxyTestCase, `getHttpClient()` has been replaced with `getHttpAdapter()`;
added HTTP method parameter to `getResponse()`.

1.4.0
-----

* Added symfony/http-kernel [HttpCache client](http://foshttpcache.readthedocs.org/en/latest/proxy-clients.html#symfony-client).
* Added [SymfonyTestCase](http://foshttpcache.readthedocs.org/en/latest/testing-your-application.html#symfonytestcase).
* Added symfony/http-kernel [HttpCache client](http://foshttpcache.readthedocs.org/en/stable/proxy-clients.html#symfony-client).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even this will at some point no longer work. can we have a 1.4 thing on readthedocs?

also, can we add some warning on the latest doc that this is now for the not-yet-stable 2.0 version? otherwise people with bookmarks will get confused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 1.4.0 to the docs. If we decide to bring out more updates for 1.0, this may become cumbersome. In that case, a 1.0 branch (and building the docs for that) are preferred.

* Added [SymfonyTestCase](http://foshttpcache.readthedocs.org/en/stable/testing-your-application.html#symfonytestcase).
* Removed unneeded files from dist packages.

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

* Added authentication support to user context subscribe.
* Fixed usage of deprecated Guzzle subtree splits.
* Fixed usage of deprecated Guzzle subtree splits.
* Fixed exposed cache tags.

1.3.0
-----

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

1.2.0
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ Symfony2-specific features to help with caching and caching proxies.
Features
--------

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

Documentation
-------------

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

License
-------
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
}
],
"require": {
"php": ">=5.3.3",
"guzzle/guzzle": "~3.8",
"php": ">=5.4.8",
"symfony/event-dispatcher": "~2.3",
"symfony/options-resolver": "~2.3"
"symfony/options-resolver": "~2.3",
"psr/http-message-implementation": "~1.0",
"php-http/adapter-implementation": "^0.1.0",
"php-http/discovery": "^0.1.1",
"php-http/message-decorator": "^0.1.0"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got rid of the Guzzle dependency. We now only have generic and virtual dependencies for the HTTP client.

},
"require-dev": {
"mockery/mockery": "~0.9.1",
"monolog/monolog": "~1.0",
"php-http/guzzle6-adapter": "^0.1.0",
"guzzlehttp/psr7": "^1.0",
"symfony/process": "~2.3",
"symfony/http-kernel": "~2.3"
},
Expand All @@ -47,7 +52,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.4.x-dev"
"dev-master": "2.0.x-dev"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the alias. Do we still want to create a 1.0 branch? I say let’s just stick to our old plan of not maintaining parallel branches (as long we can get away with doing so) and upping master to 2.0.

Copy link
Contributor

@dbu dbu Jun 13, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
3 changes: 2 additions & 1 deletion doc/includes/custom-headers.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
This allows you to pass headers that are different between purge requests.
If you want to add a header to all purge requests, such as ``Authorization``,
use a :ref:`custom Guzzle client <custom Guzzle client>` instead.
:ref:`configure the HTTP adapter <HTTP adapter configuration>` to use a
custom HTTP client instead.
7 changes: 7 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ FOSHttpCache

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

.. note::

This documentation is for the (upcoming) 2.0 of the library. For the stable
1.* version, please refer to the `stable documentation`_.

This library integrates your PHP applications with HTTP caching proxies such as
Varnish, NGINX or the Symfony HttpCache class. Use this library to send
invalidation requests from your application to the caching proxy and to test
Expand All @@ -28,3 +33,5 @@ Contents:

testing-your-application
contributing

.. _stable documentation: http://foshttpcache.readthedocs.org/en/stable/
94 changes: 67 additions & 27 deletions doc/proxy-clients.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
Caching Proxy Clients
=====================

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

.. _client setup:

Setup
-----

HTTP Adapter 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:

.. code-block:: bash

$ composer require php-http/guzzle6-adapter

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:

.. code-block:: bash

$ composer require guzzlehttp/psr7

Alternatively:

.. code-block:: bash

$ composer require zendframework/zend-diactoros

.. _HTTP adapter configuration:

HTTP Adapter 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)::

use GuzzleHttp\Client;

$config = [
// For instance, custom middlewares
];
$yourHttpClient = new Client($config);

Take your client and create a HTTP adapter from it::

use Http\Adapter\Guzzle6HttpAdapter;

$adapter = new Guzzle6HttpAdapter($client);

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

Varnish Client
~~~~~~~~~~~~~~

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

If you have configured NGINX to support purge requests at a separate location,
supply that location to the class as the third parameter::
call `setPurgeLocation()`::

use FOS\HttpCache\ProxyClient\Nginx;

$nginx = new Nginx($servers, $baseUri);
$nginx->setPurgeLocation('/purge');

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

.. note::

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

.. _custom guzzle client:

Custom Guzzle Client
--------------------

By default, the proxy clients instantiate a `Guzzle client`_ to communicate
with the caching proxy. If you need to customize the requests, for example to
send a basic authentication header, you can inject a custom Guzzle client::

use FOS\HttpCache\ProxyClient\Varnish;
use Guzzle\Http\Client;

$client = new Client();
$client->setDefaultOption('auth', array('username', 'password', 'Digest'));

$servers = array('10.0.0.1');
$varnish = new Varnish($servers, '/baseUrl', $client);

The Symfony client accepts a guzzle client as the 3rd parameter as well, NGINX
accepts it as 4th parameter.

.. _Guzzle client: http://guzzle3.readthedocs.org/
.. _HTTP Adapter: http://php-http.readthedocs.org/en/latest/
.. _PSR-7 message implementation: https://packagist.org/providers/psr/http-message-implementation
2 changes: 2 additions & 0 deletions doc/spelling_word_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ hostnames
http
invalidator
localhost
middleware
middlewares
nginx
roundtrip
symfony
Expand Down
2 changes: 1 addition & 1 deletion doc/testing-your-application.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ By having your test classes extend one of the test case classes, you get:
* an instance of this library’s client that is configured to talk to your
reverse proxy server. See reverse proxy specific sections for details;
* convenience methods for executing HTTP requests to your application:
``$this->getHttpClient()`` and ``$this->getResponse()``;
``$this->getHttpAdapter()`` and ``$this->getResponse()``;
* custom assertions ``assertHit`` and ``assertMiss`` for validating a cache
hit/miss.

Expand Down
9 changes: 8 additions & 1 deletion src/Exception/ExceptionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
* A collection of exceptions that might occur during the flush operation of a
* ProxyClientInterface implementation
*/
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
{
private $exceptions = array();

public function __construct(array $exceptions = array())
{
foreach ($exceptions as $exception) {
$this->add($exception);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the add method become private? kind of unrelated to this PR but when we start refactoring for 2.0 anyways...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

/**
* Add an exception to the collection
Expand Down
27 changes: 5 additions & 22 deletions src/Exception/InvalidUrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,30 @@ class InvalidUrlException extends InvalidArgumentException implements HttpCacheE
* @param string $url The invalid URL.
* @param string $reason Further explanation why the URL was invalid (optional)
*
* @return InvalidUrlException
* @return self
*/
public static function invalidUrl($url, $reason = null)
{
$msg = sprintf('URL "%s" is invalid.', $url);
if ($reason) {
$msg .= sprintf('Reason: %s', $reason);
$msg .= sprintf(' Reason: %s', $reason);
}

return new InvalidUrlException($msg);
return new self($msg);
}

/**
* @param string $server Invalid server
* @param array $allowed Allowed URL parts
*
* @return InvalidUrlException
* @return self
*/
public static function invalidUrlParts($server, array $allowed)
{
return new InvalidUrlException(sprintf(
return new self(sprintf(
'Server "%s" is invalid. Only %s URL parts are allowed.',
$server,
implode(', ', $allowed)
));
}

/**
* @param string $url Requested full URL
* @param string $scheme Requested URL scheme
* @param array $allowed Supported URL schemes
*
* @return InvalidUrlException
*/
public static function invalidUrlScheme($url, $scheme, array $allowed)
{
return new InvalidUrlException(sprintf(
'Host "%s" with scheme "%s" is invalid. Only schemes "%s" are supported',
$url,
$scheme,
implode(', ', $allowed)
));
}
}
Loading