11<?php
22namespace Packaged \Dispatch \Resources ;
33
4+ use DateInterval ;
5+ use DateTime ;
6+ use DateTimeZone ;
7+ use Exception ;
48use Packaged \Dispatch \Resources \Font \AfmResource ;
59use Packaged \Dispatch \Resources \Font \DfontResource ;
610use Packaged \Dispatch \Resources \Font \EotResource ;
2226use Packaged \Dispatch \Resources \Video \MpegResource ;
2327use Packaged \Dispatch \Resources \Video \QuicktimeResource ;
2428use Packaged \Dispatch \Resources \Video \WebmResource ;
29+ use Packaged \Dispatch \ResponseCacheConfig ;
2530use Packaged \Http \Response ;
2631use function array_keys ;
2732use function file_exists ;
@@ -110,11 +115,12 @@ public static function getExtensionResource($extension): DispatchResource
110115 }
111116
112117 /**
113- * @param DispatchResource $resource
118+ * @param DispatchResource $resource
114119 *
115- * @param bool $cache
120+ * @param ResponseCacheConfig| bool $cache
116121 *
117122 * @return Response
123+ * @throws Exception
118124 */
119125 public static function create (DispatchResource $ resource , $ cache = true )
120126 {
@@ -124,24 +130,31 @@ public static function create(DispatchResource $resource, $cache = true)
124130 $ response ->headers ->set ('Content-Type ' , $ resource ->getContentType ());
125131 $ response ->headers ->set ('X-Content-Type-Options ' , 'nosniff ' );
126132
127- //Ensure the cache varies on the encoding
128- //Domain specific content will vary on the uri itself
129- $ response ->headers ->set ("Vary " , "Accept-Encoding " );
133+ if ($ cache === true )
134+ {
135+ $ cache = new ResponseCacheConfig ();
136+ }
130137
131- if ($ cache )
138+ if ($ cache && $ cache instanceof ResponseCacheConfig )
132139 {
140+ //Ensure the cache varies on the encoding
141+ //Domain specific content will vary on the uri itself
142+ $ response ->headers ->set ("Vary " , $ cache ->getVaryHeader ());
143+
133144 //Set the etag to the hash of the request uri, as it is in itself a hash
134145 $ response ->setEtag ($ resource ->getHash ());
135146 $ response ->setPublic ();
136147
137148 //This resource should last for 1 year in cache
138- $ response ->setMaxAge (31536000 );
139- $ response ->setSharedMaxAge (31536000 );
140- $ response ->setExpires ((new \DateTime ())->add (new \DateInterval ('P365D ' )));
149+ $ response ->setMaxAge ($ cache ->getCacheSeconds ());
150+ $ response ->setSharedMaxAge ($ cache ->getCacheSeconds ());
151+ $ response ->setExpires (
152+ (new DateTime ())->add (DateInterval::createFromDateString ($ cache ->getCacheSeconds () . ' seconds ' ))
153+ );
141154
142155 //Set the last modified date to now
143- $ date = new \ DateTime ();
144- $ date ->setTimezone (new \ DateTimeZone ('UTC ' ));
156+ $ date = new DateTime ();
157+ $ date ->setTimezone (new DateTimeZone ('UTC ' ));
145158 $ response ->headers ->set ('Last-Modified ' , $ date ->format ('D, d M Y H:i:s ' ) . ' GMT ' );
146159 }
147160
0 commit comments