|
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; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Service for Response cache tagging. |
|
19
|
|
|
* |
|
20
|
|
|
* @author David de Boer <[email protected]> |
|
21
|
|
|
* @author David Buchmann <[email protected]> |
|
22
|
|
|
* @author André Rømcke <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ResponseTagger |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TagsInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $tags = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor |
|
38
|
|
|
* |
|
39
|
|
|
* @param TagsInterface $client |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function __construct(TagsInterface $client) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$this->client = $client; |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the HTTP header name that will hold cache tags. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getTagsHeaderName() |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->client->getTagsHeaderName(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the value for the HTTP tag header. |
|
58
|
|
|
* |
|
59
|
|
|
* This concatenates all tags and ensures correct encoding. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function getTagsHeaderValue() |
|
64
|
|
|
{ |
|
65
|
2 |
|
return $this->client->getTagsHeaderValue($this->tags); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check whether the tag handler has any tags to set on the response. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool True if this handler will set at least one tag. |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function hasTags() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return 0 < count($this->tags); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add tags to be set on the response. |
|
80
|
|
|
* |
|
81
|
|
|
* This must be called before any HTTP response is sent to the client. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $tags List of tags to add. |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function addTags(array $tags) |
|
88
|
|
|
{ |
|
89
|
2 |
|
$this->tags = array_merge($this->tags, $tags); |
|
90
|
|
|
|
|
91
|
2 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set tags on a response |
|
96
|
|
|
* |
|
97
|
|
|
* @param ResponseInterface $response Original response |
|
98
|
|
|
* @param bool $replace Whether to replace the current tags |
|
99
|
|
|
* on the response |
|
100
|
|
|
* |
|
101
|
|
|
* @return ResponseInterface Tagged response |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function tagResponse(ResponseInterface $response, $replace = false) |
|
104
|
|
|
{ |
|
105
|
1 |
|
if ($replace) { |
|
106
|
1 |
|
return $response->withHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $response->withAddedHeader($this->getTagsHeaderName(), $this->getTagsHeaderValue()); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|