Skip to content

Commit

Permalink
Refactoring JS files loading
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan committed Mar 16, 2019
1 parent 2bad1e8 commit d405ea5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 60 deletions.
44 changes: 24 additions & 20 deletions core/js/script.php
@@ -1,42 +1,46 @@
<?php
/**
* Combined JavaScript Files
* Combined JavaScript file
* @author Stefan Seehafer <sea75300@yahoo.de>
* @copyright (c) 2011-2018, Stefan Seehafer
* @copyright (c) 2011-2019, Stefan Seehafer
* @license http://www.gnu.org/licenses/gpl.txt GPLv3
*/

require_once dirname(dirname(__DIR__)).'/inc/common.php';

$data = ['content' => '', 'filesize' => 0];
$unique = preg_replace('/([^a-z0-9]*)/', '', fpcm\classes\http::getOnly('uq', [ fpcm\classes\http::FILTER_TRIM ]));
$cacheName = fpcm\view\view::JS_FILES_CACHE.'data'.$unique;

/* @var $cache fpcm\classes\cache */
$cache = \fpcm\classes\loader::getObject('\fpcm\classes\cache');
$data = $cache->read($cacheName);

$cacheName = 'theme/jsfiles';
if (!is_array($data) || \fpcm\classes\baseconfig::installerEnabled() || FPCM_DEBUG) {
$data = [
'content' => '',
'filesize' => 0
];
}

if ($cache->isExpired($cacheName) || \fpcm\classes\baseconfig::installerEnabled() || FPCM_DEBUG) {

$jsFiles = array(
__DIR__.'/ajax.js',
__DIR__.'/ui.js',
__DIR__.'/notifications.js',
__DIR__.'/system.js'
);


$jsFiles = $cache->read(fpcm\view\view::JS_FILES_CACHE.$unique);
if (!is_array($jsFiles)) {
exit;
}

foreach ($jsFiles as $jsFile) {

$fileContent = '/* '.\fpcm\model\files\ops::removeBaseDir($jsFile).' */'.PHP_EOL.file_get_contents($jsFile).PHP_EOL.PHP_EOL;
if (!$fileContent) {
$fileContent = file_get_contents($jsFile);
if (!trim($fileContent)) {
continue;
}

$data['content'] .= $fileContent;
$data['filesize'] += filesize($jsFile);

$data['content'] .= '/* '.\fpcm\model\files\ops::removeBaseDir($jsFile).' */'.PHP_EOL.$fileContent.PHP_EOL.PHP_EOL;
}

$cache->write($cacheName, $data, FPCM_LANGCACHE_TIMEOUT);
} else {
$data = $cache->read($cacheName);
$data['filesize'] = strlen($data['content']);
$cache->write($cacheName, $data);
}

header("Content-Type: application/javascript");
Expand Down
125 changes: 85 additions & 40 deletions inc/view/view.php
Expand Up @@ -25,6 +25,11 @@ class view {
const ROOTURL_CORE_THEME = '{$coreTheme}';
const ROOTURL_LIB = '{$lib}';

const JS_FILETYP_URL = 0b100;
const JS_FILETYP_FILE = 0b010;
const JS_FILETYP_FILE_EXT = 0b001;
const JS_FILES_CACHE = 'themejsfiles/';

/**
* Complete view path
* @var string
Expand All @@ -37,6 +42,12 @@ class view {
*/
protected $viewName = '';

/**
* View file path hash
* @var string
*/
protected $viewHash = '';

/**
* Form action path
* @var string
Expand All @@ -61,6 +72,13 @@ class view {
*/
protected $viewJsFiles = [];

/**
* Local view files in core/js
* @var array
* @since FPCm 4.1
*/
protected $viewJsFilesLocal = [];

/**
* View CSS files
* @var array
Expand Down Expand Up @@ -144,6 +162,13 @@ class view {
* @var bool
*/
protected $showPageToken = true;

/**
* Root urls for replacements
* @var array
* @since FPCm 4.1
*/
protected $rootUrls = [];

/**
* Konstruktor
Expand All @@ -166,6 +191,12 @@ public function __construct($viewName = '', $module = false)
$this->notifications = \fpcm\classes\loader::getObject('\fpcm\model\theme\notifications');
}

$this->rootUrls = [
self::ROOTURL_LIB => \fpcm\classes\dirs::getLibUrl(''),
self::ROOTURL_CORE_JS => \fpcm\classes\dirs::getCoreUrl(\fpcm\classes\dirs::CORE_JS, ''),
self::ROOTURL_CORE_THEME => \fpcm\classes\dirs::getCoreUrl(\fpcm\classes\dirs::CORE_THEME, '')
];

$this->defaultViewVars = new viewVars();
$this->initFileLib();
}
Expand Down Expand Up @@ -206,37 +237,39 @@ protected function initFileLib()
*/
private function addRootPath($item)
{
if (!trim($item) || substr($item, 0, 4) === 'http') {
return $item;
}

$item = str_replace('//', '/', $item);
if (substr($item, 0, 2) !== '{$' && substr($item, -3) === '.js') {
$item = self::ROOTURL_CORE_JS.$item;
}

$cacheName = 'system/rootpaths'.\fpcm\classes\baseconfig::canHttps();
$checks = $this->cache->read($cacheName);

if ($this->cache->isExpired($cacheName) || !is_array($checks)) {
$checks = [];
if (!$item) {
return '';
}

$hash = \fpcm\classes\tools::getHash($item);
if (isset($checks[$hash])) {
return $checks[$hash];
}
$jsCorePath = '';

$replace = [
self::ROOTURL_LIB => \fpcm\classes\dirs::getLibUrl(''),
self::ROOTURL_CORE_JS => \fpcm\classes\dirs::getCoreUrl(\fpcm\classes\dirs::CORE_JS, ''),
self::ROOTURL_CORE_THEME => \fpcm\classes\dirs::getCoreUrl(\fpcm\classes\dirs::CORE_THEME, ''),
];
$type = $this->getJsFileType($item, $jsCorePath);
if ($type === self::JS_FILETYP_FILE) {
$this->viewJsFilesLocal[] = $jsCorePath;
return $jsCorePath;
}

$checks[$hash] = str_replace(array_keys($replace), array_values($replace), $item);
$this->cache->write($cacheName, $checks);
return str_replace(array_keys($this->rootUrls), array_values($this->rootUrls), $item);
}

return $checks[$hash];
/**
* Checks path type of given JS file
* @param string $item
* @since FPCM 4.1
*/
private function getJsFileType(string $item, &$jsCorePath) : int
{
$item = trim($item);
if (!$item || substr($item, -3) !== '.js' || substr($item, 0, 4) === 'http' || substr($item, 0, 2) === '//') {
return self::JS_FILETYP_URL;
}

$jsCorePath = \fpcm\classes\dirs::getCoreDirPath(\fpcm\classes\dirs::CORE_JS, $item);
if (file_exists($jsCorePath)) {
return self::JS_FILETYP_FILE;
}

return self::JS_FILETYP_FILE_EXT;
}

/**
Expand Down Expand Up @@ -277,7 +310,7 @@ protected function prepareNotifications()
*/
public function addJsFiles(array $viewJsFiles)
{
$this->viewJsFiles = array_merge($this->viewJsFiles, array_map([$this, 'addRootPath'], $viewJsFiles));
$this->viewJsFiles = array_merge($this->viewJsFiles, $viewJsFiles);
}

/**
Expand All @@ -286,7 +319,7 @@ public function addJsFiles(array $viewJsFiles)
*/
public function addCssFiles(array $viewCssFiles)
{
$this->viewCssFiles = array_merge($this->viewCssFiles, array_map([$this, 'addRootPath'], $viewCssFiles));
$this->viewCssFiles = array_merge($this->viewCssFiles, $viewCssFiles);
}

/**
Expand Down Expand Up @@ -314,7 +347,7 @@ protected function mergeJsVars($jsVar, array $jsvars)
*/
public function overrideCssFiles(array $viewCssFiles)
{
$this->viewCssFiles = array_map([$this, 'addRootPath'], $viewCssFiles);
$this->viewCssFiles = viewCssFiles;
}

/**
Expand All @@ -323,7 +356,7 @@ public function overrideCssFiles(array $viewCssFiles)
*/
public function overrideJsFiles(array $viewJsFiles)
{
$this->viewJsFiles = array_map([$this, 'addRootPath'], $viewJsFiles);
$this->viewJsFiles = $viewJsFiles;
}

/**
Expand Down Expand Up @@ -513,7 +546,7 @@ public function showHeaderFooter($showHeader)
* @return bool
*/
public function render()
{
{
if (!file_exists($this->viewPath)) {
trigger_error("View file {$this->viewName} not found!");
exit("View file {$this->viewName} not found!");
Expand Down Expand Up @@ -588,10 +621,12 @@ protected function initAssigns()
$this->defaultViewVars->currentModule = \fpcm\classes\http::get('module');
$this->defaultViewVars->buttons = $this->buttons;
$this->defaultViewVars->formActionTarget = $this->formAction;

$this->defaultViewVars->lang = \fpcm\classes\loader::getObject('\fpcm\classes\language');
$this->defaultViewVars->filesCss = array_unique($this->viewCssFiles);
$this->defaultViewVars->filesJs = array_unique($this->viewJsFiles);

$this->defaultViewVars->filesCss = array_unique(array_map([$this, 'addRootPath'], $this->viewCssFiles));
$this->defaultViewVars->filesJs = array_unique(array_diff(array_map([$this, 'addRootPath'], $this->viewJsFiles), $this->viewJsFilesLocal));

$this->cache->write(self::JS_FILES_CACHE.$this->getViewHash(), array_unique($this->viewJsFilesLocal));

$this->defaultViewVars->fullWrapper = in_array($this->defaultViewVars->currentModule, ['installer']);
$this->defaultViewVars->showPageToken = $this->showPageToken;
Expand Down Expand Up @@ -640,6 +675,7 @@ public function setViewPath($viewName, $module = false)
: \fpcm\classes\dirs::getCoreDirPath(\fpcm\classes\dirs::CORE_VIEWS, $viewName);

$this->viewName = $viewName;
$this->viewHash = \fpcm\classes\tools::getHash($this->viewPath.uniqid());
}

/**
Expand Down Expand Up @@ -693,6 +729,16 @@ public function wasRendered()
return $this->rendered;
}

/**
* Returns Sha256-hash on view path
* @return string
* @since FPCM 4.1
*/
public function getViewHash() : string
{
return $this->viewHash;
}

/**
* Set form action path
* @param string $controller
Expand Down Expand Up @@ -815,11 +861,6 @@ private function initCssFiles()
return $this->viewCssFiles;
}

/**
* Gibt JS library zurück
* @return array
*/

/**
* Initialize default JavaScript files
* @return array
Expand All @@ -830,7 +871,11 @@ private function initJsFiles()
self::ROOTURL_LIB.'jquery/jquery-3.3.1.min.js',
self::ROOTURL_LIB.'jquery-ui/jquery-ui.min.js',
self::ROOTURL_LIB.'fancybox/jquery.fancybox.min.js',
self::ROOTURL_CORE_JS.'script.php'
self::ROOTURL_CORE_JS.'script.php?uq='.$this->getViewHash(),
'ajax.js',
'ui.js',
'notifications.js',
'system.js'
]);

return $this->viewJsFiles;
Expand Down

0 comments on commit d405ea5

Please sign in to comment.