Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/SettingsTrait.php
<?php | <?php | ||||
namespace App\Traits; | namespace App\Traits; | ||||
use Illuminate\Support\Facades\Cache; | |||||
trait SettingsTrait | trait SettingsTrait | ||||
{ | { | ||||
/** | /** | ||||
* Obtain the value for a setting. | * Obtain the value for a setting. | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | * $user = User::firstOrCreate(['email' => 'some@other.erg']); | ||||
* $locale = $user->getSetting('locale'); | * $locale = $user->getSetting('locale'); | ||||
* ``` | * ``` | ||||
* | * | ||||
* @param string $key Setting name | * @param string $key Setting name | ||||
* @param mixed $default Default value, to be used if not found | |||||
* | * | ||||
* @return string|null Setting value | * @return string|null Setting value | ||||
*/ | */ | ||||
public function getSetting(string $key) | public function getSetting(string $key, $default = null) | ||||
{ | |||||
$setting = $this->settings()->where('key', $key)->first(); | |||||
return $setting ? $setting->value : $default; | |||||
} | |||||
/** | |||||
* Obtain the values for many settings in one go (for better performance). | |||||
* | |||||
* @param array $keys Setting names | |||||
* | |||||
* @return array Setting key=value hash, includes also requested but non-existing settings | |||||
*/ | |||||
public function getSettings(array $keys): array | |||||
{ | { | ||||
$settings = $this->getCache(); | $settings = []; | ||||
if (!array_key_exists($key, $settings)) { | foreach ($keys as $key) { | ||||
return null; | $settings[$key] = null; | ||||
} | } | ||||
$value = $settings[$key]; | $this->settings()->whereIn('key', $keys)->get() | ||||
->each(function ($setting) use (&$settings) { | |||||
$settings[$setting->key] = $setting->value; | |||||
}); | |||||
return empty($value) ? null : $value; | return $settings; | ||||
} | } | ||||
/** | /** | ||||
* Remove a setting. | * Remove a setting. | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
Show All 23 Lines | trait SettingsTrait | ||||
* @param string $key Setting name | * @param string $key Setting name | ||||
* @param string|null $value The new value for the setting. | * @param string|null $value The new value for the setting. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function setSetting(string $key, $value): void | public function setSetting(string $key, $value): void | ||||
{ | { | ||||
$this->storeSetting($key, $value); | $this->storeSetting($key, $value); | ||||
$this->setCache(); | |||||
} | } | ||||
/** | /** | ||||
* Create or update multiple settings in one fell swoop. | * Create or update multiple settings in one fell swoop. | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | * $user = User::firstOrCreate(['email' => 'some@other.erg']); | ||||
* $user->setSettings(['locale', 'en', 'country' => 'GB']); | * $user->setSettings(['locale', 'en', 'country' => 'GB']); | ||||
* ``` | * ``` | ||||
* | * | ||||
* @param array $data An associative array of key value pairs. | * @param array $data An associative array of key value pairs. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function setSettings(array $data = []): void | public function setSettings(array $data = []): void | ||||
{ | { | ||||
foreach ($data as $key => $value) { | foreach ($data as $key => $value) { | ||||
$this->storeSetting($key, $value); | $this->storeSetting($key, $value); | ||||
} | } | ||||
$this->setCache(); | |||||
} | } | ||||
/** | |||||
* Create or update a setting. | |||||
* | |||||
* @param string $key Setting name | |||||
* @param string|null $value The new value for the setting. | |||||
* | |||||
* @return void | |||||
*/ | |||||
private function storeSetting(string $key, $value): void | private function storeSetting(string $key, $value): void | ||||
{ | { | ||||
if ($value === null || $value === '') { | if ($value === null || $value === '') { | ||||
// Note: We're selecting the record first, so observers can act | // Note: We're selecting the record first, so observers can act | ||||
if ($setting = $this->settings()->where('key', $key)->first()) { | if ($setting = $this->settings()->where('key', $key)->first()) { | ||||
$setting->delete(); | $setting->delete(); | ||||
} | } | ||||
} else { | } else { | ||||
$this->settings()->updateOrCreate( | $this->settings()->updateOrCreate( | ||||
['key' => $key], | ['key' => $key], | ||||
['value' => $value] | ['value' => $value] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
private function getCache() | |||||
{ | |||||
$model = \strtolower(get_class($this)); | |||||
if (Cache::has("{$model}_settings_{$this->id}")) { | |||||
return Cache::get("{$model}_settings_{$this->id}"); | |||||
} | |||||
return $this->setCache(); | |||||
} | |||||
private function setCache() | |||||
{ | |||||
$model = \strtolower(get_class($this)); | |||||
if (Cache::has("{$model}_settings_{$this->id}")) { | |||||
Cache::forget("{$model}_settings_{$this->id}"); | |||||
} | |||||
$cached = []; | |||||
foreach ($this->settings()->get() as $entry) { | |||||
if ($entry->value !== null && $entry->value !== '') { | |||||
$cached[$entry->key] = $entry->value; | |||||
} | |||||
} | |||||
Cache::forever("{$model}_settings_{$this->id}", $cached); | |||||
return $this->getCache(); | |||||
} | |||||
} | } |