@@ -112,10 +112,10 @@ public function onKernelResponse(FilterResponseEvent $event)
112
112
$ directives = array_intersect_key ($ options ['cache_control ' ], $ this ->supportedDirectives );
113
113
$ extraDirectives = array_diff_key ($ options ['cache_control ' ], $ directives );
114
114
if (!empty ($ directives )) {
115
- $ this ->setCache ($ response , $ directives );
115
+ $ this ->setCache ($ response , $ directives, $ options [ ' overwrite ' ] );
116
116
}
117
117
if (!empty ($ extraDirectives )) {
118
- $ this ->setExtraCacheDirectives ($ response , $ extraDirectives );
118
+ $ this ->setExtraCacheDirectives ($ response , $ extraDirectives, $ options [ ' overwrite ' ] );
119
119
}
120
120
}
121
121
@@ -127,17 +127,32 @@ public function onKernelResponse(FilterResponseEvent $event)
127
127
}
128
128
129
129
if (!empty ($ options ['vary ' ])) {
130
- $ response ->setVary (array_merge ( $ response -> getVary () , $ options ['vary ' ]), true ); //update if already has vary
130
+ $ response ->setVary ($ options [ ' vary ' ] , $ options ['overwrite ' ]);
131
131
}
132
132
133
- if (isset ($ options ['last_modified ' ]) && null === $ response ->getLastModified ()) {
133
+ if (isset ($ options ['last_modified ' ])
134
+ && ($ options ['overwrite ' ] || null === $ response ->getLastModified ())
135
+ ) {
134
136
$ response ->setLastModified (new \DateTime ($ options ['last_modified ' ]));
135
137
}
136
138
}
137
139
}
138
140
139
- private function setCache (Response $ response , array $ directives )
141
+ /**
142
+ * Set cache headers on response.
143
+ *
144
+ * @param Response $response
145
+ * @param array $directives
146
+ * @param boolean $overwrite Whether to keep existing cache headers or to overwrite them.
147
+ */
148
+ private function setCache (Response $ response , array $ directives , $ overwrite )
140
149
{
150
+ if ($ overwrite ) {
151
+ $ response ->setCache ($ directives );
152
+
153
+ return ;
154
+ }
155
+
141
156
if ('no-cache ' === $ response ->headers ->get ('cache-control ' )) {
142
157
// this single header is set by default. if its the only thing, we override it.
143
158
$ response ->setCache ($ directives );
@@ -165,16 +180,17 @@ private function setCache(Response $response, array $directives)
165
180
*
166
181
* @param Response $response
167
182
* @param array $controls
183
+ * @param boolean $overwrite Whether to keep existing cache headers or to overwrite them.
168
184
*/
169
- private function setExtraCacheDirectives (Response $ response , array $ controls )
185
+ private function setExtraCacheDirectives (Response $ response , array $ controls, $ overwrite )
170
186
{
171
187
$ flags = array ('must_revalidate ' , 'proxy_revalidate ' , 'no_transform ' , 'no_cache ' );
172
188
$ options = array ('stale_if_error ' , 'stale_while_revalidate ' );
173
189
174
190
foreach ($ flags as $ key ) {
175
191
$ flag = str_replace ('_ ' , '- ' , $ key );
176
192
if (!empty ($ controls [$ key ])
177
- && !$ response ->headers ->hasCacheControlDirective ($ flag )
193
+ && ( $ overwrite || !$ response ->headers ->hasCacheControlDirective ($ flag) )
178
194
) {
179
195
$ response ->headers ->addCacheControlDirective ($ flag );
180
196
}
@@ -183,9 +199,10 @@ private function setExtraCacheDirectives(Response $response, array $controls)
183
199
foreach ($ options as $ key ) {
184
200
$ option = str_replace ('_ ' , '- ' , $ key );
185
201
if (isset ($ controls [$ key ])
186
- && !$ response ->headers ->hasCacheControlDirective ($ option )
202
+ && ( $ overwrite || !$ response ->headers ->hasCacheControlDirective ($ option) )
187
203
) {
188
204
$ response ->headers ->addCacheControlDirective ($ option , $ controls [$ key ]);
205
+
189
206
}
190
207
}
191
208
}
0 commit comments