Skip to content

Commit

Permalink
Merge pull request #26 from symbiote/feature-php-7-4
Browse files Browse the repository at this point in the history
add(7.4): Support for PHP 7.4
  • Loading branch information
Joshua Carter committed Jun 22, 2020
2 parents a5f82e8 + 9da231f commit 8762430
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
8 changes: 6 additions & 2 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

// Ensure compatibility with PHP 7.2 ("object" is a reserved word),
// with SilverStripe 3.6 (using Object) and SilverStripe 3.7 (using SS_Object)
if (!class_exists('SS_Object')) class_alias('Object', 'SS_Object');

Image::set_backend('Symbiote\\ContentServiceAssets\\ImageBackend');

//Object::add_extension('Folder', 'CDNFolder');
//Object::add_extension('File', 'CDNFile');
//SS_Object::add_extension('Folder', 'CDNFolder');
//SS_Object::add_extension('File', 'CDNFile');
48 changes: 24 additions & 24 deletions code/content/ContentDeliveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

/**
* Provides an interface to content delivery networks
*
*
* @author marcus@symbiote.com.au
* @license BSD License http://silverstripe.org/bsd-license/
*/
class ContentDeliveryService {

const CDN_THEME_PREFIX = 'cdntheme';

/**
* @var ContentService
*/
public $contentService;

private $cleanupFiles = array();

public static $dependencies = array(
'contentService' => '%$ContentService'
);

public function removeLocalFile($filepath) {
$this->cleanupFiles[] = $filepath;
}

public function cleanup()
{
foreach ($this->cleanupFiles as $path) {
Expand All @@ -33,32 +33,32 @@ public function cleanup()
}
}
}

/**
* Get the first available CDN for a given theme
*
*
* @param string $theme
* @return ThemeCdn
*/
public function getCdnForTheme($theme) {
$cdn = ThemeCdn::get()->filter('Theme', $theme);
return $cdn->first();
}

/**
* Store the contents of a folder on a CDN.
*
* If processReferences is set, relative URL references are attempted to be
* detected and stored remotely as well, with the file to be stored rewritten
* to refer to the CDN value. This really is only useful for CSS
* Store the contents of a folder on a CDN.
*
* If processReferences is set, relative URL references are attempted to be
* detected and stored remotely as well, with the file to be stored rewritten
* to refer to the CDN value. This really is only useful for CSS
*
* @param string $folder
* @param boolean $processReferences
* @param boolean $processReferences
*/
public function storeThemeFile($toCdn, $file, $forceUpdate = false, $processReferences = false) {
$mtime = @filemtime($file);
$relativeName = self::CDN_THEME_PREFIX . '/' . $mtime . '/' . trim(str_replace(Director::baseFolder(), '', $file), '/');

if (!$forceUpdate) {
// see if the file already exists, if not we do NOT do an update
$reader = $this->contentService->findReaderFor($toCdn, $relativeName);
Expand Down Expand Up @@ -88,25 +88,25 @@ public function storeThemeFile($toCdn, $file, $forceUpdate = false, $processRefe
$id = $writer->getContentId();
return $writer->getReader()->getURL();
}

protected function processFileReferences($toCdn, $file, $forceUpdate = false) {
$content = file_get_contents($file);

$processed = array();

if (preg_match_all('/url\((.*?)\)/', $content, $matches)) {
foreach ($matches[1] as $segment) {
$segment = trim($segment, '\'"');

if (strpos($segment, '#') !== false) {
$segment = substr($segment, 0, strpos($segment, '#'));
}

if (isset($processed[$segment])) {
continue;
}

if (strpos($segment, '//') !== false || $segment{0} == '/') {
if (strpos($segment, '//') !== false || $segment[0] == '/') {
continue;
}

Expand All @@ -116,7 +116,7 @@ protected function processFileReferences($toCdn, $file, $forceUpdate = false) {
}

$replacement = $this->storeThemeFile($toCdn, $realPath, $forceUpdate);

$content = str_replace($segment, $replacement, $content);
$processed[$segment] = $replacement;
}
Expand All @@ -127,7 +127,7 @@ protected function processFileReferences($toCdn, $file, $forceUpdate = false) {
$file = $file . '.cdn';
file_put_contents($file, $content);
}

return $file;
}
}
30 changes: 15 additions & 15 deletions code/extensions/CdnControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@
class CdnControllerExtension extends Extension {

static $store_type = 'File';

/**
*
* @var ContentDeliveryService
*/
public $contentDelivery;

/**
*
* @var ContentService
* @var ContentService
*/
public $contentService;

/**
*
* @var AssetUrlConversionFilter
*/
public $contentFilter;

protected $currentCdn;

public function requireCDN($assetPath, $uploadMissing = false, $verify = false) {
// return the cdn URL for the given asset
$type = strpos($assetPath, '.css') ? 'css' : 'js';
switch ($type) {
case 'css':
case 'css':
Requirements::css($this->CDNPath($assetPath, $uploadMissing, $verify));
break;
case 'js':
case 'js':
Requirements::javascript($this->CDNPath($assetPath, $uploadMissing, $verify));
break;
}
Expand All @@ -45,23 +45,23 @@ public function currentThemeCdn() {
if (!$this->currentCdn) {
$this->currentCdn = $this->contentDelivery->getCdnForTheme(Config::inst()->get('SSViewer', 'theme'));
}

return $this->currentCdn;
}

public function CDNPath($assetPath, $uploadMissing = false, $verify = false) {
$current = $this->currentThemeCdn();
if ($current && (Director::isLive() || (isset($_GET['stage']) && $_GET['stage'] == 'Live'))) {
$store = $current->StoreIn;
// if we want to upload missing files, verify their existence.

// if we want to upload missing files, verify their existence.
if (!$verify && $uploadMissing) {
$verify = true;
}

$mtime = @filemtime(Director::baseFolder().'/'.$assetPath);
$timedAssetPath = ContentDeliveryService::CDN_THEME_PREFIX . '/' . $mtime . '/' . $assetPath;

$reader = $this->contentService->findReaderFor($store, $timedAssetPath);
if ($reader && (!$verify || $reader->exists())) {
return $reader->getURL();
Expand All @@ -71,7 +71,7 @@ public function CDNPath($assetPath, $uploadMissing = false, $verify = false) {
if (strpos($assetPath, '.css')) {
// if we're a relative path, make absolute
$fullPath = $assetPath;
if ($assetPath{0} != '/') {
if ($assetPath[0] != '/') {
$fullPath = Director::baseFolder().'/' . $assetPath;
}
if (!file_exists($fullPath)) {
Expand All @@ -84,7 +84,7 @@ public function CDNPath($assetPath, $uploadMissing = false, $verify = false) {
// otherwise just upload
$writer = $this->getWriter();
// otherwise, we need to write the file

$writer->write(Director::baseFolder().'/'.$assetPath, $timedAssetPath);

return $writer->getReader()->getURL();
Expand All @@ -104,7 +104,7 @@ protected function getWriter() {
}
return $writer;
}

public function beforeCallActionHandler($request, $action) {
if (
$this->owner instanceof LeftAndMain ||
Expand Down

0 comments on commit 8762430

Please sign in to comment.