Skip to content

Commit 9a1f1dd

Browse files
committed
Use egeloen/http-adapter for HTTP library abstraction
2 parents b0304bb + f0b7e93 commit 9a1f1dd

File tree

13 files changed

+211
-348
lines changed

13 files changed

+211
-348
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ before_script:
3939
- sudo pip install -r doc/requirements.txt
4040

4141
script:
42-
- phpunit --coverage-clover=coverage.clover
42+
# *temporarily* exclude Nginx
43+
- phpunit --coverage-clover=coverage.clover --exclude-group nginx
4344
- make -C doc SPHINXOPTS='-nW' html
4445
- make -C doc spelling
4546

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
}
2222
],
2323
"require": {
24-
"php": ">=5.3.3",
25-
"guzzle/guzzle": "~3.0",
24+
"php": ">=5.4.8",
2625
"symfony/event-dispatcher": "~2.3",
27-
"symfony/options-resolver": "~2.3"
26+
"symfony/options-resolver": "~2.3",
27+
"egeloen/http-adapter": "0.7.x-dev"
2828
},
2929
"require-dev": {
3030
"guzzle/plugin-mock": "*",
31-
"guzzlehttp/guzzle": "~5",
3231
"mockery/mockery": "*",
3332
"monolog/monolog": "*",
3433
"symfony/process": "~2.3",

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/Http/InvalidationRequest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FOS\HttpCache\Http;
4+
5+
class InvalidationRequest
6+
{
7+
8+
}

src/HttpClient/BuzzAdapter.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/HttpClient/Guzzle3Adapter.php

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/HttpClient/GuzzleAdapter.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/HttpClient/HttpClientInterface.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/ProxyClient/AbstractProxyClient.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
namespace FOS\HttpCache\ProxyClient;
1313

14-
use FOS\HttpCache\Exception\HttpClientException;
15-
use FOS\HttpCache\HttpClient\Guzzle3Adapter;
16-
use FOS\HttpCache\HttpClient\HttpClientInterface;
14+
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\Exception\ProxyUnreachableException;
1716
use FOS\HttpCache\ProxyClient\Request\InvalidationRequest;
1817
use FOS\HttpCache\ProxyClient\Request\RequestQueue;
19-
use Guzzle\Http\ClientInterface;
18+
use Ivory\HttpAdapter\HttpAdapterFactory;
19+
use Ivory\HttpAdapter\HttpAdapterInterface;
20+
use Ivory\HttpAdapter\MultiHttpAdapterException;
2021

2122
/**
2223
* Abstract caching proxy client
@@ -28,9 +29,9 @@ abstract class AbstractProxyClient implements ProxyClientInterface
2829
/**
2930
* HTTP client
3031
*
31-
* @var ClientInterface
32+
* @var HttpAdapterInterface
3233
*/
33-
private $client;
34+
private $httpAdapter;
3435

3536
/**
3637
* Request queue
@@ -50,23 +51,15 @@ abstract class AbstractProxyClient implements ProxyClientInterface
5051
* requests (optional). This is required if
5152
* you purge and refresh paths instead of
5253
* absolute URLs.
53-
* @param HttpClientInterface|ClientInterface $client HTTP client (optional).
54-
* If no HTTP client is supplied, a default
55-
* one will be created.
54+
* @param HttpAdapterInterface $httpAdapter If no HTTP client is supplied, a
55+
* default one will be created.
5656
*/
57-
public function __construct(array $servers, $baseUrl = null, $client = null)
58-
{
59-
if ($client instanceof ClientInterface) {
60-
// Only for BC; Guzzle 3 for PHP 5.3 compatibility
61-
$this->client = new Guzzle3Adapter($client);
62-
} elseif ($client instanceof HttpClientInterface) {
63-
$this->client = $client;
64-
} elseif (null === $client) {
65-
$this->client = new Guzzle3Adapter();
66-
} else {
67-
throw new HttpClientException();
68-
}
69-
57+
public function __construct(
58+
array $servers,
59+
$baseUrl = null,
60+
HttpAdapterInterface $httpAdapter = null
61+
) {
62+
$this->httpAdapter = $httpAdapter ?: HttpAdapterFactory::guess();
7063
$this->initQueue($servers, $baseUrl);
7164
}
7265

@@ -81,9 +74,26 @@ public function flush()
8174

8275
$queue = clone $this->queue;
8376
$this->queue->clear();
84-
$this->client->sendRequests($queue);
77+
78+
try {
79+
$responses = $this->httpAdapter->sendRequests($queue->all());
80+
} catch (MultiHttpAdapterException $e) {
81+
$collection = new ExceptionCollection();
82+
foreach ($e->getExceptions() as $exception) {
83+
$collection->add(
84+
ProxyUnreachableException::proxyUnreachable(
85+
$exception->getRequest()->getHeader('Host'),
86+
$exception->getMessage(),
87+
null,
88+
$exception
89+
)
90+
);
91+
}
92+
93+
throw $collection;
94+
}
8595

86-
return $queue->count();
96+
return count($responses);
8797
}
8898

8999
protected function queueRequest($method, $url, array $headers = array())

src/ProxyClient/Request/RequestQueue.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace FOS\HttpCache\ProxyClient\Request;
1313

1414
use FOS\HttpCache\Exception\InvalidUrlException;
15+
use Ivory\HttpAdapter\Message\Request;
16+
use Ivory\HttpAdapter\Message\RequestInterface;
1517

1618
/**
1719
* A queue of requests to be sent to the HTTP caching server
1820
*/
19-
class RequestQueue implements \IteratorAggregate, \Countable
21+
class RequestQueue implements \Countable
2022
{
2123
private $servers = array();
2224
private $baseUrl;
@@ -52,8 +54,11 @@ public function count()
5254
{
5355
return count($this->queue);
5456
}
55-
56-
public function getIterator()
57+
58+
/**
59+
* @return RequestInterface[]
60+
*/
61+
public function all()
5762
{
5863
$requests = array();
5964
foreach ($this->queue as $queuedRequest) {
@@ -67,15 +72,18 @@ public function getIterator()
6772
}
6873

6974
foreach ($this->servers as $server) {
70-
$requests[] = new InvalidationRequest(
71-
$queuedRequest->getMethod(),
75+
$request = new Request(
7276
$this->combineUrls($server, $queuedRequest->getUrl()),
77+
$queuedRequest->getMethod(),
78+
'php://memory',
7379
$headers
7480
);
81+
82+
$requests[] = $request;
7583
}
7684
}
7785

78-
return new \ArrayIterator($requests);
86+
return $requests;
7987
}
8088

8189

0 commit comments

Comments
 (0)