Enh: Improved theme parent lookup performance

This commit is contained in:
Lucas Bartholemy 2018-09-16 19:25:08 +02:00
parent 685dc4f845
commit 12fc576d83
2 changed files with 65 additions and 5 deletions

View File

@ -6,6 +6,7 @@ HumHub Change Log
- Fix: img overflow in markdown view
- Enh: Added console command to list and change themes
- Enh: Improved theme parent lookup performance
1.3.2 (September 4, 2018)

View File

@ -99,13 +99,15 @@ class Theme extends BaseTheme
/**
* Registers theme css and resources to the view
* @param bool $includeParents also register parent themes
*/
public function register()
public function register($includeParents = true)
{
// Register parent themes first
foreach (array_reverse($this->getParents()) as $parent) {
/** @var Theme $parent */
$parent->register();
if ($includeParents) {
foreach (array_reverse($this->getParents()) as $parent) {
/** @var Theme $parent */
$parent->register(false);
}
}
if (file_exists($this->getBasePath() . '/css/theme.css')) {
@ -115,6 +117,7 @@ class Theme extends BaseTheme
}
/**
* Activate this theme
*/
@ -123,6 +126,17 @@ class Theme extends BaseTheme
$this->publishResources(true);
$this->variables->flushCache();
Yii::$app->settings->set('theme', $this->getBasePath());
Yii::$app->settings->delete('themeParents');
}
/**
* Checks whether the Theme is currently active.
*
* @return boolean
*/
public function isActive()
{
return ($this->name === Yii::$app->view->theme->name);
}
/**
@ -202,12 +216,57 @@ class Theme extends BaseTheme
*/
public function getParents()
{
if ($this->parents !== null) {
return $this->parents;
}
if ($this->isActive()) {
$this->parents = static::getActiveParents();
}
if ($this->parents === null) {
$this->parents = ThemeHelper::getThemeTree($this, false);
if ($this->isActive()) {
// Store parent path of currently active theme as settings
// This avoids theme paths lookups
$parentPaths = [];
foreach ($this->parents as $theme) {
$parentPaths[] = $theme->getBasePath();
}
Yii::$app->settings->setSerialized('themeParents', $parentPaths);
}
}
return $this->parents;
}
/**
* Returns the parent themes of the currently active theme.
* These parents are stored in the setting variable "themeParents" for faster lookup.
*
* @return Theme[]|null the themes or null
*/
protected static function getActiveParents()
{
$parentPaths = Yii::$app->settings->getSerialized('themeParents');
if (!is_array($parentPaths)) {
return null;
}
$parents = [];
foreach ($parentPaths as $parentPath) {
$theme = ThemeHelper::getThemeByPath($parentPath);
if ($theme === null) {
Yii::$app->settings->delete('themeParents');
Yii::error('Could not load stored theme parent! - Deleted parent path.', 'ui');
return null;
}
$parents[] = $theme;
}
return $parents;
}
}