9
9
use Psr \Cache \CacheItemPoolInterface ;
10
10
use Psr \Http \Message \RequestInterface ;
11
11
use Psr \Http \Message \ResponseInterface ;
12
+ use Symfony \Component \OptionsResolver \Options ;
12
13
use Symfony \Component \OptionsResolver \OptionsResolver ;
13
14
14
15
/**
@@ -61,7 +62,13 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamF
61
62
public static function clientCache (CacheItemPoolInterface $ pool , StreamFactory $ streamFactory , array $ config = [])
62
63
{
63
64
// Allow caching of private requests
64
- $ config ['excluded_directives ' ] = ['no-cache ' ];
65
+ if (isset ($ config ['respect_response_cache_directives ' ])) {
66
+ if (!in_array ('no-cache ' , $ config ['respect_response_cache_directives ' ], true )) {
67
+ $ config ['respect_response_cache_directives ' ][] = ['no-cache ' ];
68
+ }
69
+ } else {
70
+ $ config ['respect_response_cache_directives ' ] = ['no-cache ' ];
71
+ }
65
72
66
73
self ::__construct ($ pool , $ streamFactory , $ config );
67
74
}
@@ -200,7 +207,7 @@ protected function isCacheable(ResponseInterface $response)
200
207
return true ;
201
208
}
202
209
203
- foreach ($ this ->config ['excluded_directives ' ] as $ cacheDirective ) {
210
+ foreach ($ this ->config ['respect_response_cache_directives ' ] as $ cacheDirective ) {
204
211
if ($ this ->getCacheControlDirective ($ response , $ cacheDirective )) {
205
212
return false ;
206
213
}
@@ -258,7 +265,7 @@ private function createCacheKey(RequestInterface $request)
258
265
*/
259
266
private function getMaxAge (ResponseInterface $ response )
260
267
{
261
- if (!$ this ->config ['respect_cache_headers ' ]) {
268
+ if (!$ this ->config ['respect_cache_headers ' ] || ! in_array ( ' max-age ' , $ this -> config [ ' respect_response_cache_directives ' ], true ) ) {
262
269
return $ this ->config ['default_ttl ' ];
263
270
}
264
271
@@ -292,10 +299,11 @@ private function configureOptions(OptionsResolver $resolver)
292
299
$ resolver ->setDefaults ([
293
300
'cache_lifetime ' => 86400 * 30 , // 30 days
294
301
'default_ttl ' => 0 ,
302
+ //Deprecated as of v1.3, to be removed in v2.0. Use respect_response_cache_directives instead
295
303
'respect_cache_headers ' => true ,
296
304
'hash_algo ' => 'sha1 ' ,
297
305
'methods ' => ['GET ' , 'HEAD ' ],
298
- 'excluded_directives ' => ['no-cache ' , 'private ' ],
306
+ 'respect_response_cache_directives ' => ['no-cache ' , 'private ' , ' max-age ' ],
299
307
]);
300
308
301
309
$ resolver ->setAllowedTypes ('cache_lifetime ' , ['int ' , 'null ' ]);
@@ -309,6 +317,14 @@ private function configureOptions(OptionsResolver $resolver)
309
317
310
318
return empty ($ matches );
311
319
});
320
+
321
+ $ resolver ->setNormalizer ('respect_cache_headers ' , function (Options $ options , $ value ) {
322
+ if (null !== $ value ) {
323
+ @trigger_error ('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead. ' , E_USER_DEPRECATED );
324
+ }
325
+
326
+ return $ value ;
327
+ });
312
328
}
313
329
314
330
/**
0 commit comments