Skip to content

Commit e3b7702

Browse files
committed
Update to PSR-7, Guzzle 6 and php-http/adapter
1 parent c145288 commit e3b7702

21 files changed

+355
-294
lines changed

composer.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@
2020
"homepage": "https://github.com/friendsofsymfony/FOSHttpCache/contributors"
2121
}
2222
],
23+
"repositories": [
24+
{
25+
"type": "vcs",
26+
"url": "https://github.com/ddeboer/guzzle6-adapter"
27+
}
28+
],
2329
"require": {
2430
"php": ">=5.4.8",
25-
"php": ">=5.3.3",
26-
"guzzle/guzzle": "~3.8",
2731
"symfony/event-dispatcher": "~2.3",
28-
"symfony/options-resolver": "~2.3"
32+
"symfony/options-resolver": "~2.3",
33+
"php-http/adapter": "dev-internal_separation",
34+
"guzzlehttp/psr7": "~1.0"
2935
},
3036
"require-dev": {
3137
"mockery/mockery": "~0.9.1",
3238
"monolog/monolog": "~1.0",
39+
"php-http/adapter-core": "dev-internal_separation",
40+
"php-http/guzzle6-adapter": "dev-implementation",
3341
"symfony/process": "~2.3",
3442
"symfony/http-kernel": "~2.3"
3543
},
@@ -46,5 +54,7 @@
4654
"branch-alias": {
4755
"dev-master": "1.3.x-dev"
4856
}
49-
}
57+
},
58+
"minimum-stability": "dev",
59+
"prefer-stable": true
5060
}

src/Exception/HttpClientException.php

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

src/Exception/ProxyResponseException.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,27 @@
1111

1212
namespace FOS\HttpCache\Exception;
1313

14+
use Http\Adapter\Exception\HttpAdapterException;
15+
1416
/**
1517
* Wrapping an error response from the caching proxy.
1618
*/
1719
class ProxyResponseException extends \RuntimeException implements HttpCacheExceptionInterface
1820
{
1921
/**
20-
* @param string $host The host name that was contacted.
21-
* @param string $statusCode The status code of the reply.
22-
* @param string $statusMessage The error message.
23-
* @param string $details Further details about the request that caused the error.
24-
* @param \Exception $previous The exception from the HTTP client.
22+
* @param HttpAdapterException $adapterException HTTP adapter exception.
2523
*
26-
* @return ProxyUnreachableException
24+
* @return ProxyResponseException
2725
*/
28-
public static function proxyResponse($host, $statusCode, $statusMessage, $details = '', \Exception $previous = null)
26+
public static function proxyResponse(HttpAdapterException $adapterException)
2927
{
3028
$message = sprintf(
3129
'%s error response "%s" from caching proxy at %s',
32-
$statusCode,
33-
$statusMessage,
34-
$host
30+
$adapterException->getResponse()->getStatusCode(),
31+
$adapterException->getResponse()->getReasonPhrase(),
32+
$adapterException->getRequest()->getHeaderLine('Host')
3533
);
36-
if ($details) {
37-
$message .= ". $details";
38-
}
3934

40-
return new ProxyResponseException($message, $statusCode, $previous);
35+
return new ProxyResponseException($message, 0, $adapterException);
4136
}
4237
}

src/Exception/ProxyUnreachableException.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,31 @@
1111

1212
namespace FOS\HttpCache\Exception;
1313

14+
use Http\Adapter\Exception\HttpAdapterException;
15+
1416
/**
1517
* Thrown when a request to the reverse caching proxy fails to establish a
1618
* connection.
1719
*/
1820
class ProxyUnreachableException extends \RuntimeException implements HttpCacheExceptionInterface
1921
{
2022
/**
21-
* @param string $host The host name that was contacted.
22-
* @param string $message The error message from the HTTP client.
23-
* @param string $details Further details about the request that caused the error.
24-
* @param \Exception $previous The exception from the HTTP client.
23+
* @param HttpAdapterException $adapterException
2524
*
2625
* @return ProxyUnreachableException
2726
*/
28-
public static function proxyUnreachable($host, $message, $details = '', \Exception $previous = null)
27+
public static function proxyUnreachable(HttpAdapterException $adapterException)
2928
{
3029
$message = sprintf(
3130
'Request to caching proxy at %s failed with message "%s"',
32-
$host,
33-
$message
31+
$adapterException->getRequest()->getHeaderLine('Host'),
32+
$adapterException->getMessage()
3433
);
35-
if ($details) {
36-
$message .= ". $details";
37-
}
38-
34+
3935
return new ProxyUnreachableException(
4036
$message,
4137
0,
42-
$previous
38+
$adapterException
4339
);
4440
}
4541
}

src/ProxyClient/AbstractProxyClient.php

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

1414
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\Exception\ProxyResponseException;
1516
use FOS\HttpCache\Exception\ProxyUnreachableException;
1617
use FOS\HttpCache\ProxyClient\Request\InvalidationRequest;
1718
use FOS\HttpCache\ProxyClient\Request\RequestQueue;
18-
use Ivory\HttpAdapter\HttpAdapterFactory;
19-
use Ivory\HttpAdapter\HttpAdapterInterface;
20-
use Ivory\HttpAdapter\MultiHttpAdapterException;
19+
use Http\Adapter\Exception\MultiHttpAdapterException;
20+
use Http\Adapter\Guzzle6HttpAdapter;
21+
use Http\Adapter\PsrHttpAdapter;
2122

2223
/**
2324
* Abstract caching proxy client
@@ -29,7 +30,7 @@ abstract class AbstractProxyClient implements ProxyClientInterface
2930
/**
3031
* HTTP client
3132
*
32-
* @var HttpAdapterInterface
33+
* @var PsrHttpAdapter
3334
*/
3435
private $httpAdapter;
3536

@@ -51,15 +52,15 @@ abstract class AbstractProxyClient implements ProxyClientInterface
5152
* requests (optional). This is required if
5253
* you purge and refresh paths instead of
5354
* absolute URLs.
54-
* @param HttpAdapterInterface $httpAdapter If no HTTP client is supplied, a
55+
* @param PsrHttpAdapter $httpAdapter If no HTTP adapter is supplied, a
5556
* default one will be created.
5657
*/
5758
public function __construct(
5859
array $servers,
5960
$baseUrl = null,
60-
HttpAdapterInterface $httpAdapter = null
61+
PsrHttpAdapter $httpAdapter = null
6162
) {
62-
$this->httpAdapter = $httpAdapter ?: HttpAdapterFactory::guess();
63+
$this->httpAdapter = $httpAdapter ?: new Guzzle6HttpAdapter();
6364
$this->initQueue($servers, $baseUrl);
6465
}
6566

@@ -80,20 +81,24 @@ public function flush()
8081
} catch (MultiHttpAdapterException $e) {
8182
$collection = new ExceptionCollection();
8283
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-
);
84+
// A workaround for php-http currently lacking differentiation
85+
// between client, server and networking errors.
86+
if (!$exception->getResponse()) {
87+
// Assume networking error if no response was returned.
88+
$collection->add(
89+
ProxyUnreachableException::proxyUnreachable($exception)
90+
);
91+
} else {
92+
$collection->add(
93+
ProxyResponseException::proxyResponse($exception)
94+
);
95+
}
9196
}
9297

9398
throw $collection;
9499
}
95100

96-
return count($responses);
101+
return count($queue);
97102
}
98103

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

src/ProxyClient/Nginx.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
1515
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
16-
use Guzzle\Http\ClientInterface;
16+
use Http\Adapter\PsrHttpAdapter;
1717

1818
/**
1919
* NGINX HTTP cache invalidator.
@@ -46,18 +46,18 @@ class Nginx extends AbstractProxyClient implements PurgeInterface, RefreshInterf
4646
* if you purge relative URLs and the domain
4747
* is not part of your `proxy_cache_key`
4848
* @param string $purgeLocation Path that triggers purge (optional).
49-
* @param ClientInterface $client HTTP client (optional). If no HTTP client
49+
* @param PsrHttpAdapter $adapter HTTP client (optional). If no HTTP client
5050
* is supplied, a default one will be
5151
* created.
5252
*/
5353
public function __construct(
5454
array $servers,
5555
$baseUrl = null,
5656
$purgeLocation = '',
57-
ClientInterface $client = null
57+
PsrHttpAdapter $adapter = null
5858
) {
5959
$this->purgeLocation = (string) $purgeLocation;
60-
parent::__construct($servers, $baseUrl, $client);
60+
parent::__construct($servers, $baseUrl, $adapter);
6161
}
6262

6363
/**
@@ -109,7 +109,7 @@ private function buildPurgeUrl($url)
109109
$pathStartAt = strpos($url, $urlParts['path']);
110110
$purgeUrl = substr($url, 0, $pathStartAt).$this->purgeLocation.substr($url, $pathStartAt);
111111
} else {
112-
$purgeUrl = $this->getBaseUrl().$this->purgeLocation.$url;
112+
$purgeUrl = $this->purgeLocation.$url;
113113
}
114114

115115
return $purgeUrl;

src/ProxyClient/Request/InvalidationRequest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
namespace FOS\HttpCache\ProxyClient\Request;
1313

14+
use Psr\Http\Message\RequestInterface;
15+
1416
/**
1517
* A request to the HTTP caching server
1618
*/
1719
class InvalidationRequest
1820
{
21+
22+
1923
private $method;
2024
private $url;
2125
private $headers;

src/ProxyClient/Request/RequestQueue.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
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;
15+
use Http\Message\MessageFactory;
16+
use GuzzleHttp\Psr7\Request;
17+
use Psr\Http\Message\RequestInterface;
18+
use Http\Adapter\Common\Message\MessageFactory as DefaultMessageFactory;
1719

1820
/**
1921
* A queue of requests to be sent to the HTTP caching server
@@ -30,8 +32,10 @@ class RequestQueue implements \Countable
3032
*/
3133
private $queue = array();
3234

33-
public function __construct(array $servers, $baseUrl = null)
34-
{
35+
public function __construct(
36+
array $servers,
37+
$baseUrl = null
38+
) {
3539
$this->setServers($servers);
3640
$this->setBaseUrl($baseUrl);
3741
}
@@ -73,9 +77,8 @@ public function all()
7377

7478
foreach ($this->servers as $server) {
7579
$request = new Request(
76-
$this->combineUrls($server, $queuedRequest->getUrl()),
7780
$queuedRequest->getMethod(),
78-
null,
81+
$this->combineUrls($server, $queuedRequest->getUrl()),
7982
$headers
8083
);
8184

src/Test/HttpClient/HttpAdapterMock.php

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

0 commit comments

Comments
 (0)