7
7
use FOS \HttpCache \Exception \ExceptionCollection ;
8
8
use FOS \HttpCache \Exception \ProxyResponseException ;
9
9
use FOS \HttpCache \Exception \ProxyUnreachableException ;
10
- use FOS \HttpCache \Handler \TagHandler ;
11
10
use FOS \HttpCache \ProxyClient ;
11
+ use MOC \Varnish \Service \ProxyClient \Varnish ;
12
12
use Neos \Flow \Annotations as Flow ;
13
13
14
14
/**
@@ -35,7 +35,7 @@ class VarnishBanService
35
35
protected $ settings ;
36
36
37
37
/**
38
- * @var ProxyClient\ Varnish
38
+ * @var Varnish
39
39
*/
40
40
protected $ varnishProxyClient ;
41
41
@@ -45,10 +45,9 @@ class VarnishBanService
45
45
protected $ cacheInvalidator ;
46
46
47
47
/**
48
- * @var TagHandler
48
+ * @param array $settings
49
+ * @return void
49
50
*/
50
- protected $ tagHandler ;
51
-
52
51
public function injectSettings (array $ settings ): void
53
52
{
54
53
$ this ->settings = $ settings ;
@@ -61,10 +60,15 @@ public function initializeObject(): void
61
60
array_walk ($ varnishUrls , function (&$ varnishUrl ) {
62
61
$ varnishUrl = rtrim ($ varnishUrl , '/ ' );
63
62
});
64
- $ this ->varnishProxyClient = new ProxyClient \Varnish ($ varnishUrls );
65
- $ this ->varnishProxyClient ->setDefaultBanHeader ('X-Site ' , $ this ->tokenStorage ->getToken ());
63
+ $ httpDispatcher = new ProxyClient \HttpDispatcher ($ varnishUrls );
64
+ $ options = [
65
+ 'header_length ' => $ this ->settings ['maximumHeaderLength ' ] ?? 7500 ,
66
+ 'default_ban_headers ' => [
67
+ 'X-Site ' => $ this ->tokenStorage ->getToken ()
68
+ ]
69
+ ];
70
+ $ this ->varnishProxyClient = new Varnish ($ httpDispatcher , $ options );
66
71
$ this ->cacheInvalidator = new CacheInvalidator ($ this ->varnishProxyClient );
67
- $ this ->tagHandler = new TagHandler ($ this ->cacheInvalidator , 'X-Cache-Tags ' , $ this ->settings ['maximumHeaderLength ' ] ?? 7500 );
68
72
}
69
73
70
74
/**
@@ -81,6 +85,7 @@ public function initializeObject(): void
81
85
*/
82
86
public function banAll ($ domains = null , $ contentType = null ): void
83
87
{
88
+ $ this ->varnishProxyClient ->forHosts (...$ this ->domainsToArray ($ domains ));
84
89
$ this ->cacheInvalidator ->invalidateRegex ('.* ' , $ contentType , $ domains );
85
90
$ this ->logger ->debug (sprintf ('Clearing all Varnish cache%s%s ' , $ domains ? ' for domains " ' . (is_array ($ domains ) ? implode (', ' , $ domains ) : $ domains ) . '" ' : '' , $ contentType ? ' with content type " ' . $ contentType . '" ' : '' ));
86
91
$ this ->execute ();
@@ -112,16 +117,9 @@ public function banByTags(array $tags, $domains = null): void
112
117
$ tags [$ key ] = strtr ($ tag , '.: ' , '_- ' );
113
118
}
114
119
115
- // Set specific domain before invalidating tags
116
- if ($ domains ) {
117
- $ this ->varnishProxyClient ->setDefaultBanHeader (ProxyClient \Varnish::HTTP_HEADER_HOST , is_array ($ domains ) ? '^( ' . implode ('| ' , $ domains ) . ')$ ' : $ domains );
118
- }
119
- $ this ->tagHandler ->invalidateTags ($ tags );
120
- // Unset specific domain after invalidating tags
121
- if ($ domains ) {
122
- $ this ->varnishProxyClient ->setDefaultBanHeader (ProxyClient \Varnish::HTTP_HEADER_HOST , ProxyClient \Varnish::REGEX_MATCH_ALL );
123
- }
124
- $ this ->logger ->debug (sprintf ('Clearing Varnish cache for tags "%s"%s ' , implode (', ' , $ tags ), $ domains ? ' for domains " ' . (is_array ($ domains ) ? implode (', ' , $ domains ) : $ domains ) . '" ' : '' ));
120
+ $ this ->varnishProxyClient ->forHosts (...$ this ->domainsToArray ($ domains ));
121
+ $ this ->cacheInvalidator ->invalidateTags ($ tags );
122
+ $ this ->logger ->debug (sprintf ('Cleared Varnish cache for tags "%s"%s ' , implode (', ' , $ tags ), $ domains ? ' for domains " ' . (is_array ($ domains ) ? implode (', ' , $ domains ) : $ domains ) . '" ' : '' ));
125
123
$ this ->execute ();
126
124
}
127
125
@@ -141,4 +139,13 @@ protected function execute(): void
141
139
}
142
140
}
143
141
}
142
+
143
+ /**
144
+ * @param string|string[]|null $domains
145
+ * @return array
146
+ */
147
+ private function domainsToArray ($ domains = null ): array
148
+ {
149
+ return is_array ($ domains ) ? $ domains : (is_string ($ domains ) ? [$ domains ] : []);
150
+ }
144
151
}
0 commit comments