1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\ProxyClient; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\Exception\ExceptionCollection; |
15
|
|
|
use FOS\HttpCache\Exception\InvalidArgumentException; |
16
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\BanInterface; |
17
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface; |
18
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class forwards invalidation to all attached clients. |
22
|
|
|
* |
23
|
|
|
* @author Emanuele Panzeri <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class MultiplexerClient implements BanInterface, PurgeInterface, RefreshInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ProxyClientInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $proxyClients; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* MultiplexerClient constructor. |
34
|
|
|
* |
35
|
|
|
* @param ProxyClientInterface[] $proxyClients The list of Proxy clients |
36
|
|
|
*/ |
37
|
6 |
|
public function __construct(array $proxyClients) |
38
|
|
|
{ |
39
|
6 |
|
foreach ($proxyClients as $proxyClient) { |
40
|
6 |
|
if (!$proxyClient instanceof ProxyClientInterface) { |
41
|
2 |
|
throw new InvalidArgumentException('Expected ProxyClientInterface, got: '. |
42
|
2 |
|
(is_object($proxyClient) ? get_class($proxyClient) : gettype($proxyClient)) |
43
|
2 |
|
); |
44
|
|
|
} |
45
|
4 |
|
} |
46
|
|
|
|
47
|
4 |
|
$this->proxyClients = $proxyClients; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Forwards to all clients. |
52
|
|
|
* |
53
|
|
|
* @param array $headers HTTP headers that path must match to be banned |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function ban(array $headers) |
58
|
|
|
{ |
59
|
|
|
$this->invoke(BanInterface::class, 'ban', [$headers]); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Forwards to all clients. |
66
|
|
|
* |
67
|
|
|
* @param string $path Regular expression pattern for URI to invalidate |
68
|
|
|
* @param string $contentType Regular expression pattern for the content type to limit banning, for instance |
69
|
|
|
* 'text' |
70
|
|
|
* @param array|string $hosts Regular expression of a host name or list of exact host names to limit banning |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function banPath($path, $contentType = null, $hosts = null) |
75
|
|
|
{ |
76
|
|
|
$this->invoke(BanInterface::class, 'banPath', [$path, $contentType, $hosts]); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Forwards to all clients. |
83
|
|
|
* |
84
|
|
|
* @throws ExceptionCollection If any errors occurred during flush |
85
|
|
|
* |
86
|
|
|
* @return int The number of cache invalidations performed per caching server |
87
|
|
|
*/ |
88
|
|
|
public function flush() |
89
|
|
|
{ |
90
|
|
|
$count = 0; |
91
|
|
|
foreach ($this->proxyClients as $proxyClient) { |
92
|
|
|
$count += $proxyClient->flush(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $count; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Forwards to all clients. |
100
|
|
|
* |
101
|
|
|
* @param string $url Path or URL to purge |
102
|
|
|
* @param array $headers Extra HTTP headers to send to the caching proxy (optional) |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function purge($url, array $headers = array()) |
107
|
|
|
{ |
108
|
|
|
$this->invoke(PurgeInterface::class, 'purge', [$url, $headers]); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Forwards to all clients. |
115
|
|
|
* |
116
|
|
|
* @param string $url Path or URL to refresh |
117
|
|
|
* @param array $headers Extra HTTP headers to send to the caching proxy (optional) |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function refresh($url, array $headers = []) |
122
|
|
|
{ |
123
|
|
|
$this->invoke(RefreshInterface::class, 'refresh', [$url, $headers]); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Helper function to invoke the given $method on all available ProxyClients implementing the given $interface. |
130
|
|
|
* |
131
|
|
|
* @param string $interface The FQN of the interface |
132
|
|
|
* @param string $method The method to invoke |
133
|
|
|
* @param array $arguments The arguments to be passed to the method |
134
|
|
|
*/ |
135
|
|
|
private function invoke($interface, $method, array $arguments) |
136
|
|
|
{ |
137
|
|
|
foreach ($this->proxyClients as $proxyClient) { |
138
|
|
|
if (is_subclass_of($proxyClient, $interface)) { |
139
|
|
|
call_user_func_array([$proxyClient, $method], $arguments); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|