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\Exception\InvalidTagException; |
15
|
|
|
use FOS\HttpCache\TagHeaderFormatter\CommaSeparatedTagHeaderFormatter; |
16
|
|
|
use FOS\HttpCache\TagHeaderFormatter\TagHeaderFormatter; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\Options; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Service for Response cache tagging. |
23
|
|
|
* |
24
|
|
|
* Record tags with this class and then either get the tags header or have the |
25
|
|
|
* tagger add the tags to a PSR-7 response. |
26
|
|
|
* Recorded tags are cleared after tagging a response. |
27
|
|
|
* |
28
|
|
|
* @author David de Boer <[email protected]> |
29
|
|
|
* @author David Buchmann <[email protected]> |
30
|
|
|
* @author André Rømcke <[email protected]> |
31
|
|
|
* @author Wicliff Wolda <[email protected]> |
32
|
|
|
* @author Yanick Witschi <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class ResponseTagger |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $options; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TagHeaderFormatter |
43
|
|
|
*/ |
44
|
|
|
private $headerFormatter; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $tags = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create the response tagger with a tag header formatter and options. |
53
|
|
|
* |
54
|
|
|
* Supported options are: |
55
|
|
|
* |
56
|
|
|
* - header_formatter (TagHeaderFormatter) Default: CommaSeparatedTagHeaderFormatter with default header name |
57
|
|
|
* - strict (bool) Default: false. If set to true, throws exception when adding empty tags |
58
|
|
|
*/ |
59
|
9 |
|
public function __construct(array $options = []) |
60
|
|
|
{ |
61
|
9 |
|
$resolver = new OptionsResolver(); |
62
|
9 |
|
$resolver->setDefaults([ |
63
|
|
|
// callback to avoid instantiating the formatter when its not needed |
64
|
|
|
'header_formatter' => function (Options $options) { |
|
|
|
|
65
|
2 |
|
return new CommaSeparatedTagHeaderFormatter(); |
66
|
9 |
|
}, |
67
|
|
|
'strict' => false, |
68
|
|
|
]); |
69
|
|
|
|
70
|
9 |
|
$resolver->setAllowedTypes('header_formatter', TagHeaderFormatter::class); |
71
|
9 |
|
$resolver->setAllowedTypes('strict', 'bool'); |
72
|
|
|
|
73
|
9 |
|
$this->options = $resolver->resolve($options); |
74
|
9 |
|
$this->headerFormatter = $this->options['header_formatter']; |
75
|
9 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the HTTP header name that will hold cache tags. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
3 |
|
public function getTagsHeaderName() |
83
|
|
|
{ |
84
|
3 |
|
return $this->headerFormatter->getTagsHeaderName(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the value for the HTTP tag header. |
89
|
|
|
* |
90
|
|
|
* This concatenates all tags and ensures correct encoding. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
5 |
|
public function getTagsHeaderValue() |
95
|
|
|
{ |
96
|
5 |
|
return $this->headerFormatter->getTagsHeaderValue($this->tags); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check whether the tag handler has any tags to set on the response. |
101
|
|
|
* |
102
|
|
|
* @return bool True if this handler will set at least one tag |
103
|
|
|
*/ |
104
|
7 |
|
public function hasTags() |
105
|
|
|
{ |
106
|
7 |
|
return 0 < count($this->tags); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add tags to be set on the response. |
111
|
|
|
* |
112
|
|
|
* This must be called before any HTTP response is sent to the client. |
113
|
|
|
* |
114
|
|
|
* @param string[] $tags List of tags to add |
115
|
|
|
* |
116
|
|
|
* @throws InvalidTagException |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
7 |
|
public function addTags(array $tags) |
121
|
|
|
{ |
122
|
7 |
|
$filtered = array_filter($tags, 'is_string'); |
123
|
7 |
|
$filtered = array_filter($filtered, 'strlen'); |
124
|
|
|
|
125
|
7 |
|
if ($this->options['strict'] && array_diff($tags, $filtered)) { |
126
|
1 |
|
throw new InvalidTagException('Empty tags are not allowed'); |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
$this->tags = array_unique(array_merge($this->tags, $filtered)); |
130
|
|
|
|
131
|
6 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Remove all tags that have been recorded. |
136
|
|
|
* |
137
|
|
|
* This is usually called after adding the tags header to a response. It is |
138
|
|
|
* automatically called by the tagResponse method. |
139
|
|
|
*/ |
140
|
3 |
|
public function clear() |
141
|
|
|
{ |
142
|
3 |
|
$this->tags = []; |
143
|
3 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set tags on a response and then clear the tags. |
147
|
|
|
* |
148
|
|
|
* @param ResponseInterface $response Original response |
149
|
|
|
* @param bool $replace Whether to replace the current tags |
150
|
|
|
* on the response |
151
|
|
|
* |
152
|
|
|
* @return ResponseInterface Tagged response |
153
|
|
|
*/ |
154
|
3 |
|
public function tagResponse(ResponseInterface $response, $replace = false) |
155
|
|
|
{ |
156
|
3 |
|
if (!$this->hasTags()) { |
157
|
1 |
|
return $response; |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
$tagsHeaderValue = $this->getTagsHeaderValue(); |
161
|
2 |
|
$this->clear(); |
162
|
|
|
|
163
|
2 |
|
if ($replace) { |
164
|
1 |
|
return $response->withHeader($this->getTagsHeaderName(), $tagsHeaderValue); |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $response->withAddedHeader($this->getTagsHeaderName(), $tagsHeaderValue); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.