Skip to content

Commit

Permalink
Add theme rewriting support
Browse files Browse the repository at this point in the history
Fix issue preventing resources from being rewritten when in a subdirectory
Move incorrectly nested condition for prefetch injection
  • Loading branch information
DorsetDigital committed Jun 13, 2018
1 parent f4a5d0e commit 60523e8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 57 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The options are hopefully fairly self explanatory:
* `cdn_domain` - the full domain name of the CDN (required to enable module)
* `rewrite_assets` - whether to rewrite references to the 'assets' directory (default false)
* `rewrite_resources` - whether to rewrite references to the 'resources' directory (default false)
* `rewrite_themes` - whether to rewrite references to the 'themes' directory (default false)
* `add_debug_headers` - if enabled, adds extra HTML headers to show the various operations being applied (default false)
* `enable_in_dev` - enable the CDN in dev mode (default false)
* `subdirectory` - set this if your site is in a subdirectory (eg. for http://www.example.com/silverstripe - set this to 'silverstripe')
Expand Down
173 changes: 116 additions & 57 deletions src/CDNMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class CDNMiddleware implements HTTPMiddleware
*/
private static $rewrite_resources = false;

/**
* @config
*
* should themes also be rewritten?
* @var bool
*/
private static $rewrite_themes = false;

/**
* @config
*
Expand All @@ -73,13 +81,12 @@ class CDNMiddleware implements HTTPMiddleware

/**
* @config
*
*
* Add dns-prefetch links to the html head
* @var boolean
*/
private static $add_prefetch = false;



/**
* Process the request
* @param HTTPRequest $request
Expand Down Expand Up @@ -119,63 +126,115 @@ private function canRun()

private function updateBody(&$body, &$response)
{

if ($this->config()->get('rewrite_assets') === true) {
$this->rewriteAssets($body, $response);
}

if ($this->config()->get('rewrite_resources') === true) {
$this->rewriteResources($body, $response);
}

if ($this->config()->get('rewrite_themes') === true) {
$this->rewriteThemes($body, $response);
}

if ($this->config()->get('add_prefetch') === true) {
$this->addPrefetch($body, $response);
}
}

private function rewriteAssets(&$body, &$response)
{

$cdn = $this->config()->get('cdn_domain');
$subDir = $this->getSubdirectory();

if ($this->config()->get('rewrite_assets') === true) {
$search = [
'src="' . $subDir . 'assets/',
'src="/' . $subDir . 'assets/',
'src=\"/' . $subDir . 'assets/',
'href="/' . $subDir . 'assets/',
Director::absoluteBaseURL() . 'assets/'
];

$replace = [
'src="' . $cdn . '/' . $subDir . 'assets/',
'src="' . $cdn . '/' . $subDir . 'assets/',
'src=\"' . $cdn . '/' . $subDir . 'assets/',
'href="' . $cdn . '/' . $subDir . 'assets/',
$cdn . '/' . $subDir . 'assets/'
];

$body = str_replace($search, $replace, $body);

$search = [
'src="' . $subDir . 'assets/',
'src="/' . $subDir . 'assets/',
'src=\"/' . $subDir . 'assets/',
'href="/' . $subDir . 'assets/',
Director::absoluteBaseURL() . 'assets/'
];
if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Assets', 'Enabled');
}
}

$replace = [
'src="' . $cdn . '/' . $subDir . 'assets/',
'src="' . $cdn . '/' . $subDir . 'assets/',
'src=\"' . $cdn . '/' . $subDir . 'assets/',
'href="' . $cdn . '/' . $subDir . 'assets/',
$cdn . '/' . $subDir . 'assets/'
];
private function rewriteThemes(&$body, &$response)
{

$body = str_replace($search, $replace, $body);
$cdn = $this->config()->get('cdn_domain');
$subDir = $this->getSubdirectory();

if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Assets', 'Enabled');
}

if ($this->config()->get('add_prefetch') === true) {
$prefetchTag = $this->getPrefetchTag();
$body = str_replace('<head>', "<head>" . $prefetchTag, $body);
if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Prefetch', 'Enabled');
}
}
$search = [
'src="' . $subDir . 'themes/',
'src="/' . $subDir . 'themes/',
'src=\"/' . $subDir . 'themes/',
'href="/' . $subDir . 'themes/',
Director::absoluteBaseURL() . 'themes/'
];

$replace = [
'src="' . $cdn . '/' . $subDir . 'themes/',
'src="' . $cdn . '/' . $subDir . 'themes/',
'src=\"' . $cdn . '/' . $subDir . 'themes/',
'href="' . $cdn . '/' . $subDir . 'themes/',
$cdn . '/' . $subDir . 'themes/'
];

$body = str_replace($search, $replace, $body);

if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Themes', 'Enabled');
}
}

if ($this->config()->get('rewrite_resources') === true) {
private function rewriteResources(&$body, &$response)
{

$cdn = $this->config()->get('cdn_domain');
$subDir = $this->getSubdirectory();

$search = [
'src="/resources/',
'src="' . Director::absoluteBaseURL() . 'resources/',
'href="/resources/',
'href="' . Director::absoluteBaseURL() . 'resources/'
];
$search = [
'src="/' . $subDir . 'resources/',
'src="' . Director::absoluteBaseURL() . $subDir . 'resources/',
'href="/' . $subDir . 'resources/',
'href="' . Director::absoluteBaseURL() . $subDir . 'resources/'
];

$replace = [
'src="' . $cdn . '/resources/',
'src="' . $cdn . '/resources/',
'href="' . $cdn . '/resources/',
'href="' . $cdn . '/resources/'
];
$replace = [
'src="' . $cdn . '/' . $subDir . 'resources/',
'src="' . $cdn . '/' . $subDir . 'resources/',
'href="' . $cdn . '/' . $subDir . 'resources/',
'href="' . $cdn . '/' . $subDir . 'resources/'
];

$body = str_replace($search, $replace, $body);
$body = str_replace($search, $replace, $body);

if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Resources', 'Enabled');
}
if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Resources', 'Enabled');
}
}

private function addPrefetch(&$body, &$response)
{
$prefetchTag = $this->getPrefetchTag();
$body = str_replace('<head>', "<head>" . $prefetchTag, $body);
if ($this->config()->get('add_debug_headers') == true) {
$response->addHeader('X-CDN-Prefetch', 'Enabled');
}
}

Expand All @@ -187,16 +246,16 @@ private function getSubdirectory()
}
return $subDir;
}
private function getPrefetchTag()

private function getPrefetchTag()
{
$atts = [
'rel' => 'dns-prefetch',
'href' => $this->config()->get('cdn_domain')
];
$pfTag = "\n" . HTML::createTag('link', $atts);
return $pfTag;
$atts = [
'rel' => 'dns-prefetch',
'href' => $this->config()->get('cdn_domain')
];
$pfTag = "\n" . HTML::createTag('link', $atts);

return $pfTag;
}

/**
Expand All @@ -209,7 +268,7 @@ private function getPrefetchTag()
private function getIsAdmin(HTTPRequest $request)
{
$adminPaths = static::config()->get('admin_url_paths');
$adminPaths[ ] = AdminRootController::config()->get('url_base') . '/';
$adminPaths[] = AdminRootController::config()->get('url_base') . '/';
$currentPath = rtrim($request->getURL(), '/') . '/';
foreach ($adminPaths as $adminPath) {
if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) {
Expand Down

0 comments on commit 60523e8

Please sign in to comment.