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\ProxyClient; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\Http\HttpAdapter; |
15
|
|
|
use Http\Client\HttpAsyncClient; |
16
|
|
|
use Http\Discovery\HttpAsyncClientDiscovery; |
17
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
18
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
19
|
|
|
use Http\Message\MessageFactory; |
20
|
|
|
use Http\Message\UriFactory; |
21
|
|
|
use Psr\Http\Message\UriInterface; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract HTTP based caching proxy client. |
26
|
|
|
* |
27
|
|
|
* @author David de Boer <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractProxyClient implements ProxyClientInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* HTTP client adapter. |
33
|
|
|
* |
34
|
|
|
* @var HttpAdapter |
35
|
|
|
*/ |
36
|
|
|
protected $httpAdapter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var MessageFactory |
40
|
|
|
*/ |
41
|
|
|
protected $messageFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The options configured in the constructor argument or default values. |
45
|
|
|
* |
46
|
|
|
* @var array The resolved options. |
47
|
|
|
*/ |
48
|
|
|
protected $options; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor. |
52
|
|
|
* |
53
|
|
|
* Supported options: |
54
|
|
|
* |
55
|
|
|
* - base_uri Default application hostname, optionally including base URL, |
56
|
|
|
* for purge and refresh requests (optional). This is required if you |
57
|
|
|
* purge and refresh paths instead of absolute URLs. |
58
|
|
|
* |
59
|
|
|
* @param array $servers Caching proxy server hostnames or IP |
60
|
|
|
* addresses, including port if not port 80. |
61
|
|
|
* E.g. ['127.0.0.1:6081'] |
62
|
|
|
* @param array $options List of options for the client. |
63
|
|
|
* @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
64
|
|
|
* client is supplied, a default one is created. |
65
|
|
|
* @param MessageFactory|null $messageFactory Factory for PSR-7 messages. If none supplied, |
66
|
|
|
* a default one is created. |
67
|
|
|
* @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a |
68
|
|
|
* default one is created. |
69
|
|
|
*/ |
70
|
48 |
|
public function __construct( |
71
|
|
|
array $servers, |
72
|
|
|
array $options = [], |
73
|
|
|
HttpAsyncClient $httpClient = null, |
74
|
|
|
MessageFactory $messageFactory = null, |
75
|
|
|
UriFactory $uriFactory = null |
76
|
|
|
) { |
77
|
48 |
|
if (!$httpClient) { |
78
|
27 |
|
$httpClient = HttpAsyncClientDiscovery::find(); |
79
|
27 |
|
} |
80
|
48 |
|
if (!$uriFactory) { |
81
|
48 |
|
$uriFactory = UriFactoryDiscovery::find(); |
82
|
48 |
|
} |
83
|
|
|
|
84
|
48 |
|
$this->options = $this->getDefaultOptions()->resolve($options); |
85
|
48 |
|
$this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient, $uriFactory); |
86
|
45 |
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
87
|
45 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
39 |
|
public function flush() |
93
|
|
|
{ |
94
|
39 |
|
return $this->httpAdapter->flush(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get options resolver with default settings. |
99
|
|
|
* |
100
|
|
|
* @return OptionsResolver |
101
|
|
|
*/ |
102
|
48 |
|
protected function getDefaultOptions() |
103
|
|
|
{ |
104
|
48 |
|
$resolver = new OptionsResolver(); |
105
|
48 |
|
$resolver->setDefaults(['base_uri' => null]); |
106
|
|
|
|
107
|
48 |
|
return $resolver; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create a request and queue it with the HttpAdapter. |
112
|
|
|
* |
113
|
|
|
* @param string $method |
114
|
|
|
* @param string|UriInterface $url |
115
|
|
|
* @param array $headers |
116
|
|
|
*/ |
117
|
13 |
|
protected function queueRequest($method, $url, array $headers) |
118
|
|
|
{ |
119
|
13 |
|
$this->httpAdapter->invalidate( |
120
|
13 |
|
$this->messageFactory->createRequest($method, $url, $headers) |
121
|
13 |
|
); |
122
|
13 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Make sure that the tags are valid. |
126
|
|
|
* |
127
|
|
|
* Reusable function for proxy clients. |
128
|
|
|
* Escapes `,` and `\n` (newline) characters. |
129
|
|
|
* |
130
|
|
|
* @param array $tags The tags to escape. |
131
|
|
|
* |
132
|
|
|
* @return array Sane tags. |
133
|
|
|
*/ |
134
|
|
|
protected function escapeTags(array $tags) |
135
|
|
|
{ |
136
|
4 |
|
array_walk($tags, function (&$tag) { |
137
|
4 |
|
$tag = str_replace([',', "\n"], ['_', '_'], $tag); |
138
|
4 |
|
}); |
139
|
|
|
|
140
|
4 |
|
return $tags; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|