Skip to content

Commit c60666e

Browse files
committed
Merge pull request #272 from FriendsOfSymfony/make-discovery-optional
make php-http/discovery optional
2 parents 61c496c + 6ea3002 commit c60666e

File tree

5 files changed

+38
-64
lines changed

5 files changed

+38
-64
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
"symfony/event-dispatcher": "^2.3||^3.0",
2828
"symfony/options-resolver": "^2.3||^3.0",
2929
"php-http/client-implementation": "^1.0.0",
30-
"php-http/discovery": "~0.8",
3130
"php-http/message": "~0.3"
3231
},
3332
"require-dev": {
3433
"mockery/mockery": "~0.9.1",
3534
"monolog/monolog": "^1.0",
35+
"php-http/discovery": "~0.8",
3636
"php-http/guzzle6-adapter": "^0.3.1",
3737
"php-http/mock-client": "~0.1",
3838
"symfony/process": "^2.3||^3.0",
3939
"symfony/http-kernel": "^2.3||^3.0"
4040
},
4141
"suggest": {
42+
"php-http/discovery": "When not explicitly instantiating the php-http client for the proxy client",
4243
"monolog/monolog": "For logging issues while invalidating"
4344
},
4445
"autoload": {

src/ProxyClient/AbstractProxyClient.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Http\Client\HttpAsyncClient;
1616
use Http\Discovery\HttpAsyncClientDiscovery;
1717
use Http\Discovery\MessageFactoryDiscovery;
18+
use Http\Discovery\UriFactoryDiscovery;
1819
use Http\Message\MessageFactory;
20+
use Http\Message\UriFactory;
1921
use Psr\Http\Message\UriInterface;
2022
use Symfony\Component\OptionsResolver\OptionsResolver;
2123

@@ -62,18 +64,29 @@ abstract class AbstractProxyClient implements ProxyClientInterface
6264
* client is supplied, a default one is created.
6365
* @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied,
6466
* a default one is created.
67+
* @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a
68+
* default one is created.
6569
*/
6670
public function __construct(
6771
array $servers,
6872
array $options = [],
6973
HttpAsyncClient $httpClient = null,
70-
MessageFactory $messageFactory = null
74+
MessageFactory $messageFactory = null,
75+
UriFactory $uriFactory = null
7176
) {
77+
if ((!$httpClient || !$messageFactory || !$uriFactory) && !class_exists('Http\Discovery\HttpAsyncClientDiscovery')) {
78+
throw new \LogicException('Either specify the client and the message and uri factories or include php-http/discovery in your project');
79+
}
80+
7281
if (!$httpClient) {
7382
$httpClient = HttpAsyncClientDiscovery::find();
7483
}
84+
if (!$uriFactory) {
85+
$uriFactory = UriFactoryDiscovery::find();
86+
}
87+
7588
$this->options = $this->getDefaultOptions()->resolve($options);
76-
$this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient);
89+
$this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient, $uriFactory);
7790
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
7891
}
7992

src/ProxyClient/Http/HttpAdapter.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Http\Client\Exception\HttpException;
1212
use Http\Client\Exception\RequestException;
1313
use Http\Client\HttpAsyncClient;
14+
use Http\Message\UriFactory;
1415
use Http\Promise\Promise;
15-
use Http\Discovery\UriFactoryDiscovery;
1616
use Psr\Http\Message\RequestInterface;
1717
use Psr\Http\Message\UriInterface;
1818

@@ -28,6 +28,11 @@ class HttpAdapter
2828
*/
2929
private $httpClient;
3030

31+
/**
32+
* @var UriFactory
33+
*/
34+
private $uriFactory;
35+
3136
/**
3237
* Queued requests
3338
*
@@ -59,12 +64,15 @@ class HttpAdapter
5964
* you purge and refresh paths instead of
6065
* absolute URLs.
6166
* @param HttpAsyncClient $httpClient
67+
* @param UriFactory $uriFactory
6268
*/
63-
public function __construct(array $servers, $baseUri, HttpAsyncClient $httpClient)
69+
public function __construct(array $servers, $baseUri, HttpAsyncClient $httpClient, UriFactory $uriFactory)
6470
{
71+
$this->httpClient = $httpClient;
72+
$this->uriFactory = $uriFactory;
73+
6574
$this->setServers($servers);
6675
$this->setBaseUri($baseUri);
67-
$this->httpClient = $httpClient;
6876
}
6977

7078
/**
@@ -247,7 +255,7 @@ private function filterUri($uriString, array $allowedParts = [])
247255
}
248256

249257
try {
250-
$uri = UriFactoryDiscovery::find()->createUri($uriString);
258+
$uri = $this->uriFactory->createUri($uriString);
251259
} catch (\InvalidArgumentException $e) {
252260
throw InvalidUrlException::invalidUrl($uriString);
253261
}

src/ProxyClient/Symfony.php

+3-20
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,21 @@
1414
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
1515
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
1616
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
17-
use Http\Client\HttpAsyncClient;
1817
use Http\Message\MessageFactory;
19-
use Symfony\Component\OptionsResolver\OptionsResolver;
2018

2119
/**
2220
* Symfony HttpCache invalidator.
2321
*
22+
* Additional constructor options:
23+
* - purge_method: HTTP method that identifies purge requests.
24+
*
2425
* @author David de Boer <[email protected]>
2526
* @author David Buchmann <[email protected]>
2627
*/
2728
class Symfony extends AbstractProxyClient implements PurgeInterface, RefreshInterface
2829
{
2930
const HTTP_METHOD_REFRESH = 'GET';
3031

31-
/**
32-
* {@inheritDoc}
33-
*
34-
* When creating the client, you can configure options:
35-
*
36-
* - purge_method: HTTP method that identifies purge requests.
37-
*
38-
* @param array $options The purge_method that should be used.
39-
*/
40-
public function __construct(
41-
array $servers,
42-
array $options,
43-
HttpAsyncClient $httpClient = null,
44-
MessageFactory $messageFactory = null
45-
) {
46-
parent::__construct($servers, $options, $httpClient, $messageFactory);
47-
}
48-
4932
/**
5033
* {@inheritdoc}
5134
*/

src/ProxyClient/Varnish.php

+6-37
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
1818
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
1919
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface;
20-
use Http\Client\HttpAsyncClient;
2120
use Http\Message\MessageFactory;
22-
use Symfony\Component\OptionsResolver\OptionsResolver;
2321

2422
/**
2523
* Varnish HTTP cache invalidator.
2624
*
25+
* Additional constructor options:
26+
* - tags_header Header for tagging responses, defaults to X-Cache-Tags
27+
*
2728
* @author David de Boer <[email protected]>
2829
*/
2930
class Varnish extends AbstractProxyClient implements BanInterface, PurgeInterface, RefreshInterface, TagsInterface
@@ -46,38 +47,6 @@ class Varnish extends AbstractProxyClient implements BanInterface, PurgeInterfac
4647
self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL
4748
];
4849

49-
/**
50-
* Has a base URI been set?
51-
*
52-
* @var bool
53-
*/
54-
private $baseUriSet;
55-
56-
/**
57-
* @var string
58-
*/
59-
private $tagsHeader;
60-
61-
/**
62-
* {@inheritdoc}
63-
*
64-
* Additional options:
65-
*
66-
* - tags_header Header for tagging responses, defaults to X-Cache-Tags
67-
*
68-
* @param string $tagsHeader
69-
*/
70-
public function __construct(
71-
array $servers,
72-
array $options = [],
73-
HttpAsyncClient $httpClient = null,
74-
MessageFactory $messageFactory = null
75-
) {
76-
parent::__construct($servers, $options, $httpClient, $messageFactory);
77-
$this->baseUriSet = $this->options['base_uri'] !== null;
78-
$this->tagsHeader = $this->options['tags_header'];
79-
}
80-
8150
/**
8251
* Set the default headers that get merged with the provided headers in self::ban().
8352
*
@@ -107,7 +76,7 @@ public function invalidateTags(array $tags)
10776
{
10877
$tagExpression = sprintf('(%s)(,.+)?$', implode('|', array_map('preg_quote', $this->escapeTags($tags))));
10978

110-
return $this->ban([$this->tagsHeader => $tagExpression]);
79+
return $this->ban([$this->options['tags_header'] => $tagExpression]);
11180
}
11281

11382
/**
@@ -125,7 +94,7 @@ public function getTagsHeaderValue(array $tags)
12594
*/
12695
public function getTagsHeaderName()
12796
{
128-
return $this->tagsHeader;
97+
return $this->options['tags_header'];
12998
}
13099

131100
/**
@@ -215,7 +184,7 @@ protected function queueRequest($method, $url, array $headers = [])
215184
$request = $this->messageFactory->createRequest($method, $url, $headers);
216185

217186
if (self::HTTP_METHOD_BAN !== $method
218-
&& !$this->baseUriSet
187+
&& null === $this->options['base_uri']
219188
&& !$request->getHeaderLine('Host')
220189
) {
221190
throw MissingHostException::missingHost($url);

0 commit comments

Comments
 (0)