@@ -87,6 +87,9 @@ public function __construct(array $options = [])
8787 $ resolver ->setDefault ('cache_tags_header ' , 'Cache-Tags ' )
8888 ->setAllowedTypes ('cache_tags_header ' , 'string ' );
8989
90+ $ resolver ->setDefault ('generate_content_digests ' , true )
91+ ->setAllowedTypes ('generate_content_digests ' , 'boolean ' );
92+
9093 $ resolver ->setDefault ('cache ' , function (Options $ options ) {
9194 if (!isset ($ options ['cache_directory ' ])) {
9295 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)
198201 }
199202
200203 // 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 ] = [
202206 'vary ' => $ response ->getVary (),
203207 'headers ' => $ headers ,
204208 'status ' => $ response ->getStatusCode (),
205209 ];
206210
211+ // Add content if content digests are disabled
212+ if (!$ this ->options ['generate_content_digests ' ]) {
213+ $ entries [$ varyKey ]['content ' ] = $ response ->getContent ();
214+ }
215+
207216 // If the response has a Vary header we remove the non-varying entry
208217 if ($ response ->hasVary ()) {
209218 unset($ entries [self ::NON_VARYING_KEY ]);
@@ -412,7 +421,7 @@ public function getCacheKey(Request $request)
412421 }
413422
414423 /**
415- * @return string
424+ * @return string|null
416425 *
417426 * @internal Do not use in public code, this is for unit testing purposes only
418427 */
@@ -422,6 +431,10 @@ public function generateContentDigest(Response $response)
422431 return 'bf ' .hash_file ('sha256 ' , $ response ->getFile ()->getPathname ());
423432 }
424433
434+ if (!$ this ->options ['generate_content_digests ' ]) {
435+ return null ;
436+ }
437+
425438 return 'en ' .hash ('sha256 ' , $ response ->getContent ());
426439 }
427440
@@ -465,6 +478,11 @@ private function saveContentDigest(Response $response)
465478 }
466479
467480 $ contentDigest = $ this ->generateContentDigest ($ response );
481+
482+ if (null === $ contentDigest ) {
483+ return ;
484+ }
485+
468486 $ digestCacheItem = $ this ->cache ->getItem ($ contentDigest );
469487
470488 if ($ digestCacheItem ->isHit ()) {
@@ -575,7 +593,18 @@ private function restoreResponse(array $cacheData)
575593 // Unset internal debug info
576594 unset($ cacheData ['headers ' ][self ::CACHE_DEBUG_HEADER ]);
577595
596+ // Check for content digest header
578597 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
579608 return null ;
580609 }
581610
0 commit comments