Skip to content

Commit

Permalink
Support response config cache
Browse files Browse the repository at this point in the history
Add Accepts to Vary header
  • Loading branch information
bajb committed Jul 17, 2020
1 parent dde3b40 commit 71d3328
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Packaged\Dispatch;

use Composer\Autoload\ClassLoader;
use Exception;
use Packaged\Config\Provider\ConfigProvider;
use Packaged\Dispatch\Resources\AbstractDispatchableResource;
use Packaged\Dispatch\Resources\AbstractResource;
Expand Down Expand Up @@ -65,12 +66,17 @@ class Dispatch
protected $_bits = 0;

private const BIT_WEBP = 0b1;
/**
* @var ResponseCacheConfig
*/
protected $_defaultCacheConfig;

public function __construct($projectRoot, $baseUri = null, ClassLoader $loader = null)
{
$this->_projectRoot = $projectRoot;
$this->_config = new ConfigProvider();
$this->_resourceStore = new ResourceStore();
$this->_defaultCacheConfig = new ResponseCacheConfig();
$this->_baseUri = $baseUri;
$this->_classLoader = $loader;
}
Expand Down Expand Up @@ -176,7 +182,7 @@ public function getComponentAliases()
* @param Request $request
*
* @return Response
* @throws \Exception
* @throws Exception
*/
public function handleRequest(Request $request): Response
{
Expand Down Expand Up @@ -282,7 +288,7 @@ public function handleRequest(Request $request): Response
$resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems());
}
}
return ResourceFactory::create($resource, $contentHashMatch);
return ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false);
}

public function config()
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public function getFilePath($relativePath)
{
return Path::system($this->_componentPath, $relativePath);
}
throw new \Exception("invalid map type");
throw new Exception("invalid map type");
}

public static function componentClass(string $componentClassName, $options = [])
Expand Down
11 changes: 6 additions & 5 deletions src/Resources/AbstractDispatchableResource.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
namespace Packaged\Dispatch\Resources;

use Exception;
use Packaged\Dispatch\ResourceManager;
use Packaged\Helpers\Path;
use Packaged\Helpers\Strings;
use Packaged\Helpers\ValueAs;
use RuntimeException;
use function array_shift;
use function base64_encode;
use function basename;
Expand All @@ -22,7 +24,6 @@ abstract class AbstractDispatchableResource extends AbstractResource implements
*/
protected $_manager;
protected $_path;
protected $_workingDirectory;
protected $_processedContent = false;

public function setManager(ResourceManager $am)
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function _minify() { }
* @param $path
*
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _getDispatchUrl($path): string
{
Expand All @@ -145,7 +146,7 @@ protected function _getDispatchUrl($path): string
{
$appendChar = min($queryPos, $fragPos) == $queryPos ? '?' : '#';
}
list($newPath, $append) = Strings::explode($appendChar, $path, [$path, null], 2);
[$newPath, $append] = Strings::explode($appendChar, $path, [$path, null], 2);
$append = $append ? $appendChar . $append : null;
}
else
Expand All @@ -160,7 +161,7 @@ protected function _getDispatchUrl($path): string
{
$url = $this->_manager->getResourceUri($newPath);
}
catch(\RuntimeException $e)
catch(RuntimeException $e)
{
$url = null;
}
Expand Down Expand Up @@ -202,7 +203,7 @@ protected function _makeFullPath($relativePath, $workingDirectory)
$relativePath = Path::url(...$newParts);

// currentDir
$relativePath = preg_replace('~(?<=\/|^).\/~', '', $relativePath);
$relativePath = preg_replace('~(?<=/|^)./~', '', $relativePath);

return $relativePath;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/CssResource.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Dispatch\Resources;

use Exception;
use Packaged\Helpers\Strings;
use function preg_replace;
use function preg_replace_callback;
Expand Down Expand Up @@ -45,7 +46,7 @@ protected function _dispatch()
* @param $uri
*
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _dispatchUrlPaths($uri)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Resources/JavascriptResource.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Dispatch\Resources;

use Exception;
use JShrink\Minifier;
use Packaged\Helpers\Strings;
use function file_exists;
Expand Down Expand Up @@ -40,7 +41,7 @@ protected function _dispatch()
* @param $uri
*
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _dispatchImportUrls($uri)
{
Expand All @@ -67,7 +68,7 @@ protected function _minify()
{
$this->_content = Minifier::minify($this->_content);
}
catch(\Exception $e)
catch(Exception $e)
{
//If minification doesnt work, return the original content
}
Expand Down
35 changes: 24 additions & 11 deletions src/Resources/ResourceFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace Packaged\Dispatch\Resources;

use DateInterval;
use DateTime;
use DateTimeZone;
use Exception;
use Packaged\Dispatch\Resources\Font\AfmResource;
use Packaged\Dispatch\Resources\Font\DfontResource;
use Packaged\Dispatch\Resources\Font\EotResource;
Expand All @@ -22,6 +26,7 @@
use Packaged\Dispatch\Resources\Video\MpegResource;
use Packaged\Dispatch\Resources\Video\QuicktimeResource;
use Packaged\Dispatch\Resources\Video\WebmResource;
use Packaged\Dispatch\ResponseCacheConfig;
use Packaged\Http\Response;
use function array_keys;
use function file_exists;
Expand Down Expand Up @@ -110,11 +115,12 @@ public static function getExtensionResource($extension): DispatchResource
}

/**
* @param DispatchResource $resource
* @param DispatchResource $resource
*
* @param bool $cache
* @param ResponseCacheConfig|bool $cache
*
* @return Response
* @throws Exception
*/
public static function create(DispatchResource $resource, $cache = true)
{
Expand All @@ -124,24 +130,31 @@ public static function create(DispatchResource $resource, $cache = true)
$response->headers->set('Content-Type', $resource->getContentType());
$response->headers->set('X-Content-Type-Options', 'nosniff');

//Ensure the cache varies on the encoding
//Domain specific content will vary on the uri itself
$response->headers->set("Vary", "Accept-Encoding");
if($cache === true)
{
$cache = new ResponseCacheConfig();
}

if($cache)
if($cache && $cache instanceof ResponseCacheConfig)
{
//Ensure the cache varies on the encoding
//Domain specific content will vary on the uri itself
$response->headers->set("Vary", $cache->getVaryHeader());

//Set the etag to the hash of the request uri, as it is in itself a hash
$response->setEtag($resource->getHash());
$response->setPublic();

//This resource should last for 1 year in cache
$response->setMaxAge(31536000);
$response->setSharedMaxAge(31536000);
$response->setExpires((new \DateTime())->add(new \DateInterval('P365D')));
$response->setMaxAge($cache->getCacheSeconds());
$response->setSharedMaxAge($cache->getCacheSeconds());
$response->setExpires(
(new DateTime())->add(DateInterval::createFromDateString($cache->getCacheSeconds() . ' seconds'))
);

//Set the last modified date to now
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
}

Expand Down
47 changes: 47 additions & 0 deletions src/ResponseCacheConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Packaged\Dispatch;

class ResponseCacheConfig
{
protected $_varyHeader = 'Accept-Encoding, Accept';
protected $_cacheSeconds = 31536000;

/**
* @return string
*/
public function getVaryHeader(): string
{
return $this->_varyHeader;
}

/**
* @param string $vary
*
* @return ResponseCacheConfig
*/
public function setVaryHeader(string $vary)
{
$this->_varyHeader = $vary;
return $this;
}

/**
* @return int
*/
public function getCacheSeconds(): int
{
return $this->_cacheSeconds;
}

/**
* @param int $cacheTimeSeconds
*
* @return ResponseCacheConfig
*/
public function setCacheSeconds(int $cacheTimeSeconds)
{
$this->_cacheSeconds = $cacheTimeSeconds;
return $this;
}

}

0 comments on commit 71d3328

Please sign in to comment.