@@ -85,6 +85,24 @@ class Response
8585 const HTTP_NOT_EXTENDED = 510 ; // RFC2774
8686 const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511 ; // RFC6585
8787
88+ /**
89+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
90+ */
91+ private const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
92+ 'must_revalidate ' => false ,
93+ 'no_cache ' => false ,
94+ 'no_store ' => false ,
95+ 'no_transform ' => false ,
96+ 'public ' => false ,
97+ 'private ' => false ,
98+ 'proxy_revalidate ' => false ,
99+ 'max_age ' => true ,
100+ 's_maxage ' => true ,
101+ 'immutable ' => false ,
102+ 'last_modified ' => true ,
103+ 'etag ' => true ,
104+ ];
105+
88106 /**
89107 * @var ResponseHeaderBag
90108 */
@@ -921,7 +939,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
921939 /**
922940 * Sets the response's cache headers (validation and/or expiration).
923941 *
924- * Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable .
942+ * Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag .
925943 *
926944 * @return $this
927945 *
@@ -931,7 +949,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
931949 */
932950 public function setCache (array $ options ): object
933951 {
934- if ($ diff = array_diff (array_keys ($ options ), [ ' etag ' , ' last_modified ' , ' max_age ' , ' s_maxage ' , ' private ' , ' public ' , ' immutable ' ] )) {
952+ if ($ diff = array_diff (array_keys ($ options ), array_keys ( static :: HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES ) )) {
935953 throw new \InvalidArgumentException (sprintf ('Response does not support the following options: "%s". ' , implode ('", " ' , $ diff )));
936954 }
937955
@@ -951,6 +969,16 @@ public function setCache(array $options): object
951969 $ this ->setSharedMaxAge ($ options ['s_maxage ' ]);
952970 }
953971
972+ foreach (self ::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $ directive => $ hasValue ) {
973+ if (!$ hasValue && isset ($ options [$ directive ])) {
974+ if ($ options [$ directive ]) {
975+ $ this ->headers ->addCacheControlDirective (str_replace ('_ ' , '- ' , $ directive ));
976+ } else {
977+ $ this ->headers ->removeCacheControlDirective (str_replace ('_ ' , '- ' , $ directive ));
978+ }
979+ }
980+ }
981+
954982 if (isset ($ options ['public ' ])) {
955983 if ($ options ['public ' ]) {
956984 $ this ->setPublic ();
@@ -967,10 +995,6 @@ public function setCache(array $options): object
967995 }
968996 }
969997
970- if (isset ($ options ['immutable ' ])) {
971- $ this ->setImmutable ((bool ) $ options ['immutable ' ]);
972- }
973-
974998 return $ this ;
975999 }
9761000
0 commit comments