44
55use Psr \Log \LoggerAwareInterface ;
66use Psr \Log \LoggerAwareTrait ;
7- use Psr \Log \LoggerInterface ;
87use TYPO3 \CMS \Core \Utility \GeneralUtility ;
8+ use TYPO3 \CMS \Extbase \Configuration \ConfigurationManager ;
99use TYPO3 \CMS \Extbase \Configuration \ConfigurationManagerInterface ;
10- use TYPO3 \CMS \Extbase \Object \ObjectManager ;
11-
12- use function array_merge ;
1310use function array_unique ;
14- use function count ;
1511use function curl_close ;
1612use function curl_errno ;
1713use function curl_error ;
1814use function curl_getinfo ;
15+ use function curl_init ;
1916use function curl_multi_add_handle ;
2017use function curl_multi_close ;
18+ use function curl_multi_exec ;
2119use function curl_multi_init ;
2220use function curl_multi_remove_handle ;
21+ use function curl_multi_select ;
2322use function curl_setopt ;
24- use function implode ;
2523use function is_array ;
24+ use function is_resource ;
25+ use const CURLM_CALL_MULTI_PERFORM ;
26+ use const CURLM_OK ;
27+ use const CURLOPT_CUSTOMREQUEST ;
28+ use const CURLOPT_HTTPHEADER ;
29+ use const CURLOPT_RETURNTRANSFER ;
30+ use const CURLOPT_SSL_VERIFYHOST ;
31+ use const CURLOPT_SSL_VERIFYPEER ;
32+ use const CURLOPT_URL ;
2633
2734final class CacheManager implements LoggerAwareInterface
2835{
2936 use LoggerAwareTrait;
3037
31- /**
32- * @var LoggerInterface
33- */
34- protected $ logger ;
35-
3638 /**
3739 * @var array<string, mixed>
3840 */
@@ -41,70 +43,29 @@ final class CacheManager implements LoggerAwareInterface
4143 * @var array<string>
4244 */
4345 protected array $ clearQueue = [];
44- /**
45- * @var array<string>
46- */
47- protected array $ clearQueueTags = [];
48- /**
49- * @var array<string>
50- */
51- protected array $ clearQueueSoftTags = [];
5246
5347 public function __construct ()
5448 {
55- $ objectManager = GeneralUtility::makeInstance (ObjectManager::class);
56- $ configurationManager = $ objectManager ->get (ConfigurationManagerInterface::class);
49+ $ configurationManager = GeneralUtility::makeInstance (ConfigurationManager::class);
5750 $ this ->settings = $ configurationManager ->getConfiguration (
58- ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
59- )['tx_cachepurger. ' ]['settings. ' ] ?? [];
60- }
61-
62- /**
63- * @param string $path
64- */
65- public function clearForUrl (string $ path ): void
66- {
67- $ this ->logger ->debug ('clearCacheForUrl: ' . $ path );
68-
69- $ this ->clearQueue [] = $ path ;
70- $ this ->clearQueue = array_unique ($ this ->clearQueue );
51+ ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
52+ )['tx_cachepurger. ' ]['settings. ' ] ?? [];
7153 }
7254
7355 /**
7456 * @param string $tag
7557 */
7658 public function clearForTag (string $ tag ): void
7759 {
78- $ this ->logger ->debug ('clearCacheForUrl: ' . $ tag );
79-
80- $ this ->clearQueueTags [] = $ tag ;
81- $ this ->clearQueueTags = array_unique ($ this ->clearQueueTags );
82- }
83-
84- /**
85- * @param string $tag
86- */
87- public function clearSoftForTag (string $ tag ): void
88- {
89- $ this ->logger ->debug ('clearCacheSoftForTag: ' . $ tag );
90-
91- $ this ->clearQueueSoftTags [] = $ tag ;
92- $ this ->clearQueueSoftTags = array_unique ($ this ->clearQueueSoftTags );
60+ $ this ->clearQueue [] = $ tag ;
61+ $ this ->clearQueue = array_unique ($ this ->clearQueue );
9362 }
9463
9564 public function execute (): void
9665 {
9766 $ curlHandles = [];
98- $ this ->logger ->debug ('execute: ' , $ this ->clearQueue );
9967
100- if (
101- !is_array ($ this ->settings ['domains. ' ]) ||
102- !is_array ($ this ->settings ['varnish. ' ]) ||
103- (
104- count ($ this ->clearQueue ) === 0 ||
105- count ($ this ->clearQueueSoftTags ) === 0
106- )
107- ) {
68+ if (!isset ($ this ->settings ['varnish. ' ]) || !is_array ($ this ->settings ['varnish. ' ])) {
10869 return ;
10970 }
11071
@@ -115,29 +76,18 @@ public function execute(): void
11576 }
11677
11778 foreach ($ this ->settings ['varnish. ' ] as $ varnishInstance ) {
118- foreach ($ this ->clearQueue as $ path ) {
119- $ ch = $ this ->getCurlHandleForCacheClearing ($ path , $ varnishInstance );
120- if (!is_resource ($ ch )) {
121- continue ;
122- }
123- $ curlHandles [] = $ ch ;
124- curl_multi_add_handle ($ multiHandle , $ ch );
125- }
126- foreach ($ this ->clearQueueTags as $ tag ) {
79+ foreach ($ this ->clearQueue as $ tag ) {
12780 $ ch = $ this ->getCurlHandleForCacheClearingAsTag ($ tag , $ varnishInstance );
12881 if (!is_resource ($ ch )) {
12982 continue ;
13083 }
13184 $ curlHandles [] = $ ch ;
13285 curl_multi_add_handle ($ multiHandle , $ ch );
13386 }
87+ }
13488
135- $ ch = $ this ->getCurlHandleForSoftCacheClearingAsTag ($ this ->clearQueueSoftTags , $ varnishInstance );
136- if (!is_resource ($ ch )) {
137- continue ;
138- }
139- $ curlHandles [] = $ ch ;
140- curl_multi_add_handle ($ multiHandle , $ ch );
89+ if (count ($ curlHandles ) === 0 ) {
90+ return ;
14191 }
14292
14393 // initialize all connections
@@ -170,7 +120,7 @@ public function execute(): void
170120 $ this ->logger ->error ('error: ' . curl_error ($ ch ));
171121 } else {
172122 $ info = curl_getinfo ($ ch );
173- $ this ->logger ->debug ('info: ' , $ info );
123+ $ this ->logger ->error ('info: ' , $ info );
174124 }
175125 curl_multi_remove_handle ($ multiHandle , $ ch );
176126 curl_close ($ ch );
@@ -179,8 +129,6 @@ public function execute(): void
179129 curl_multi_close ($ multiHandle );
180130
181131 $ this ->clearQueue = [];
182- $ this ->clearQueueTags = [];
183- $ this ->clearQueueSoftTags = [];
184132 }
185133
186134 public function __destruct ()
@@ -197,17 +145,9 @@ public function clearCache(?string $cmd): void
197145 case 'all ' :
198146 $ this ->logger ->debug ('clearCacheCmd() all ' );
199147
200- if (isset ($ this ->settings ['domain. ' ])) {
201- $ this ->clearForUrl ($ this ->settings ['domain. ' ]);
202- }
203- if (isset ($ this ->settings ['domains. ' ]) && is_array ($ this ->settings ['domains. ' ])) {
204- foreach ($ this ->settings ['domains. ' ] as $ basePath ) {
205- $ this ->clearForUrl ($ basePath );
206- }
207- }
208148 if (isset ($ this ->settings ['tags. ' ]) && is_array ($ this ->settings ['tags. ' ])) {
209149 foreach ($ this ->settings ['tags. ' ] as $ tag ) {
210- $ this ->clearSoftForTag ($ tag );
150+ $ this ->clearForTag ($ tag );
211151 }
212152 }
213153 break ;
@@ -218,60 +158,14 @@ public function clearCache(?string $cmd): void
218158 }
219159 }
220160
221- /**
222- * @param array<string> $paths
223- */
224- public function addPathsToQueue (array $ paths ): void
225- {
226- $ this ->clearQueue = array_merge ($ this ->clearQueue , $ paths );
227- $ this ->clearQueue = array_unique ($ this ->clearQueue );
228- }
229-
230- /**
231- * @param array<string> $tags
232- */
233- public function addTagsToQueue (array $ tags ): void
234- {
235- $ this ->clearQueueTags = array_merge ($ this ->clearQueueTags , $ tags );
236- $ this ->clearQueueTags = array_unique ($ this ->clearQueueTags );
237- }
238-
239- /**
240- * @return false|resource
241- */
242- protected function getCurlHandleForCacheClearing (string $ url , string $ varnishUrl )
243- {
244- $ this ->logger ->debug ('getCurlHandleForCacheClearing: ' . $ url );
245-
246- return $ this ->createCurlHandle ($ varnishUrl , 'X-Url: ( ' . $ url . ') ' );
247- }
248-
249161 /**
250162 * @param string $tag
251163 * @param string $varnishUrl
252164 * @return false|resource
253165 */
254166 protected function getCurlHandleForCacheClearingAsTag (string $ tag , string $ varnishUrl )
255167 {
256- $ this ->logger ->debug ('getCurlHandleForCacheClearing: ' . $ tag );
257-
258- return $ this ->createCurlHandle ($ varnishUrl , 'X-Tags: ' . $ tag , 'BAN ' );
259- }
260-
261- /**
262- * @param array<string> $tags
263- * @param string $varnishUrl
264- * @return false|resource
265- */
266- protected function getCurlHandleForSoftCacheClearingAsTag (array $ tags , string $ varnishUrl )
267- {
268- $ combinedTags = implode (' ' , $ tags );
269-
270- $ this ->logger ->debug ('getCurlHandleForCacheClearing: ' . $ combinedTags );
271-
272- $ header = 'X-Tags: ' . $ combinedTags ;
273-
274- return $ this ->createCurlHandle ($ varnishUrl , $ header , 'BAN ' );
168+ return $ this ->createCurlHandle ($ varnishUrl , 'X-Tags: ' . $ tag );
275169 }
276170
277171 /**
0 commit comments