mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°2710 - Fix extremely slow page load for first user after setup (regression introduced in N°2314)
This commit is contained in:
@@ -259,7 +259,7 @@ EOF
|
||||
|
||||
protected function LoadTheme()
|
||||
{
|
||||
$sCssThemeUrl = ThemeHandler::GetTheme();
|
||||
$sCssThemeUrl = ThemeHandler::GetDefaultThemeUrl();
|
||||
$this->add_linked_stylesheet($sCssThemeUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,67 +27,102 @@ use ScssPhp\ScssPhp\Compiler;
|
||||
*/
|
||||
class ThemeHandler
|
||||
{
|
||||
|
||||
/**
|
||||
* Return the absolute URL for the compiled theme CSS file
|
||||
* Return the absolute URL for the default theme CSS file
|
||||
*
|
||||
* @return string
|
||||
* @throws \CoreException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function GetTheme()
|
||||
public static function GetDefaultThemeUrl()
|
||||
{
|
||||
$sThemeId = MetaModel::GetConfig()->Get('backoffice_default_theme');
|
||||
$sEnvPath = APPROOT.'env-'.utils::GetCurrentEnvironment().'/';
|
||||
$sThemePath = $sEnvPath.'/branding/themes/'.$sThemeId.'/';
|
||||
$aThemeParameters = json_decode(@file_get_contents($sThemePath.'theme-parameters.json'), true);
|
||||
$sThemeCssPath = $sThemePath.'main.css';
|
||||
static::CompileTheme($sThemeId);
|
||||
|
||||
// Check that theme is compiled
|
||||
if ($aThemeParameters === null)
|
||||
// Return absolute url to theme compiled css
|
||||
return utils::GetAbsoluteUrlModulesRoot().'/branding/themes/'.$sThemeId.'/main.css';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the $sThemeId theme
|
||||
*
|
||||
* @param string $sThemeId
|
||||
* @param array|null $aThemeParameters Parameters (variables, imports, stylesheets) for the theme, if not passed, will be retrieved from compiled DM
|
||||
* @param array|null $aImportsPaths Paths where imports can be found
|
||||
* @param string|null $sWorkingPath Path of the folder used during compilation
|
||||
*
|
||||
* @throws \CoreException
|
||||
*/
|
||||
public static function CompileTheme($sThemeId, $aThemeParameters = null, $aImportsPaths = null, $sWorkingPath = null)
|
||||
{
|
||||
// Default working path
|
||||
if($sWorkingPath === null)
|
||||
{
|
||||
throw new CoreException('Could not load "'.$sThemeId.'" theme parameters from file, check that it has been compiled correctly');
|
||||
$sWorkingPath = APPROOT.'env-'.utils::GetCurrentEnvironment().'/';
|
||||
}
|
||||
|
||||
$sTheme = '';
|
||||
// Default import paths (env-*)
|
||||
if($aImportsPaths === null)
|
||||
{
|
||||
$aImportsPaths = array(
|
||||
APPROOT.'env-'.utils::GetCurrentEnvironment().'/',
|
||||
);
|
||||
}
|
||||
|
||||
// Note: We do NOT check that the folder exists!
|
||||
$sThemeFolderPath = $sWorkingPath.'/branding/themes/'.$sThemeId.'/';
|
||||
$sThemeCssPath = $sThemeFolderPath.'main.css';
|
||||
|
||||
// Save parameters if passed...
|
||||
if(is_array($aThemeParameters))
|
||||
{
|
||||
file_put_contents($sThemeFolderPath.'/theme-parameters.json', json_encode($aThemeParameters));
|
||||
}
|
||||
// ... Otherwise, retrieve them from compiled DM
|
||||
else
|
||||
{
|
||||
$aThemeParameters = json_decode(@file_get_contents($sThemeFolderPath.'theme-parameters.json'), true);
|
||||
if ($aThemeParameters === null)
|
||||
{
|
||||
throw new CoreException('Could not load "'.$sThemeId.'" theme parameters from file, check that it has been compiled correctly');
|
||||
}
|
||||
}
|
||||
|
||||
$sTmpThemeScssContent = '';
|
||||
$iStyleLastModified = 0;
|
||||
clearstatcache();
|
||||
// Loading files to import and stylesheet to compile, also getting most recent modification time on overall files
|
||||
foreach ($aThemeParameters['imports'] as $sImport)
|
||||
{
|
||||
$sTheme .= '@import "'.$sImport.'";'."\n";
|
||||
$sTmpThemeScssContent .= '@import "'.$sImport.'";'."\n";
|
||||
|
||||
$iImportLastModified = filemtime($sEnvPath.$sImport);
|
||||
$iImportLastModified = filemtime($sWorkingPath.$sImport);
|
||||
$iStyleLastModified = $iStyleLastModified < $iImportLastModified ? $iImportLastModified : $iStyleLastModified;
|
||||
}
|
||||
foreach ($aThemeParameters['stylesheets'] as $sStylesheet)
|
||||
{
|
||||
$sTheme .= '@import "'.$sStylesheet.'";'."\n";
|
||||
$sTmpThemeScssContent .= '@import "'.$sStylesheet.'";'."\n";
|
||||
|
||||
$iStylesheetLastModified = filemtime($sEnvPath.$sStylesheet);
|
||||
$iStylesheetLastModified = filemtime($sWorkingPath.$sStylesheet);
|
||||
$iStyleLastModified = $iStyleLastModified < $iStylesheetLastModified ? $iStylesheetLastModified : $iStyleLastModified;
|
||||
}
|
||||
|
||||
// Checking if our compiled css is outdated
|
||||
if (!file_exists($sThemeCssPath) || (is_writable($sThemePath) && (filemtime($sThemeCssPath) < $iStyleLastModified)))
|
||||
if (!file_exists($sThemeCssPath) || (is_writable($sThemeFolderPath) && (filemtime($sThemeCssPath) < $iStyleLastModified)))
|
||||
{
|
||||
$oScss = new Compiler();
|
||||
$oScss->setFormatter('ScssPhp\\ScssPhp\\Formatter\\Expanded');
|
||||
// Setting our xml variables
|
||||
$oScss->setVariables($aThemeParameters['variables']);
|
||||
// Setting our import path to env-*
|
||||
$oScss->setImportPaths($sEnvPath);
|
||||
// Setting our imports paths
|
||||
$oScss->setImportPaths($aImportsPaths);
|
||||
// Temporary disabling max exec time while compiling
|
||||
$iCurrentMaxExecTime = (int)ini_get('max_execution_time');
|
||||
set_time_limit(0);
|
||||
// Compiling our theme
|
||||
$sThemeCss = $oScss->compile($sTheme);
|
||||
$sTmpThemeCssContent = $oScss->compile($sTmpThemeScssContent);
|
||||
set_time_limit($iCurrentMaxExecTime);
|
||||
file_put_contents($sThemePath.'main.css', $sThemeCss);
|
||||
file_put_contents($sThemeCssPath, $sTmpThemeCssContent);
|
||||
}
|
||||
|
||||
// Return absolute url to theme compiled css
|
||||
return utils::GetAbsoluteUrlModulesRoot().'/branding/themes/'.$sThemeId.'/main.css';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2676,6 +2676,14 @@ EOF;
|
||||
*/
|
||||
protected function CompileThemes($oBrandingNode, $sTempTargetDir, $sFinalTargetDir)
|
||||
{
|
||||
// Set imports paths
|
||||
// Note: During compilation, we don't have access to "env-xxx", so we have to set several imports paths:
|
||||
// - The CSS directory for the
|
||||
$aImportsPaths = array(
|
||||
APPROOT.'css/',
|
||||
$sTempTargetDir,
|
||||
);
|
||||
|
||||
// Build compiled themes folder
|
||||
$sThemesDir = $sTempTargetDir.'/branding/themes/';
|
||||
if(!is_dir($sThemesDir))
|
||||
@@ -2683,60 +2691,64 @@ EOF;
|
||||
SetupUtils::builddir($sThemesDir);
|
||||
}
|
||||
|
||||
// Parsing themes
|
||||
// Parsing themes from DM
|
||||
$aThemes = array();
|
||||
/** @var \DOMNodeList $oThemeNodes */
|
||||
$oThemeNodes = $oBrandingNode->GetNodes('themes/theme');
|
||||
foreach($oThemeNodes as $oTheme)
|
||||
{
|
||||
$sThemeId = $oTheme->getAttribute('id');
|
||||
$sThemeDir = $sThemesDir.$sThemeId;
|
||||
|
||||
if(!is_dir($sThemesDir.$sThemeId))
|
||||
{
|
||||
SetupUtils::builddir($sThemesDir.$sThemeId);
|
||||
}
|
||||
|
||||
$oVariables = $oTheme->GetNodes('variables/variable');
|
||||
$oImports = $oTheme->GetNodes('imports/import');
|
||||
$oStylesheets = $oTheme->GetNodes('stylesheets/stylesheet');
|
||||
|
||||
$aThemeParameters = array(
|
||||
'variables' => array(),
|
||||
'imports' => array(),
|
||||
'stylesheets' => array(),
|
||||
);
|
||||
// json array
|
||||
$sVariablesContent = '';
|
||||
|
||||
/** @var \DOMNodeList $oVariables */
|
||||
$oVariables = $oTheme->GetNodes('variables/variable');
|
||||
foreach($oVariables as $oVariable)
|
||||
{
|
||||
$sVariableId = $oVariable->getAttribute('id');
|
||||
$aThemeParameters['variables'][$sVariableId] = $oVariable->GetText();
|
||||
}
|
||||
|
||||
/** @var \DOMNodeList $oImports */
|
||||
$oImports = $oTheme->GetNodes('imports/import');
|
||||
foreach($oImports as $oImport)
|
||||
{
|
||||
$sImportId = $oImport->getAttribute('id');
|
||||
$aThemeParameters['imports'][$sImportId] = $oImport->GetText();
|
||||
}
|
||||
|
||||
|
||||
/** @var \DOMNodeList $oStylesheets */
|
||||
$oStylesheets = $oTheme->GetNodes('stylesheets/stylesheet');
|
||||
foreach($oStylesheets as $oStylesheet)
|
||||
{
|
||||
$sStylesheetId = $oStylesheet->getAttribute('id');
|
||||
$aThemeParameters['stylesheets'][$sStylesheetId] = $oStylesheet->GetText();
|
||||
}
|
||||
file_put_contents($sThemeDir.'/theme-parameters.json', json_encode($aThemeParameters));
|
||||
|
||||
$aThemes[$sThemeId] = $aThemeParameters;
|
||||
}
|
||||
|
||||
if($oThemeNodes->length === 0)
|
||||
// Force to have a default theme if none in the DM
|
||||
if(empty($aThemes))
|
||||
{
|
||||
$aDefaultThemeInfo = $this->GetDefaultThemeInformation();
|
||||
$sThemeDir = $sThemesDir.$aDefaultThemeInfo['name'];
|
||||
$aThemes[$aDefaultThemeInfo['name']] = $aDefaultThemeInfo['parameters'];
|
||||
}
|
||||
|
||||
// Compile themes
|
||||
foreach($aThemes as $sThemeId => $aThemeParameters)
|
||||
{
|
||||
$sThemeDir = $sThemesDir.$sThemeId;
|
||||
if(!is_dir($sThemeDir))
|
||||
{
|
||||
SetupUtils::builddir($sThemeDir);
|
||||
}
|
||||
file_put_contents($sThemeDir.'/theme-parameters.json', json_encode($aDefaultThemeInfo['parameters']));
|
||||
|
||||
|
||||
ThemeHandler::CompileTheme($sThemeId, $aThemeParameters, $aImportsPaths, $sTempTargetDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user