Repository navigation
Expand file tree
/
Copy pathMultiplexerClient.php
More file actions
179 lines (154 loc) · 4.51 KB
/
Copy pathMultiplexerClient.php
File metadata and controls
179 lines (154 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache\ProxyClient;
use FOS\HttpCache\Exception\InvalidArgumentException;
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable;
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable;
use FOS\HttpCache\ProxyClient\Invalidation\PrefixCapable;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
/**
* This class forwards invalidation to all attached clients.
*
* @author Emanuele Panzeri <thepanz@gmail.com>
*/
class MultiplexerClient implements BanCapable, PrefixCapable, PurgeCapable, RefreshCapable, TagCapable, ClearCapable
{
/**
* @var ProxyClient[]
*/
private array $proxyClients;
/**
* MultiplexerClient constructor.
*
* @param ProxyClient[] $proxyClients The list of Proxy clients
*/
public function __construct(array $proxyClients)
{
foreach ($proxyClients as $proxyClient) {
if (!$proxyClient instanceof ProxyClient) {
throw new InvalidArgumentException(
'Expected ProxyClientInterface, got: '.get_debug_type($proxyClient)
);
}
}
$this->proxyClients = $proxyClients;
}
public function ban(array $headers): static
{
$this->invoke(BanCapable::class, 'ban', [$headers]);
return $this;
}
public function banPath(string $path, ?string $contentType = null, array|string|null $hosts = null): static
{
$this->invoke(BanCapable::class, 'banPath', [$path, $contentType, $hosts]);
return $this;
}
/**
* {@inheritDoc}
*
* Forwards to all clients.
*/
public function flush(): int
{
$count = 0;
foreach ($this->proxyClients as $proxyClient) {
$count += $proxyClient->flush();
}
return $count;
}
/**
* Forwards tag invalidation request to all clients.
*
* {@inheritdoc}
*/
public function invalidateTags(array $tags): static
{
if (!$tags) {
return $this;
}
$this->invoke(TagCapable::class, 'invalidateTags', [$tags]);
return $this;
}
/**
* Forwards prefix invalidation request to all clients.
*
* {@inheritdoc}
*/
public function invalidatePrefixes(array $prefixes): static
{
if (!$prefixes) {
return $this;
}
$this->invoke(PrefixCapable::class, 'invalidatePrefixes', [$prefixes]);
return $this;
}
/**
* Forwards to all clients.
*
* {@inheritdoc}
*/
public function purge(string $url, array $headers = []): static
{
$this->invoke(PurgeCapable::class, 'purge', [$url, $headers]);
return $this;
}
/**
* Forwards to all clients.
*
* {@inheritdoc}
*/
public function refresh(string $url, array $headers = []): static
{
$this->invoke(RefreshCapable::class, 'refresh', [$url, $headers]);
return $this;
}
/**
* Forwards to all clients.
*
* {@inheritdoc}
*/
public function clear(): static
{
$this->invoke(ClearCapable::class, 'clear', []);
return $this;
}
/**
* Invoke the given $method on all available ProxyClients implementing the
* given $interface.
*
* @param class-string $interface The FQN of the interface
* @param string $method The method to invoke
* @param array<mixed> $arguments The arguments to be passed to the method
*/
private function invoke(string $interface, string $method, array $arguments): void
{
foreach ($this->getProxyClients($interface) as $proxyClient) {
call_user_func_array([$proxyClient, $method], $arguments);
}
}
/**
* Get proxy clients that implement a feature interface.
*
* @param class-string $interface
*
* @return ProxyClient[]
*/
private function getProxyClients(string $interface): array
{
return array_filter(
$this->proxyClients,
static function (ProxyClient $proxyClient) use ($interface) {
return is_subclass_of($proxyClient, $interface);
}
);
}
}