Skip to content

Commit 71d3328

Browse files
committed
Support response config cache
Add Accepts to Vary header
1 parent dde3b40 commit 71d3328

File tree

7 files changed

+91
-22
lines changed

7 files changed

+91
-22
lines changed

src/Dispatch.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Packaged\Dispatch;
33

44
use Composer\Autoload\ClassLoader;
5+
use Exception;
56
use Packaged\Config\Provider\ConfigProvider;
67
use Packaged\Dispatch\Resources\AbstractDispatchableResource;
78
use Packaged\Dispatch\Resources\AbstractResource;
@@ -65,12 +66,17 @@ class Dispatch
6566
protected $_bits = 0;
6667

6768
private const BIT_WEBP = 0b1;
69+
/**
70+
* @var ResponseCacheConfig
71+
*/
72+
protected $_defaultCacheConfig;
6873

6974
public function __construct($projectRoot, $baseUri = null, ClassLoader $loader = null)
7075
{
7176
$this->_projectRoot = $projectRoot;
7277
$this->_config = new ConfigProvider();
7378
$this->_resourceStore = new ResourceStore();
79+
$this->_defaultCacheConfig = new ResponseCacheConfig();
7480
$this->_baseUri = $baseUri;
7581
$this->_classLoader = $loader;
7682
}
@@ -176,7 +182,7 @@ public function getComponentAliases()
176182
* @param Request $request
177183
*
178184
* @return Response
179-
* @throws \Exception
185+
* @throws Exception
180186
*/
181187
public function handleRequest(Request $request): Response
182188
{
@@ -282,7 +288,7 @@ public function handleRequest(Request $request): Response
282288
$resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems());
283289
}
284290
}
285-
return ResourceFactory::create($resource, $contentHashMatch);
291+
return ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false);
286292
}
287293

288294
public function config()

src/ResourceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public function getFilePath($relativePath)
374374
{
375375
return Path::system($this->_componentPath, $relativePath);
376376
}
377-
throw new \Exception("invalid map type");
377+
throw new Exception("invalid map type");
378378
}
379379

380380
public static function componentClass(string $componentClassName, $options = [])

src/Resources/AbstractDispatchableResource.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22
namespace Packaged\Dispatch\Resources;
33

4+
use Exception;
45
use Packaged\Dispatch\ResourceManager;
56
use Packaged\Helpers\Path;
67
use Packaged\Helpers\Strings;
78
use Packaged\Helpers\ValueAs;
9+
use RuntimeException;
810
use function array_shift;
911
use function base64_encode;
1012
use function basename;
@@ -22,7 +24,6 @@ abstract class AbstractDispatchableResource extends AbstractResource implements
2224
*/
2325
protected $_manager;
2426
protected $_path;
25-
protected $_workingDirectory;
2627
protected $_processedContent = false;
2728

2829
public function setManager(ResourceManager $am)
@@ -119,7 +120,7 @@ protected function _minify() { }
119120
* @param $path
120121
*
121122
* @return string
122-
* @throws \Exception
123+
* @throws Exception
123124
*/
124125
protected function _getDispatchUrl($path): string
125126
{
@@ -145,7 +146,7 @@ protected function _getDispatchUrl($path): string
145146
{
146147
$appendChar = min($queryPos, $fragPos) == $queryPos ? '?' : '#';
147148
}
148-
list($newPath, $append) = Strings::explode($appendChar, $path, [$path, null], 2);
149+
[$newPath, $append] = Strings::explode($appendChar, $path, [$path, null], 2);
149150
$append = $append ? $appendChar . $append : null;
150151
}
151152
else
@@ -160,7 +161,7 @@ protected function _getDispatchUrl($path): string
160161
{
161162
$url = $this->_manager->getResourceUri($newPath);
162163
}
163-
catch(\RuntimeException $e)
164+
catch(RuntimeException $e)
164165
{
165166
$url = null;
166167
}
@@ -202,7 +203,7 @@ protected function _makeFullPath($relativePath, $workingDirectory)
202203
$relativePath = Path::url(...$newParts);
203204

204205
// currentDir
205-
$relativePath = preg_replace('~(?<=\/|^).\/~', '', $relativePath);
206+
$relativePath = preg_replace('~(?<=/|^)./~', '', $relativePath);
206207

207208
return $relativePath;
208209
}

src/Resources/CssResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Packaged\Dispatch\Resources;
33

4+
use Exception;
45
use Packaged\Helpers\Strings;
56
use function preg_replace;
67
use function preg_replace_callback;
@@ -45,7 +46,7 @@ protected function _dispatch()
4546
* @param $uri
4647
*
4748
* @return string
48-
* @throws \Exception
49+
* @throws Exception
4950
*/
5051
protected function _dispatchUrlPaths($uri)
5152
{

src/Resources/JavascriptResource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Packaged\Dispatch\Resources;
33

4+
use Exception;
45
use JShrink\Minifier;
56
use Packaged\Helpers\Strings;
67
use function file_exists;
@@ -40,7 +41,7 @@ protected function _dispatch()
4041
* @param $uri
4142
*
4243
* @return string
43-
* @throws \Exception
44+
* @throws Exception
4445
*/
4546
protected function _dispatchImportUrls($uri)
4647
{
@@ -67,7 +68,7 @@ protected function _minify()
6768
{
6869
$this->_content = Minifier::minify($this->_content);
6970
}
70-
catch(\Exception $e)
71+
catch(Exception $e)
7172
{
7273
//If minification doesnt work, return the original content
7374
}

src/Resources/ResourceFactory.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
namespace Packaged\Dispatch\Resources;
33

4+
use DateInterval;
5+
use DateTime;
6+
use DateTimeZone;
7+
use Exception;
48
use Packaged\Dispatch\Resources\Font\AfmResource;
59
use Packaged\Dispatch\Resources\Font\DfontResource;
610
use Packaged\Dispatch\Resources\Font\EotResource;
@@ -22,6 +26,7 @@
2226
use Packaged\Dispatch\Resources\Video\MpegResource;
2327
use Packaged\Dispatch\Resources\Video\QuicktimeResource;
2428
use Packaged\Dispatch\Resources\Video\WebmResource;
29+
use Packaged\Dispatch\ResponseCacheConfig;
2530
use Packaged\Http\Response;
2631
use function array_keys;
2732
use 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

src/ResponseCacheConfig.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace Packaged\Dispatch;
3+
4+
class ResponseCacheConfig
5+
{
6+
protected $_varyHeader = 'Accept-Encoding, Accept';
7+
protected $_cacheSeconds = 31536000;
8+
9+
/**
10+
* @return string
11+
*/
12+
public function getVaryHeader(): string
13+
{
14+
return $this->_varyHeader;
15+
}
16+
17+
/**
18+
* @param string $vary
19+
*
20+
* @return ResponseCacheConfig
21+
*/
22+
public function setVaryHeader(string $vary)
23+
{
24+
$this->_varyHeader = $vary;
25+
return $this;
26+
}
27+
28+
/**
29+
* @return int
30+
*/
31+
public function getCacheSeconds(): int
32+
{
33+
return $this->_cacheSeconds;
34+
}
35+
36+
/**
37+
* @param int $cacheTimeSeconds
38+
*
39+
* @return ResponseCacheConfig
40+
*/
41+
public function setCacheSeconds(int $cacheTimeSeconds)
42+
{
43+
$this->_cacheSeconds = $cacheTimeSeconds;
44+
return $this;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)