1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-16 20:28:28 +01:00

Preference added to enable experimental JS/CSS Caching. JS/CSS Caching tweaked to correct broken paths.

This commit is contained in:
Cameron 2016-02-05 15:31:54 -08:00
parent fc7efdc0c6
commit a1c4707bcf
5 changed files with 92 additions and 3 deletions

View File

@ -55,6 +55,7 @@ if (isset($_POST['submit_cache']))
{
e107::getConfig()->set('cachestatus', intval($_POST['cachestatus']))
->set('syscachestatus', intval($_POST['syscachestatus']))
->set('jscsscachestatus', intval($_POST['jscsscachestatus']))
->save(false);
}
@ -89,6 +90,11 @@ if (isset($_POST['trigger_empty_cache']))
e107::getAdminLog()->flushMessages(CACLAN_25);
break;
case 'empty_jscss':
e107::getCache()->clearAll('jscss');
// e107::getAdminLog()->flushMessages(CACLAN_5);
break;
// all
default:
e107::getCache()->clearAll('content');
@ -105,16 +111,19 @@ $syscache_files = glob(e_CACHE_CONTENT.'S_*.*');
$cache_files = glob(e_CACHE_CONTENT.'C_*.*');
$imgcache_files = glob(e_CACHE_IMAGE.'*.cache.bin');
$dbcache_files = glob(e_CACHE_DB.'*.php');
$jscsscache_files = glob(e_WEB.'cache/*.*');
$syscache_files_num = count($syscache_files);
$cache_files_num = count($cache_files);
$imgcache_files_num = count($imgcache_files);
$dbcache_files_num = count($dbcache_files);
$jscsscache_files_num = count($jscsscache_files);
$syscache_label = $syscache_files_num.' '.($syscache_files_num != 1 ? CACLAN_19 : CACLAN_18);
$contentcache_label = $cache_files_num.' '.($cache_files_num != 1 ? CACLAN_19 : CACLAN_18);
$imgcache_label = $imgcache_files_num.' '.($imgcache_files_num != 1 ? CACLAN_19 : CACLAN_18);
$dbcache_label = $dbcache_files_num.' '.($dbcache_files_num != 1 ? CACLAN_19 : CACLAN_18);
$jscsscache_label = $jscsscache_files_num.' '.($jscsscache_files_num != 1 ? CACLAN_19 : CACLAN_18);
$text = "
<form method='post' action='".e_SELF."'>
@ -154,6 +163,19 @@ $text = "
".$frm->radio_switch('syscachestatus', e107::getPref('syscachestatus'))."
</td>
</tr>
<tr>
<td>
<strong class='e-tip'>".CACLAN_28." <span class='label label-warning'>Experimental</span></strong>
<div class='field-help'>".CACLAN_29."</div>
</td>
<td>{$jscsscache_label}</td>
<td class='left middle'>
".$frm->radio_switch('jscsscachestatus', e107::getPref('jscsscachestatus'))."
</td>
</tr>
<tr>
<td>
<strong class='e-tip'>".CACLAN_20."</strong>
@ -174,6 +196,9 @@ $text = "
".LAN_ENABLED."
</td>
</tr>
</tbody>
</table>
<div class='buttons-bar form-inline'>
@ -182,6 +207,7 @@ $text = "
'empty_all' => CACLAN_26,
'empty_contentcache' => CACLAN_5,
'empty_syscache' => CACLAN_16,
'empty_jscss' => CACLAN_30,
'empty_dbcache' => CACLAN_24,
'empty_imgcache' => CACLAN_25,
'empty_browsercache' => CACLAN_27,

View File

@ -326,6 +326,12 @@ class ecache {
$mask = ($mask == null) ? '.*\.cache\.bin' : $mask;
}
if($type == 'jscss')
{
$path = e_WEB."cache/";
$mask = ($mask == null) ? '.*(\.js|\.css)$' : $mask;
}
if((null == $path) || (null == $mask))
{
return;

View File

@ -206,6 +206,9 @@ class e_jsmanager
*/
protected $_dependenceLoaded = array();
protected $_cache_enabled = false;
/**
* Constructor
*
@ -275,6 +278,7 @@ class e_jsmanager
}
$customJqueryUrls = e107::getPref('library-jquery-urls');
$this->_cache_enabled = e107::getPref('jscsscachestatus',false);
if(vartrue($customJqueryUrls) && $this->_in_admin === false)
{
@ -1345,7 +1349,12 @@ class e_jsmanager
*/
private function addCache($type,$path)
{
return false; //return false if cache is disabled - CURRENTLY DISABLED - TODO Add Pref etc.
if($this->_cache_enabled != true)
{
return false;
}
if(substr($path,0,2) == '//' || e_ADMIN_AREA){ return false; }
$localPath = e107::getParser()->replaceConstants($path);
$this->_cache_list[$type][] = $localPath;
@ -1362,6 +1371,10 @@ class e_jsmanager
*/
public function renderCached($type)
{
if($this->_cache_enabled != true)
{
return false;
}
if(!empty($this->_cache_list[$type]))
{
@ -1376,8 +1389,8 @@ class e_jsmanager
foreach($this->_cache_list[$type] as $k=>$path)
{
$content .= "\n\n/* ".str_replace("../",'',$path)." */ \n\n";
$content .= file_get_contents($path);
$content .= "\n\n/* File: ".str_replace("../",'',$path)." */ \n\n";
$content .= $this->getCacheFileContent($path, $type);
}
if(!@file_put_contents($saveFilePath, $content))
@ -1412,6 +1425,46 @@ class e_jsmanager
}
/**
* Get js/css file to be cached and update url links.
* @param $path string
* @param $type string (js|css)
* @return mixed|string
*/
function getCacheFileContent($path, $type)
{
$content = @file_get_contents($path);
if($type == 'js')
{
// e107::minify() todo
return $content;
}
preg_match_all('/url\([\'"]?([^\'"\) ]*)[\'"]?\)/',$content, $match);
$newpath = array();
if(empty($match[0]))
{
return $content;
}
foreach($match[1] as $k=>$v)
{
if(substr($v,5) == 'data:')
{
unset($match[0][$k]);
continue;
}
$dir = "url(../../".dirname($path)."/".$v.")"; // relative to e_WEB_ABS."cache/";
$newpath[$k] = $dir;
}
return str_replace($match[0], $newpath, $content);
}
function getCacheFileId($paths)
{
$id = '';

View File

@ -32,4 +32,8 @@ define("CACLAN_24", "Empty DB Structure Cache");
define("CACLAN_25", "Empty Thumbnail Cache");
define("CACLAN_26", "Empty All Cache");
define("CACLAN_27", "Empty Browser Cache");
define("CACLAN_28", "JS/CSS Cache");
define("CACLAN_29", "Consolidate and cache javascript files and cascading stylesheet files.");
define("CACLAN_30", "Empty JS/CSS Cache");
?>

0
e107_web/cache/index.html vendored Normal file
View File