Skip to content

Commit

Permalink
Remove duplicate database calls for AppSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bettag Fabian authored and Jigsaw5279 committed Jul 10, 2023
1 parent 6457e56 commit 4ebf226
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Services/Settings/SettingsGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SettingsGroup

private ?Closure $availableWhen = null;

private ?AppSetting $appSetting = null;

public static function make(): self
{
return new self();
Expand Down Expand Up @@ -70,12 +72,12 @@ public function hasSection(string $sectionName): bool

public function getSettingsModel(): AppSetting
{
$settingsModel = AppSetting::where(['name' => $this->getName()])->first();
$settingsModel = $this->appSetting ?? AppSetting::where(['name' => $this->getName()])->first();
if (!$settingsModel) {
$settingsModel = AppSetting::create(['name' => $this->getName()]);
}

return $settingsModel;
return $this->appSetting = $settingsModel;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/TwillAppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TwillAppSettings
*/
private array $settingsGroups = [];

private array $groupBlockCache = [];

public function registerSettingsGroup(SettingsGroup $section): void
{
$this->settingsGroups[$section->getName()] = $section;
Expand Down Expand Up @@ -103,12 +105,17 @@ public function getGroupForName(string $groupName): SettingsGroup

public function getGroupDataForSectionAndName(string $group, string $section): Block
{
$cacheKey = $group . $section;
if (array_key_exists($cacheKey, $this->groupBlockCache)) {
return $this->groupBlockCache[$cacheKey];
}

$groupObject = $this->getGroupForGroupAndSectionName($group, $section);

return $groupObject->getSettingsModel()->blocks()
->where('editor_name', $section)
->where('parent_id', null)
->firstOrFail();
return $this->groupBlockCache[$cacheKey] = $groupObject->getSettingsModel()->blocks()
->where('editor_name', $section)
->where('parent_id', null)
->firstOrFail();
}

public function getBlockServiceForGroupAndSection(string $group, string $section): BlockService
Expand Down

0 comments on commit 4ebf226

Please sign in to comment.