Skip to content

Commit 0739cc4

Browse files
committed
Add HTTP client adapter
1 parent d9979f9 commit 0739cc4

File tree

13 files changed

+586
-263
lines changed

13 files changed

+586
-263
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"require-dev": {
3030
"guzzle/plugin-mock": "*",
31+
"guzzlehttp/guzzle": "~5",
3132
"mockery/mockery": "*",
3233
"monolog/monolog": "*",
3334
"symfony/process": "~2.3",

doc/cache-invalidator.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ implements ``\Psr\Log\LoggerInterface``. For instance, when using Monolog_::
195195
Then add the logger as a subscriber to the cache invalidator::
196196

197197
use FOS\HttpCache\EventListener\LogSubscriber;
198+
use FOS\HttpCache\CacheInvalidator;
199+
200+
$invalidator = new CacheInvalidator(
198201

199202
$subscriber = new LogSubscriber($monolog);
200203
$cacheInvalidator->getEventDispatcher()->addSubscriber($subscriber);

src/Exception/HttpClientException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\Exception;
4+
5+
class HttpClientException extends \InvalidArgumentException
6+
{
7+
public function __construct()
8+
{
9+
parent::__construct(
10+
'$client must be an instance of HttpClientInterface,'
11+
. 'a Guzzle client (deprecated) or null'
12+
);
13+
}
14+
}

src/HttpClient/BuzzAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\HttpClient\Adapter;
4+
5+
use FOS\HttpCache\HttpClient\HttpClientInterface;
6+
7+
class BuzzAdapter implements HttpClientInterface
8+
{
9+
10+
}

src/HttpClient/Guzzle3Adapter.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\HttpClient;
13+
14+
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\Exception\ProxyResponseException;
16+
use FOS\HttpCache\Exception\ProxyUnreachableException;
17+
use FOS\HttpCache\ProxyClient\Request\RequestQueue;
18+
use Guzzle\Common\Exception\ExceptionCollection as GuzzleExceptionCollection;
19+
use Guzzle\Http\Client;
20+
use Guzzle\Http\ClientInterface;
21+
use Guzzle\Http\Exception\CurlException;
22+
use Guzzle\Http\Exception\RequestException;
23+
24+
class Guzzle3Adapter implements HttpClientInterface
25+
{
26+
/**
27+
* Constructor
28+
*
29+
* @param ClientInterface $client Guzzle3 client (optional)
30+
*/
31+
public function __construct(ClientInterface $client = null)
32+
{
33+
$this->client = $client ?: new Client();
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function sendRequests(RequestQueue $requests)
40+
{
41+
$guzzleRequests = array();
42+
foreach ($requests as $request) {
43+
$guzzleRequests[] = $this->client->createRequest(
44+
$request->getMethod(),
45+
$request->getUrl(),
46+
$request->getHeaders()
47+
);
48+
}
49+
50+
try {
51+
$this->client->send($guzzleRequests);
52+
} catch (GuzzleExceptionCollection $e) {
53+
$this->handleException($e);
54+
}
55+
}
56+
57+
/**
58+
* Handle request exception
59+
*
60+
* @param GuzzleExceptionCollection $exceptions
61+
*
62+
* @throws ExceptionCollection
63+
*/
64+
private function handleException(GuzzleExceptionCollection $exceptions)
65+
{
66+
$collection = new ExceptionCollection();
67+
68+
foreach ($exceptions as $exception) {
69+
if ($exception instanceof CurlException) {
70+
// Caching proxy unreachable
71+
$e = ProxyUnreachableException::proxyUnreachable(
72+
$exception->getRequest()->getHost(),
73+
$exception->getMessage(),
74+
$exception
75+
);
76+
} elseif ($exception instanceof RequestException) {
77+
// Other error
78+
$e = ProxyResponseException::proxyResponse(
79+
$exception->getRequest()->getHost(),
80+
$exception->getCode(),
81+
$exception->getMessage(),
82+
$exception->getRequest()->getRawHeaders(),
83+
$exception
84+
);
85+
} else {
86+
// Unexpected exception type
87+
$e = $exception;
88+
}
89+
90+
$collection->add($e);
91+
}
92+
93+
throw $collection;
94+
}
95+
}

src/HttpClient/GuzzleAdapter.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\HttpClient;
13+
14+
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\ProxyClient\Request\RequestQueue;
16+
use GuzzleHttp\Client;
17+
use GuzzleHttp\ClientInterface;
18+
19+
class GuzzleAdapter implements HttpClientInterface
20+
{
21+
private $client;
22+
23+
public function __construct(ClientInterface $client = null)
24+
{
25+
$this->client = $client ?: new Client();
26+
}
27+
28+
/*
29+
* {@inheritdoc}
30+
*/
31+
public function sendRequests(RequestQueue $requests)
32+
{
33+
$exceptions = new ExceptionCollection();
34+
35+
foreach ($requests as $request) {
36+
$response = $this->client->send(
37+
$this->client->createRequest(
38+
$request->getMethod(),
39+
$request->getUrl(),
40+
array(
41+
'headers' => $request->getHeaders(),
42+
'future' => true,
43+
)
44+
)
45+
);
46+
47+
$response->then(
48+
function ($response) {
49+
// ignore
50+
},
51+
function ($error) use ($exceptions) {
52+
$exceptions->add($error);
53+
}
54+
);
55+
}
56+
}
57+
}
58+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\HttpClient;
13+
14+
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\ProxyClient\Request\RequestQueue;
16+
17+
/**
18+
* HTTP client that sends invalidation requests to a reverse caching proxy
19+
*/
20+
interface HttpClientInterface
21+
{
22+
/**
23+
* Send queued requests
24+
*
25+
* @param RequestQueue $requests
26+
*
27+
* @return int Number of invalidations
28+
*
29+
* @throws ExceptionCollection
30+
*/
31+
public function sendRequests(RequestQueue $requests);
32+
}

0 commit comments

Comments
 (0)