Skip to content

Commit bbdc28e

Browse files
committed
Update lazy setting configuration and methods
The commit includes the update of the 'cache_key' to 'cache_prefix' in the lazys setting file. A new 'config' method has been introduced in LazySetting.php to fetch configurations. Furthermore, the logic of the 'getCacheKey' method has also been adjusted in line with these changes. Lastly, a 'getTable' method has been added to the Setting model to retrieve the table configuration.
1 parent 8c03392 commit bbdc28e

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

config/lazy/setting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// config for Step2Dev/LazySetting
44
return [
55
'table' => 'settings',
6-
'cache_key' => 'cache-settings',
6+
'cache_prefix' => 'lazy_',
77
'cache_ttl' => null, // in seconds (default: forever)
88
'default' => [
99
'group' => 'default',

src/LazySetting.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,28 @@ public function __construct()
1717
$this->settings = Collection::make();
1818
}
1919

20+
public static function config(string $string = ''): string|array|null
21+
{
22+
$config = [
23+
'table' => self::getTable(),
24+
'cache_key' => self::getCacheKey(),
25+
'cache_ttl' => self::getCacheTtl(),
26+
'default' => [
27+
'group' => self::getDefaultGroup(),
28+
'type' => self::getDefaultType(),
29+
],
30+
];
31+
32+
if ($string) {
33+
return $config[$string] ?? null;
34+
}
35+
36+
return $config;
37+
}
38+
2039
public static function getCacheKey(): string
2140
{
22-
return config('lazy-setting.cache_key', self::getCacheKey());
41+
return config('lazy-setting.cache_prefix').'settings';
2342
}
2443

2544
public static function getCacheTtl(): int
@@ -37,6 +56,11 @@ public static function getDefaultType(): string
3756
return config('lazy-setting.default.type', 'string');
3857
}
3958

59+
public static function getTable(): string
60+
{
61+
return trim((string) config('lazy-setting.table'));
62+
}
63+
4064
public function init(): static
4165
{
4266
if ($this->settings->isEmpty()) {
@@ -135,13 +159,13 @@ public function getFieldType(string $type = ''): string
135159

136160
return match ($type) {
137161
'string', 'text', 'textarea', 'richtext' => 'string',
138-
'integer', 'int' => 'integer',
139-
'float', 'double' => 'float',
140-
'boolean', 'bool' => 'boolean',
141-
'array' => 'array',
142-
'json' => 'json',
143-
'image' => 'image',
144-
default => self::getDefaultType(),
162+
'integer', 'int' => 'integer',
163+
'float', 'double' => 'float',
164+
'boolean', 'bool' => 'boolean',
165+
'array' => 'array',
166+
'json' => 'json',
167+
'image' => 'image',
168+
default => self::getDefaultType(),
145169
};
146170
}
147171

@@ -176,7 +200,7 @@ public function create(string $key, array $data, ?string $type = null): Setting
176200
{
177201
$setting = Setting::create([
178202
...$this->getKeyAndGroup($key),
179-
'type' => $type ?? 'string',
203+
'type' => $type ?? 'string',
180204
'value' => $data,
181205
]);
182206

src/Models/Setting.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Str;
8+
use Step2Dev\LazySetting\LazySetting;
79

810
/**
911
* @property string|null $group
@@ -45,4 +47,9 @@ public function getSettingKeyAttribute(): string
4547
{
4648
return $this->group.'.'.$this->key;
4749
}
50+
51+
public function getTable(): string
52+
{
53+
return LazySetting::config('table') ?: parent::getTable();
54+
}
4855
}

0 commit comments

Comments
 (0)