|
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\ProxyClient\Invalidation\ClearCapable; |
|
15
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable; |
|
16
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable; |
|
17
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable; |
|
18
|
|
|
use FOS\HttpCache\SymfonyCache\PurgeListener; |
|
19
|
|
|
use FOS\HttpCache\SymfonyCache\PurgeTagsListener; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Symfony HttpCache invalidator. |
|
23
|
|
|
* |
|
24
|
|
|
* Additional constructor options: |
|
25
|
|
|
* - purge_method: HTTP method that identifies purge requests. |
|
26
|
|
|
* |
|
27
|
|
|
* @author David de Boer <[email protected]> |
|
28
|
|
|
* @author David Buchmann <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Symfony extends HttpProxyClient implements PurgeCapable, RefreshCapable, TagCapable, ClearCapable |
|
31
|
|
|
{ |
|
32
|
|
|
public const HTTP_METHOD_REFRESH = 'GET'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function purge($url, array $headers = []) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->queueRequest($this->options['purge_method'], $url, $headers); |
|
40
|
|
|
|
|
41
|
4 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function refresh($url, array $headers = []) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
|
50
|
3 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
|
51
|
|
|
|
|
52
|
3 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
10 |
|
protected function configureOptions() |
|
56
|
|
|
{ |
|
57
|
10 |
|
$resolver = parent::configureOptions(); |
|
58
|
10 |
|
$resolver->setDefaults([ |
|
59
|
10 |
|
'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD, |
|
60
|
10 |
|
'clear_cache_header' => PurgeListener::DEFAULT_CLEAR_CACHE_HEADER, |
|
61
|
10 |
|
'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD, |
|
62
|
10 |
|
'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER, |
|
63
|
10 |
|
'tags_invalidate_path' => '/', |
|
64
|
10 |
|
'header_length' => 7500, |
|
65
|
|
|
]); |
|
66
|
10 |
|
$resolver->setAllowedTypes('purge_method', 'string'); |
|
67
|
10 |
|
$resolver->setAllowedTypes('clear_cache_header', 'string'); |
|
68
|
10 |
|
$resolver->setAllowedTypes('tags_method', 'string'); |
|
69
|
10 |
|
$resolver->setAllowedTypes('tags_header', 'string'); |
|
70
|
10 |
|
$resolver->setAllowedTypes('tags_invalidate_path', 'string'); |
|
71
|
10 |
|
$resolver->setAllowedTypes('header_length', 'int'); |
|
72
|
|
|
|
|
73
|
10 |
|
return $resolver; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function invalidateTags(array $tags) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$escapedTags = $this->escapeTags($tags); |
|
82
|
|
|
|
|
83
|
2 |
|
$chunkSize = $this->determineTagsPerHeader($escapedTags, ','); |
|
84
|
|
|
|
|
85
|
2 |
|
foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) { |
|
86
|
2 |
|
$this->queueRequest( |
|
87
|
2 |
|
$this->options['tags_method'], |
|
88
|
2 |
|
$this->options['tags_invalidate_path'], |
|
89
|
2 |
|
[$this->options['tags_header'] => implode(',', $tagchunk)], |
|
90
|
2 |
|
false |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
* |
|
100
|
|
|
* Clearing the cache is implemented with a purge request with a special |
|
101
|
|
|
* header to indicate that the whole cache should be removed. |
|
102
|
|
|
* |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function clear() |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->queueRequest( |
|
108
|
1 |
|
$this->options['purge_method'], |
|
109
|
1 |
|
'/', |
|
110
|
1 |
|
[$this->options['clear_cache_header'] => 'true'], |
|
111
|
1 |
|
false |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|