1 | <?php |
||
32 | class HttpAdapter |
||
33 | { |
||
34 | /** |
||
35 | * @var HttpAsyncClient |
||
36 | */ |
||
37 | private $httpClient; |
||
38 | |||
39 | /** |
||
40 | * @var UriFactory |
||
41 | */ |
||
42 | private $uriFactory; |
||
43 | |||
44 | /** |
||
45 | * Queued requests. |
||
46 | * |
||
47 | * @var RequestInterface[] |
||
48 | */ |
||
49 | private $queue = []; |
||
50 | |||
51 | /** |
||
52 | * Caching proxy server host names or IP addresses. |
||
53 | * |
||
54 | * @var UriInterface[] |
||
55 | */ |
||
56 | private $servers; |
||
57 | |||
58 | /** |
||
59 | * Application host name and optional base URL. |
||
60 | * |
||
61 | * @var UriInterface |
||
62 | */ |
||
63 | private $baseUri; |
||
64 | |||
65 | /** |
||
66 | * @param string[] $servers Caching proxy server hostnames or IP |
||
67 | * addresses, including port if not port 80. |
||
68 | * E.g. ['127.0.0.1:6081'] |
||
69 | * @param string $baseUri Default application hostname, optionally |
||
70 | * including base URL, for purge and refresh |
||
71 | * requests (optional). This is required if |
||
72 | * you purge and refresh paths instead of |
||
73 | * absolute URLs. |
||
74 | * @param HttpAsyncClient $httpClient |
||
75 | * @param UriFactory $uriFactory |
||
76 | */ |
||
77 | 32 | public function __construct(array $servers, $baseUri, HttpAsyncClient $httpClient, UriFactory $uriFactory) |
|
85 | |||
86 | /** |
||
87 | * Queue invalidation request. |
||
88 | * |
||
89 | * @param RequestInterface $invalidationRequest |
||
90 | */ |
||
91 | 21 | public function invalidate(RequestInterface $invalidationRequest) |
|
101 | |||
102 | /** |
||
103 | * Send all pending invalidation requests and make sure the requests have terminated and gather exceptions. |
||
104 | * |
||
105 | * @return int The number of cache invalidations performed per caching server. |
||
106 | * |
||
107 | * @throws ExceptionCollection If any errors occurred during flush. |
||
108 | */ |
||
109 | 22 | public function flush() |
|
146 | |||
147 | /** |
||
148 | * Duplicate a request for each caching server. |
||
149 | * |
||
150 | * @param RequestInterface $request The request to duplicate for each configured server |
||
151 | * |
||
152 | * @return RequestInterface[] |
||
153 | */ |
||
154 | 21 | private function fanOut(RequestInterface $request) |
|
155 | { |
||
156 | 21 | $requests = []; |
|
157 | |||
158 | 21 | $uri = $request->getUri(); |
|
159 | |||
160 | // If a base URI is configured, try to make partial invalidation |
||
161 | // requests complete. |
||
162 | 21 | if ($this->baseUri) { |
|
163 | 18 | if ($uri->getHost()) { |
|
164 | // Absolute URI: does it already have a scheme? |
||
165 | if (!$uri->getScheme() && $this->baseUri->getScheme() !== '') { |
||
166 | $uri = $uri->withScheme($this->baseUri->getScheme()); |
||
167 | } |
||
168 | } else { |
||
169 | // Relative URI |
||
170 | 18 | if ($this->baseUri->getHost() !== '') { |
|
171 | 18 | $uri = $uri->withHost($this->baseUri->getHost()); |
|
172 | 18 | } |
|
173 | |||
174 | 18 | if ($this->baseUri->getPort()) { |
|
175 | 2 | $uri = $uri->withPort($this->baseUri->getPort()); |
|
176 | 2 | } |
|
177 | |||
178 | // Base path |
||
179 | 18 | if ($this->baseUri->getPath() !== '') { |
|
180 | 1 | $path = $this->baseUri->getPath().'/'.ltrim($uri->getPath(), '/'); |
|
181 | 1 | $uri = $uri->withPath($path); |
|
182 | 1 | } |
|
183 | } |
||
184 | 18 | } |
|
185 | |||
186 | // Close connections to make sure invalidation (PURGE/BAN) requests |
||
187 | // will not interfere with content (GET) requests. |
||
188 | 21 | $request = $request->withUri($uri)->withHeader('Connection', 'Close'); |
|
189 | |||
190 | // Create a request to each caching proxy server |
||
191 | 21 | foreach ($this->servers as $server) { |
|
192 | 21 | $requests[] = $request->withUri( |
|
193 | $uri |
||
194 | 21 | ->withScheme($server->getScheme()) |
|
195 | 21 | ->withHost($server->getHost()) |
|
196 | 21 | ->withPort($server->getPort()), |
|
197 | true // Preserve application Host header |
||
198 | 21 | ); |
|
199 | 21 | } |
|
200 | |||
201 | 21 | return $requests; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Set caching proxy server URI objects, validating them. |
||
206 | * |
||
207 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
||
208 | * addresses, including port if not port 80. |
||
209 | * E.g. ['127.0.0.1:6081'] |
||
210 | * |
||
211 | * @throws InvalidUrlException If server is invalid or contains URL |
||
212 | * parts other than scheme, host, port |
||
213 | */ |
||
214 | 32 | private function setServers(array $servers) |
|
221 | |||
222 | /** |
||
223 | * Set application base URI that will be prefixed to relative purge and |
||
224 | * refresh requests, and validate it. |
||
225 | * |
||
226 | * @param string $uriString Your application’s base URI |
||
227 | * |
||
228 | * @throws InvalidUrlException If the base URI is not a valid URI. |
||
229 | */ |
||
230 | 29 | private function setBaseUri($uriString = null) |
|
240 | |||
241 | /** |
||
242 | * Filter a URL. |
||
243 | * |
||
244 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
||
245 | * for validity. You can specify what parts of the URL are allowed. |
||
246 | * |
||
247 | * @param string $uriString |
||
248 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
||
249 | * |
||
250 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
||
251 | * |
||
252 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
||
253 | * contains parts that are not expected. |
||
254 | */ |
||
255 | 32 | private function filterUri($uriString, array $allowedParts = []) |
|
284 | |||
285 | /** |
||
286 | * Build a request signature based on the request data. Unique for every different request, identical |
||
287 | * for the same requests. |
||
288 | * |
||
289 | * This signature is used to avoid sending the same invalidation request twice. |
||
290 | * |
||
291 | * @param RequestInterface $request An invalidation request. |
||
292 | * |
||
293 | * @return string A signature for this request. |
||
294 | */ |
||
295 | 21 | private function getRequestSignature(RequestInterface $request) |
|
302 | } |
||
303 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: