1 | <?php |
||
34 | class CacheInvalidator |
||
35 | { |
||
36 | /** |
||
37 | * Value to check support of invalidatePath operation. |
||
38 | */ |
||
39 | const PATH = 'path'; |
||
40 | |||
41 | /** |
||
42 | * Value to check support of refreshPath operation. |
||
43 | */ |
||
44 | const REFRESH = 'refresh'; |
||
45 | |||
46 | /** |
||
47 | * Value to check support of invalidate and invalidateRegex operations. |
||
48 | */ |
||
49 | const INVALIDATE = 'invalidate'; |
||
50 | |||
51 | /** |
||
52 | * Value to check support of invalidateTags operation. |
||
53 | */ |
||
54 | const TAGS = 'tags'; |
||
55 | |||
56 | /** |
||
57 | * @var ProxyClient |
||
58 | */ |
||
59 | private $cache; |
||
60 | |||
61 | /** |
||
62 | * @var EventDispatcherInterface |
||
63 | */ |
||
64 | private $eventDispatcher; |
||
65 | |||
66 | /** |
||
67 | * Constructor. |
||
68 | * |
||
69 | * @param ProxyClient $cache HTTP cache |
||
70 | */ |
||
71 | 12 | public function __construct(ProxyClient $cache) |
|
75 | |||
76 | /** |
||
77 | * Check whether this invalidator instance supports the specified |
||
78 | * operation. |
||
79 | * |
||
80 | * Support for PATH means invalidatePath will work, REFRESH means |
||
81 | * refreshPath works, TAGS means that invalidateTags works and |
||
82 | * INVALIDATE is for the invalidate and invalidateRegex methods. |
||
83 | * |
||
84 | * @param string $operation one of the class constants |
||
85 | * |
||
86 | * @return bool |
||
87 | * |
||
88 | * @throws InvalidArgumentException |
||
89 | */ |
||
90 | 3 | public function supports($operation) |
|
91 | { |
||
92 | switch ($operation) { |
||
93 | 3 | case self::PATH: |
|
94 | 2 | return $this->cache instanceof PurgeCapable; |
|
95 | 3 | case self::REFRESH: |
|
96 | 2 | return $this->cache instanceof RefreshCapable; |
|
97 | 3 | case self::INVALIDATE: |
|
98 | 2 | return $this->cache instanceof BanCapable; |
|
99 | 3 | case self::TAGS: |
|
100 | 2 | return $this->cache instanceof TagCapable; |
|
101 | default: |
||
102 | 1 | throw new InvalidArgumentException('Unknown operation '.$operation); |
|
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Set event dispatcher - may only be called once. |
||
108 | * |
||
109 | * @param EventDispatcherInterface $eventDispatcher |
||
110 | * |
||
111 | * @return $this |
||
112 | * |
||
113 | * @throws \Exception When trying to override the event dispatcher |
||
114 | */ |
||
115 | 2 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
124 | |||
125 | /** |
||
126 | * Get the event dispatcher used by the cache invalidator. |
||
127 | * |
||
128 | * @return EventDispatcherInterface |
||
129 | */ |
||
130 | 2 | public function getEventDispatcher() |
|
138 | |||
139 | /** |
||
140 | * Invalidate a path or URL. |
||
141 | * |
||
142 | * @param string $path Path or URL |
||
143 | * @param array $headers HTTP headers (optional) |
||
144 | * |
||
145 | * @throws UnsupportedProxyOperationException |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | 1 | public function invalidatePath($path, array $headers = []) |
|
150 | { |
||
151 | 1 | if (!$this->cache instanceof PurgeCapable) { |
|
152 | throw UnsupportedProxyOperationException::cacheDoesNotImplement('PURGE'); |
||
153 | } |
||
154 | |||
155 | 1 | $this->cache->purge($path, $headers); |
|
156 | |||
157 | 1 | return $this; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Refresh a path or URL. |
||
162 | * |
||
163 | * @param string $path Path or URL |
||
164 | * @param array $headers HTTP headers (optional) |
||
165 | * |
||
166 | * @see RefreshCapable::refresh() |
||
167 | * |
||
168 | * @throws UnsupportedProxyOperationException |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | 1 | public function refreshPath($path, array $headers = []) |
|
173 | { |
||
174 | 1 | if (!$this->cache instanceof RefreshCapable) { |
|
175 | throw UnsupportedProxyOperationException::cacheDoesNotImplement('REFRESH'); |
||
176 | } |
||
177 | |||
178 | 1 | $this->cache->refresh($path, $headers); |
|
179 | |||
180 | 1 | return $this; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Invalidate all cached objects matching the provided HTTP headers. |
||
185 | * |
||
186 | * Each header is a a POSIX regular expression, for example |
||
187 | * ['X-Host' => '^(www\.)?(this|that)\.com$'] |
||
188 | * |
||
189 | * @see BanCapable::ban() |
||
190 | * |
||
191 | * @param array $headers HTTP headers that path must match to be banned |
||
192 | * |
||
193 | * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | 1 | public function invalidate(array $headers) |
|
198 | { |
||
199 | 1 | if (!$this->cache instanceof BanCapable) { |
|
200 | throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN'); |
||
201 | } |
||
202 | |||
203 | 1 | $this->cache->ban($headers); |
|
204 | |||
205 | 1 | return $this; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Remove/Expire cache objects based on cache tags. |
||
210 | * |
||
211 | * @see TagCapable::tags() |
||
212 | * |
||
213 | * @param array $tags Tags that should be removed/expired from the cache |
||
214 | * |
||
215 | * @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | 2 | public function invalidateTags(array $tags) |
|
220 | { |
||
221 | 2 | if (!$this->cache instanceof TagCapable) { |
|
222 | throw UnsupportedProxyOperationException::cacheDoesNotImplement('Tags'); |
||
223 | } |
||
224 | 2 | $this->cache->invalidateTags($tags); |
|
225 | |||
226 | 2 | return $this; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Invalidate URLs based on a regular expression for the URI, an optional |
||
231 | * content type and optional limit to certain hosts. |
||
232 | * |
||
233 | * The hosts parameter can either be a regular expression, e.g. |
||
234 | * '^(www\.)?(this|that)\.com$' or an array of exact host names, e.g. |
||
235 | * ['example.com', 'other.net']. If the parameter is empty, all hosts |
||
236 | * are matched. |
||
237 | * |
||
238 | * @see BanCapable::banPath() |
||
239 | * |
||
240 | * @param string $path Regular expression pattern for URI to |
||
241 | * invalidate |
||
242 | * @param string $contentType Regular expression pattern for the content |
||
243 | * type to limit banning, for instance 'text' |
||
244 | * @param array|string $hosts Regular expression of a host name or list of |
||
245 | * exact host names to limit banning |
||
246 | * |
||
247 | * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | 1 | public function invalidateRegex($path, $contentType = null, $hosts = null) |
|
252 | { |
||
253 | 1 | if (!$this->cache instanceof BanCapable) { |
|
254 | throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN'); |
||
255 | } |
||
256 | |||
257 | 1 | $this->cache->banPath($path, $contentType, $hosts); |
|
258 | |||
259 | 1 | return $this; |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * Send all pending invalidation requests. |
||
264 | * |
||
265 | * @return int The number of cache invalidations performed per caching server |
||
266 | * |
||
267 | * @throws ExceptionCollection If any errors occurred during flush |
||
268 | */ |
||
269 | 3 | public function flush() |
|
287 | } |
||
288 |