@@ -85,6 +85,24 @@ class Response
85
85
const HTTP_NOT_EXTENDED = 510 ; // RFC2774
86
86
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511 ; // RFC6585
87
87
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
+
88
106
/**
89
107
* @var ResponseHeaderBag
90
108
*/
@@ -893,35 +911,10 @@ public function getEtag(): ?string
893
911
return $ this ->headers ->get ('ETag ' );
894
912
}
895
913
896
- /**
897
- * Sets the ETag value.
898
- *
899
- * @param string|null $etag The ETag unique identifier or null to remove the header
900
- * @param bool $weak Whether you want a weak ETag or not
901
- *
902
- * @return $this
903
- *
904
- * @final
905
- */
906
- public function setEtag (string $ etag = null , bool $ weak = false ): object
907
- {
908
- if (null === $ etag ) {
909
- $ this ->headers ->remove ('Etag ' );
910
- } else {
911
- if (0 !== strpos ($ etag , '" ' )) {
912
- $ etag = '" ' .$ etag .'" ' ;
913
- }
914
-
915
- $ this ->headers ->set ('ETag ' , (true === $ weak ? 'W/ ' : '' ).$ etag );
916
- }
917
-
918
- return $ this ;
919
- }
920
-
921
914
/**
922
915
* Sets the response's cache headers (validation and/or expiration).
923
916
*
924
- * Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable .
917
+ * Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag .
925
918
*
926
919
* @return $this
927
920
*
@@ -931,7 +924,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
931
924
*/
932
925
public function setCache (array $ options ): object
933
926
{
934
- if ($ diff = array_diff (array_keys ($ options ), [ ' etag ' , ' last_modified ' , ' max_age ' , ' s_maxage ' , ' private ' , ' public ' , ' immutable ' ] )) {
927
+ if ($ diff = array_diff (array_keys ($ options ), array_keys ( static :: HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES ) )) {
935
928
throw new \InvalidArgumentException (sprintf ('Response does not support the following options: "%s". ' , implode ('", " ' , $ diff )));
936
929
}
937
930
@@ -951,6 +944,18 @@ public function setCache(array $options): object
951
944
$ this ->setSharedMaxAge ($ options ['s_maxage ' ]);
952
945
}
953
946
947
+ foreach (self ::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $ directive => $ hasValue ) {
948
+ if (!$ hasValue ) {
949
+ if (isset ($ options [$ directive ])) {
950
+ if ($ options [$ directive ]) {
951
+ $ this ->headers ->addCacheControlDirective (str_replace ('_ ' , '- ' , $ directive ));
952
+ } else {
953
+ $ this ->headers ->removeCacheControlDirective (str_replace ('_ ' , '- ' , $ directive ));
954
+ }
955
+ }
956
+ }
957
+ }
958
+
954
959
if (isset ($ options ['public ' ])) {
955
960
if ($ options ['public ' ]) {
956
961
$ this ->setPublic ();
@@ -967,8 +972,29 @@ public function setCache(array $options): object
967
972
}
968
973
}
969
974
970
- if (isset ($ options ['immutable ' ])) {
971
- $ this ->setImmutable ((bool ) $ options ['immutable ' ]);
975
+ return $ this ;
976
+ }
977
+
978
+ /**
979
+ * Sets the ETag value.
980
+ *
981
+ * @param string|null $etag The ETag unique identifier or null to remove the header
982
+ * @param bool $weak Whether you want a weak ETag or not
983
+ *
984
+ * @return $this
985
+ *
986
+ * @final
987
+ */
988
+ public function setEtag (string $ etag = null , bool $ weak = false ): object
989
+ {
990
+ if (null === $ etag ) {
991
+ $ this ->headers ->remove ('Etag ' );
992
+ } else {
993
+ if (0 !== strpos ($ etag , '" ' )) {
994
+ $ etag = '" ' .$ etag .'" ' ;
995
+ }
996
+
997
+ $ this ->headers ->set ('ETag ' , (true === $ weak ? 'W/ ' : '' ).$ etag );
972
998
}
973
999
974
1000
return $ this ;
0 commit comments