@@ -87,6 +87,9 @@ public function __construct(array $options = [])
87
87
$ resolver ->setDefault ('cache_tags_header ' , 'Cache-Tags ' )
88
88
->setAllowedTypes ('cache_tags_header ' , 'string ' );
89
89
90
+ $ resolver ->setDefault ('generate_content_digests ' , true )
91
+ ->setAllowedTypes ('generate_content_digests ' , 'boolean ' );
92
+
90
93
$ resolver ->setDefault ('cache ' , function (Options $ options ) {
91
94
if (!isset ($ options ['cache_directory ' ])) {
92
95
throw new MissingOptionsException ('The cache_directory option is required unless you set the cache explicitly ' );
@@ -198,12 +201,18 @@ public function write(Request $request, Response $response)
198
201
}
199
202
200
203
// Add or replace entry with current Vary header key
201
- $ entries [$ this ->getVaryKey ($ response ->getVary (), $ request )] = [
204
+ $ varyKey = $ this ->getVaryKey ($ response ->getVary (), $ request );
205
+ $ entries [$ varyKey ] = [
202
206
'vary ' => $ response ->getVary (),
203
207
'headers ' => $ headers ,
204
208
'status ' => $ response ->getStatusCode (),
205
209
];
206
210
211
+ // Add content if content digests are disabled
212
+ if (!$ this ->options ['generate_content_digests ' ]) {
213
+ $ entries [$ varyKey ]['content ' ] = $ response ->getContent ();
214
+ }
215
+
207
216
// If the response has a Vary header we remove the non-varying entry
208
217
if ($ response ->hasVary ()) {
209
218
unset($ entries [self ::NON_VARYING_KEY ]);
@@ -412,7 +421,7 @@ public function getCacheKey(Request $request)
412
421
}
413
422
414
423
/**
415
- * @return string
424
+ * @return string|null
416
425
*
417
426
* @internal Do not use in public code, this is for unit testing purposes only
418
427
*/
@@ -422,6 +431,10 @@ public function generateContentDigest(Response $response)
422
431
return 'bf ' .hash_file ('sha256 ' , $ response ->getFile ()->getPathname ());
423
432
}
424
433
434
+ if (!$ this ->options ['generate_content_digests ' ]) {
435
+ return null ;
436
+ }
437
+
425
438
return 'en ' .hash ('sha256 ' , $ response ->getContent ());
426
439
}
427
440
@@ -465,6 +478,11 @@ private function saveContentDigest(Response $response)
465
478
}
466
479
467
480
$ contentDigest = $ this ->generateContentDigest ($ response );
481
+
482
+ if (null === $ contentDigest ) {
483
+ return ;
484
+ }
485
+
468
486
$ digestCacheItem = $ this ->cache ->getItem ($ contentDigest );
469
487
470
488
if ($ digestCacheItem ->isHit ()) {
@@ -575,7 +593,18 @@ private function restoreResponse(array $cacheData)
575
593
// Unset internal debug info
576
594
unset($ cacheData ['headers ' ][self ::CACHE_DEBUG_HEADER ]);
577
595
596
+ // Check for content digest header
578
597
if (!isset ($ cacheData ['headers ' ]['x-content-digest ' ][0 ])) {
598
+ // No digest was generated but the content was stored inline
599
+ if (isset ($ cacheData ['content ' ])) {
600
+ return new Response (
601
+ $ cacheData ['content ' ],
602
+ $ cacheData ['status ' ],
603
+ $ cacheData ['headers ' ]
604
+ );
605
+ }
606
+
607
+ // No content digest and no inline content means we cannot restore the response
579
608
return null ;
580
609
}
581
610
0 commit comments