@@ -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
*/
@@ -909,10 +927,10 @@ public function setEtag(string $etag = null, bool $weak = false): object
909
927
$ this ->headers ->remove ('Etag ' );
910
928
} else {
911
929
if (0 !== strpos ($ etag , '" ' )) {
912
- $ etag = '" ' . $ etag. '" ' ;
930
+ $ etag = '" ' . $ etag . '" ' ;
913
931
}
914
932
915
- $ this ->headers ->set ('ETag ' , (true === $ weak ? 'W/ ' : '' ). $ etag );
933
+ $ this ->headers ->set ('ETag ' , (true === $ weak ? 'W/ ' : '' ) . $ etag );
916
934
}
917
935
918
936
return $ this ;
@@ -921,7 +939,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
921
939
/**
922
940
* Sets the response's cache headers (validation and/or expiration).
923
941
*
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 .
925
943
*
926
944
* @return $this
927
945
*
@@ -931,7 +949,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
931
949
*/
932
950
public function setCache (array $ options ): object
933
951
{
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 ) )) {
935
953
throw new \InvalidArgumentException (sprintf ('Response does not support the following options: "%s". ' , implode ('", " ' , $ diff )));
936
954
}
937
955
@@ -951,6 +969,16 @@ public function setCache(array $options): object
951
969
$ this ->setSharedMaxAge ($ options ['s_maxage ' ]);
952
970
}
953
971
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
+
954
982
if (isset ($ options ['public ' ])) {
955
983
if ($ options ['public ' ]) {
956
984
$ this ->setPublic ();
@@ -967,10 +995,6 @@ public function setCache(array $options): object
967
995
}
968
996
}
969
997
970
- if (isset ($ options ['immutable ' ])) {
971
- $ this ->setImmutable ((bool ) $ options ['immutable ' ]);
972
- }
973
-
974
998
return $ this ;
975
999
}
976
1000
0 commit comments