1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichanFongdasen\Varnishable\Concerns; |
4
|
|
|
|
5
|
|
|
trait InvalidateVarnishCache |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Flush entire cache for an application hostname. |
9
|
|
|
* |
10
|
|
|
* @param string $hostname [description] |
11
|
|
|
* @return void |
12
|
|
|
*/ |
13
|
|
|
public function flush($hostname) |
14
|
|
|
{ |
15
|
|
|
$this->sendBanRequest([ |
16
|
|
|
'X-Ban-Host' => $hostname |
17
|
|
|
], 'FULLBAN'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generate ban request for an application hostname, |
22
|
|
|
* specified by a set of regular expression. |
23
|
|
|
* |
24
|
|
|
* @param string $hostname |
25
|
|
|
* @param string|array $patterns |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function banByPatterns($hostname, $patterns) |
29
|
|
|
{ |
30
|
|
|
if (!is_array($patterns)) { |
31
|
|
|
return $this->sendBanRequest([ |
32
|
|
|
'X-Ban-Host' => $hostname, |
33
|
|
|
'X-Ban-Regex' => $patterns |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
foreach ($patterns as $pattern) { |
38
|
|
|
$this->banByPatterns($hostname, $pattern); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Generate ban request for an application hostname, |
44
|
|
|
* specified by a set of urls. |
45
|
|
|
* |
46
|
|
|
* @param string $hostname |
47
|
|
|
* @param string|array $urls |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function banByUrls($hostname, $urls) |
51
|
|
|
{ |
52
|
|
|
if (!is_array($urls)) { |
53
|
|
|
return $this->sendBanRequest([ |
54
|
|
|
'X-Ban-Host' => $hostname, |
55
|
|
|
'X-Ban-Url' => $urls |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($urls as $url) { |
60
|
|
|
$this->banByUrls($hostname, $url); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a valid varnish host url to send the |
66
|
|
|
* ban request. |
67
|
|
|
* |
68
|
|
|
* @param string $varnishHost |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function getVarnishUrl($varnishHost) |
72
|
|
|
{ |
73
|
|
|
return 'http://' . $varnishHost . ':' . $this->getConfig('varnish_port') . '/'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Send the ban request to every varnish hosts. |
78
|
|
|
* |
79
|
|
|
* @param array $headers |
80
|
|
|
* @param string $method |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function sendBanRequest(array $headers, $method = 'BAN') |
84
|
|
|
{ |
85
|
|
|
$guzzle = $this->getGuzzle(); |
86
|
|
|
|
87
|
|
|
foreach ((array) $this->getConfig('varnish_hosts') as $varnishHost) { |
88
|
|
|
$url = $this->getVarnishUrl($varnishHost); |
89
|
|
|
|
90
|
|
|
$guzzle->request($method, $url, ['headers' => $headers]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get configuration value for a specific key. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
abstract public function getConfig($key); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get guzzle client object. |
104
|
|
|
* |
105
|
|
|
* @return \GuzzleHttp\Client |
106
|
|
|
*/ |
107
|
|
|
abstract public function getGuzzle(); |
108
|
|
|
} |
109
|
|
|
|