1 | <?php |
||
24 | class HttpAdapter |
||
25 | { |
||
26 | /** |
||
27 | * @var HttpAsyncClient |
||
28 | */ |
||
29 | private $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Queued requests |
||
33 | * |
||
34 | * @var RequestInterface[] |
||
35 | */ |
||
36 | private $queue = []; |
||
37 | |||
38 | /** |
||
39 | * Caching proxy server host names or IP addresses. |
||
40 | * |
||
41 | * @var UriInterface[] |
||
42 | */ |
||
43 | private $servers; |
||
44 | |||
45 | /** |
||
46 | * Application host name and optional base URL. |
||
47 | * |
||
48 | * @var UriInterface |
||
49 | */ |
||
50 | private $baseUri; |
||
51 | |||
52 | /** |
||
53 | * @param string[] $servers Caching proxy server hostnames or IP |
||
54 | * addresses, including port if not port 80. |
||
55 | * E.g. ['127.0.0.1:6081'] |
||
56 | * @param string $baseUri Default application hostname, optionally |
||
57 | * including base URL, for purge and refresh |
||
58 | * requests (optional). This is required if |
||
59 | * you purge and refresh paths instead of |
||
60 | * absolute URLs. |
||
61 | * @param HttpAsyncClient $httpClient |
||
62 | */ |
||
63 | 19 | public function __construct(array $servers, $baseUri, HttpAsyncClient $httpClient) |
|
69 | |||
70 | /** |
||
71 | * Queue invalidation request. |
||
72 | * |
||
73 | * @param RequestInterface $invalidationRequest |
||
74 | */ |
||
75 | 16 | public function invalidate(RequestInterface $invalidationRequest) |
|
85 | |||
86 | /** |
||
87 | * Send all pending invalidation requests and make sure the requests have terminated and gather exceptions. |
||
88 | * |
||
89 | * @return int The number of cache invalidations performed per caching server. |
||
90 | * |
||
91 | * @throws ExceptionCollection If any errors occurred during flush. |
||
92 | */ |
||
93 | 17 | public function flush() |
|
130 | |||
131 | /** |
||
132 | * Duplicate a request for each caching server |
||
133 | * |
||
134 | * @param RequestInterface $request The request to duplicate for each configured server |
||
135 | * |
||
136 | * @return RequestInterface[] |
||
137 | */ |
||
138 | 16 | private function fanOut(RequestInterface $request) |
|
139 | { |
||
140 | 16 | $requests = []; |
|
141 | |||
142 | 16 | $uri = $request->getUri(); |
|
143 | |||
144 | // If a base URI is configured, try to make partial invalidation |
||
145 | // requests complete. |
||
146 | 16 | if ($this->baseUri) { |
|
147 | 15 | if ($uri->getHost()) { |
|
148 | // Absolute URI: does it already have a scheme? |
||
149 | if (!$uri->getScheme() && $this->baseUri->getScheme() !== '') { |
||
150 | $uri = $uri->withScheme($this->baseUri->getScheme()); |
||
151 | } |
||
152 | } else { |
||
153 | // Relative URI |
||
154 | 15 | if ($this->baseUri->getHost() !== '') { |
|
155 | 15 | $uri = $uri->withHost($this->baseUri->getHost()); |
|
156 | 15 | } |
|
157 | |||
158 | 15 | if ($this->baseUri->getPort()) { |
|
159 | $uri = $uri->withPort($this->baseUri->getPort()); |
||
160 | } |
||
161 | |||
162 | // Base path |
||
163 | 15 | if ($this->baseUri->getPath() !== '') { |
|
164 | 1 | $path = $this->baseUri->getPath() . '/' . ltrim($uri->getPath(), '/'); |
|
165 | 1 | $uri = $uri->withPath($path); |
|
166 | 1 | } |
|
167 | } |
||
168 | 15 | } |
|
169 | |||
170 | // Close connections to make sure invalidation (PURGE/BAN) requests |
||
171 | // will not interfere with content (GET) requests. |
||
172 | 16 | $request = $request->withUri($uri)->withHeader('Connection', 'Close'); |
|
173 | |||
174 | // Create a request to each caching proxy server |
||
175 | 16 | foreach ($this->servers as $server) { |
|
176 | 16 | $requests[] = $request->withUri( |
|
177 | $uri |
||
178 | 16 | ->withScheme($server->getScheme()) |
|
179 | 16 | ->withHost($server->getHost()) |
|
180 | 16 | ->withPort($server->getPort()) |
|
181 | 16 | , |
|
182 | true // Preserve application Host header |
||
183 | 16 | ); |
|
184 | 16 | } |
|
185 | |||
186 | 16 | return $requests; |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Set caching proxy server URI objects, validating them. |
||
191 | * |
||
192 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
||
193 | * addresses, including port if not port 80. |
||
194 | * E.g. ['127.0.0.1:6081'] |
||
195 | * |
||
196 | * @throws InvalidUrlException If server is invalid or contains URL |
||
197 | * parts other than scheme, host, port |
||
198 | */ |
||
199 | 19 | private function setServers(array $servers) |
|
206 | |||
207 | /** |
||
208 | * Set application base URI that will be prefixed to relative purge and |
||
209 | * refresh requests, and validate it. |
||
210 | * |
||
211 | * @param string $uriString Your application’s base URI |
||
212 | * |
||
213 | * @throws InvalidUrlException If the base URI is not a valid URI. |
||
214 | */ |
||
215 | 19 | private function setBaseUri($uriString = null) |
|
225 | |||
226 | /** |
||
227 | * Filter a URL |
||
228 | * |
||
229 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
||
230 | * for validity. You can specify what parts of the URL are allowed. |
||
231 | * |
||
232 | * @param string $uriString |
||
233 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
||
234 | * |
||
235 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
||
236 | * |
||
237 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
||
238 | * contains parts that are not expected. |
||
239 | */ |
||
240 | 19 | private function filterUri($uriString, array $allowedParts = []) |
|
241 | { |
||
242 | // Creating a PSR-7 URI without scheme (with parse_url) results in the |
||
243 | // original hostname to be seen as path. So first add a scheme if none |
||
244 | // is given. |
||
245 | 19 | if (false === strpos($uriString, '://')) { |
|
246 | 19 | $uriString = sprintf('%s://%s', 'http', $uriString); |
|
247 | 19 | } |
|
248 | |||
249 | try { |
||
250 | 19 | $uri = UriFactoryDiscovery::find()->createUri($uriString); |
|
251 | 19 | } catch (\InvalidArgumentException $e) { |
|
252 | throw InvalidUrlException::invalidUrl($uriString); |
||
253 | } |
||
254 | |||
255 | 19 | if (!$uri->getScheme()) { |
|
256 | throw InvalidUrlException::invalidUrl($uriString, 'empty scheme'); |
||
257 | } |
||
258 | |||
259 | 19 | if (count($allowedParts) > 0) { |
|
260 | 19 | $parts = parse_url((string) $uri); |
|
261 | 19 | $diff = array_diff(array_keys($parts), $allowedParts); |
|
262 | 19 | if (count($diff) > 0) { |
|
263 | throw InvalidUrlException::invalidUrlParts($uriString, $allowedParts); |
||
264 | } |
||
265 | 19 | } |
|
266 | |||
267 | 19 | return $uri; |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Build a request signature based on the request data. Unique for every different request, identical |
||
272 | * for the same requests. |
||
273 | * |
||
274 | * This signature is used to avoid sending the same invalidation request twice. |
||
275 | * |
||
276 | * @param RequestInterface $request An invalidation request. |
||
277 | * |
||
278 | * @return string A signature for this request. |
||
279 | */ |
||
280 | 16 | private function getRequestSignature(RequestInterface $request) |
|
287 | } |
||
288 |
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: