1 | <?php |
||
37 | class HttpDispatcher |
||
38 | { |
||
39 | /** |
||
40 | * @var HttpAsyncClient |
||
41 | */ |
||
42 | private $httpClient; |
||
43 | |||
44 | /** |
||
45 | * @var UriFactory |
||
46 | */ |
||
47 | private $uriFactory; |
||
48 | |||
49 | /** |
||
50 | * Queued requests. |
||
51 | * |
||
52 | * @var RequestInterface[] |
||
53 | */ |
||
54 | private $queue = []; |
||
55 | |||
56 | /** |
||
57 | * Caching proxy server host names or IP addresses. |
||
58 | * |
||
59 | * @var UriInterface[] |
||
60 | */ |
||
61 | private $servers; |
||
62 | |||
63 | /** |
||
64 | * Application host name and optional base URL. |
||
65 | * |
||
66 | * @var UriInterface |
||
67 | */ |
||
68 | private $baseUri; |
||
69 | |||
70 | /** |
||
71 | * If you specify a custom HTTP client, make sure that it converts HTTP |
||
72 | * errors to exceptions. |
||
73 | * |
||
74 | * If your proxy server IPs can not be statically configured, extend this |
||
75 | * class and overwrite getServers. Be sure to have some caching in |
||
76 | * getServers. |
||
77 | * |
||
78 | * @param string[] $servers Caching proxy server hostnames or IP |
||
79 | * addresses, including port if not port 80. |
||
80 | * E.g. ['127.0.0.1:6081'] |
||
81 | * @param string $baseUri Default application hostname, optionally |
||
82 | * including base URL, for purge and refresh |
||
83 | * requests (optional). This is required if |
||
84 | * you purge and refresh paths instead of |
||
85 | * absolute URLs |
||
86 | * @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
||
87 | * client is supplied, a default one is created |
||
88 | * @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a |
||
89 | * default one is created |
||
90 | */ |
||
91 | 38 | public function __construct( |
|
109 | |||
110 | /** |
||
111 | * Queue invalidation request. |
||
112 | * |
||
113 | * @param RequestInterface $invalidationRequest |
||
114 | * @param bool $validateHost If false, do not validate that we either have a |
||
115 | * base uri or the invalidation request specifies |
||
116 | * the host |
||
117 | */ |
||
118 | 33 | public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) |
|
132 | |||
133 | /** |
||
134 | * Send all pending invalidation requests and make sure the requests have terminated and gather exceptions. |
||
135 | * |
||
136 | * @return int The number of cache invalidations performed per caching server |
||
137 | * |
||
138 | * @throws ExceptionCollection If any errors occurred during flush |
||
139 | */ |
||
140 | 33 | public function flush() |
|
179 | |||
180 | /** |
||
181 | * Get the list of servers to send invalidation requests to. |
||
182 | * |
||
183 | * @return UriInterface[] |
||
184 | */ |
||
185 | 32 | protected function getServers() |
|
189 | |||
190 | /** |
||
191 | * Duplicate a request for each caching server. |
||
192 | * |
||
193 | * @param RequestInterface $request The request to duplicate for each configured server |
||
194 | * |
||
195 | * @return RequestInterface[] |
||
196 | */ |
||
197 | 32 | private function fanOut(RequestInterface $request) |
|
246 | |||
247 | /** |
||
248 | * Set caching proxy server URI objects, validating them. |
||
249 | * |
||
250 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
||
251 | * addresses, including port if not port 80. |
||
252 | * E.g. ['127.0.0.1:6081'] |
||
253 | * |
||
254 | * @throws InvalidUrlException If server is invalid or contains URL |
||
255 | * parts other than scheme, host, port |
||
256 | */ |
||
257 | 38 | private function setServers(array $servers) |
|
264 | |||
265 | /** |
||
266 | * Set application base URI that will be prefixed to relative purge and |
||
267 | * refresh requests, and validate it. |
||
268 | * |
||
269 | * @param string $uriString Your application’s base URI |
||
270 | * |
||
271 | * @throws InvalidUrlException If the base URI is not a valid URI |
||
272 | */ |
||
273 | 35 | private function setBaseUri($uriString = null) |
|
283 | |||
284 | /** |
||
285 | * Filter a URL. |
||
286 | * |
||
287 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
||
288 | * for validity. You can specify what parts of the URL are allowed. |
||
289 | * |
||
290 | * @param string $uriString |
||
291 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
||
292 | * |
||
293 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
||
294 | * |
||
295 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
||
296 | * contains parts that are not expected |
||
297 | */ |
||
298 | 38 | private function filterUri($uriString, array $allowedParts = []) |
|
334 | |||
335 | /** |
||
336 | * Build a request signature based on the request data. Unique for every different request, identical |
||
337 | * for the same requests. |
||
338 | * |
||
339 | * This signature is used to avoid sending the same invalidation request twice. |
||
340 | * |
||
341 | * @param RequestInterface $request An invalidation request |
||
342 | * |
||
343 | * @return string A signature for this request |
||
344 | */ |
||
345 | 32 | private function getRequestSignature(RequestInterface $request) |
|
352 | } |
||
353 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: