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
|
|
|
const HTTP_METHOD_REFRESH = 'GET'; |
33
|
|
|
|
34
|
4 |
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
4 |
|
*/ |
37
|
|
|
public function purge($url, array $headers = []) |
38
|
4 |
|
{ |
39
|
|
|
$this->queueRequest($this->options['purge_method'], $url, $headers); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
3 |
|
*/ |
47
|
3 |
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
48
|
|
|
{ |
49
|
3 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
50
|
|
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
51
|
|
|
|
52
|
7 |
|
return $this; |
53
|
|
|
} |
54
|
7 |
|
|
55
|
7 |
|
protected function configureOptions() |
56
|
7 |
|
{ |
57
|
|
|
$resolver = parent::configureOptions(); |
58
|
|
|
$resolver->setDefaults([ |
59
|
7 |
|
'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD, |
60
|
|
|
'clear_cache_header' => PurgeListener::DEFAULT_CLEAR_CACHE_HEADER, |
61
|
|
|
'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD, |
62
|
|
|
'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER, |
63
|
|
|
'tags_invalidate_path' => '/', |
64
|
|
|
'header_length' => 7500, |
65
|
|
|
]); |
66
|
|
|
$resolver->setAllowedTypes('purge_method', 'string'); |
67
|
|
|
$resolver->setAllowedTypes('clear_cache_header', 'string'); |
68
|
|
|
$resolver->setAllowedTypes('tags_method', 'string'); |
69
|
|
|
$resolver->setAllowedTypes('tags_header', 'string'); |
70
|
|
|
$resolver->setAllowedTypes('tags_invalidate_path', 'string'); |
71
|
|
|
$resolver->setAllowedTypes('header_length', 'int'); |
72
|
|
|
|
73
|
|
|
return $resolver; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function invalidateTags(array $tags) |
80
|
|
|
{ |
81
|
|
|
$escapedTags = $this->escapeTags($tags); |
82
|
|
|
|
83
|
|
|
$chunkSize = $this->determineTagsPerHeader($escapedTags, ','); |
84
|
|
|
|
85
|
|
|
foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) { |
86
|
|
|
$this->queueRequest( |
87
|
|
|
$this->options['tags_method'], |
88
|
|
|
$this->options['tags_invalidate_path'], |
89
|
|
|
[$this->options['tags_header'] => implode(',', $tagchunk)], |
90
|
|
|
false |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
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
|
|
|
public function clear() |
106
|
|
|
{ |
107
|
|
|
$this->queueRequest( |
108
|
|
|
$this->options['purge_method'], '/', |
109
|
|
|
[$this->options['clear_cache_header'] => 'true'], |
110
|
|
|
false |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.