From cbc5bb70d02cd7a7f48605a1a3d3693c7e88d7be Mon Sep 17 00:00:00 2001 From: Molkobain Date: Sun, 25 Apr 2021 14:00:07 +0200 Subject: [PATCH] Performance: Change theme signature check to be done only when necessary at runtime in a production environment --- application/themehandler.class.inc.php | 56 ++++++++++++++++++++++++-- core/config.class.inc.php | 8 ++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/application/themehandler.class.inc.php b/application/themehandler.class.inc.php index ef3c4d24e..a168fdfb3 100644 --- a/application/themehandler.class.inc.php +++ b/application/themehandler.class.inc.php @@ -174,6 +174,29 @@ class ThemeHandler return static::GetCompiledThemesFolderAbsolutePath().$sThemeId.'/'; } + /** + * @param string $sThemeId + * + * @return string Absolute path of the compiled file for the $sThemeId theme (Note: It doesn't mean that the theme is actually compiled) + * @since 3.0.0 + */ + public static function GetCompiledThemeFileAbsolutePath(string $sThemeId): string + { + return static::GetCompiledThemeFolderAbsolutePath($sThemeId).'main.css'; + } + + /** + * @param string $sThemeId + * + * @return string Absolute URL of the compiled file for the $sThemeId theme (Note: It doesn't mean that the theme is actually compiled) + * @throws \Exception + * @since 3.0.0 + */ + public static function GetCompiledThemeFileAbsoluteUrl(string $sThemeId): string + { + return utils::GetAbsoluteUrlModulesRoot().'branding/themes/'.$sThemeId.'/main.css'; + } + /** * Return the absolute URL for the current theme CSS file * @@ -186,7 +209,9 @@ class ThemeHandler // Try to compile theme defined in the configuration // Note: In maintenance mode we should stick to the app theme (also we don't have access to many PHP classes, including the user preferences) $sThemeId = SetupUtils::IsInMaintenanceMode() ? static::GetApplicationThemeId() : static::GetCurrentUserThemeId(); - static::CompileTheme($sThemeId); + if (static::ShouldThemeSignatureCheckBeForced($sThemeId)) { + static::CompileTheme($sThemeId); + } } catch (CoreException $oCompileException) { // Fallback on our default theme (should always be compilable) in case the previous theme doesn't exists @@ -199,11 +224,34 @@ class ThemeHandler SetupUtils::builddir($sDefaultThemeDirPath); } - static::CompileTheme($sThemeId, false, "", $aDefaultTheme['parameters']); + if (static::ShouldThemeSignatureCheckBeForced($sThemeId)) { + static::CompileTheme($sThemeId, false, "", $aDefaultTheme['parameters']); + } } - // Return absolute url to theme compiled css - return utils::GetAbsoluteUrlModulesRoot().'branding/themes/'.$sThemeId.'/main.css'; + return static::GetCompiledThemeFileAbsoluteUrl($sThemeId); + } + + /** + * @param string $sThemeId + * + * @return bool True if the $sThemeId signature check -and possibly the compilation- should be forced (dev. environment, missing compiled file, ...) + */ + protected static function ShouldThemeSignatureCheckBeForced(string $sThemeId): bool + { + if (utils::IsDevelopmentEnvironment()) { + return true; + } + + if (false === file_exists(static::GetCompiledThemeFileAbsolutePath($sThemeId))) { + return true; + } + + if (true === utils::GetConfig()->Get('theme.force_signature_check_at_runtime')) { + return true; + } + + return false; } /** diff --git a/core/config.class.inc.php b/core/config.class.inc.php index 4806e5800..5da4a3660 100644 --- a/core/config.class.inc.php +++ b/core/config.class.inc.php @@ -1407,6 +1407,14 @@ class Config 'source_of_value' => '', 'show_in_conf_sample' => false, ], + 'theme.force_signature_check_at_runtime' => [ + 'type' => 'bool', + 'description' => 'If true, checking that the current theme signature matches the compiled file -to recompile it if necessary- will be done for each page. This can slow the application, only use it if you are experiencing issues while customizing a theme.)', + 'default' => false, + 'value' => false, + 'source_of_value' => '', + 'show_in_conf_sample' => false, + ], ]; public function IsProperty($sPropCode)