Skip to content

Instantly share code, notes, and snippets.

@martianatwork
Created November 20, 2020 20:28
Show Gist options
  • Save martianatwork/0d872705a4450fadff1f45170d3f396c to your computer and use it in GitHub Desktop.
Save martianatwork/0d872705a4450fadff1f45170d3f396c to your computer and use it in GitHub Desktop.
bavix/laravel-settings Global Settings

I had a specific use case where my global key was different than the user key so i had to add globalKey. Also in order to distinglish between global and user setting i chosse to add is_global to my table but this can be implemented differently.

Another improvement that i will do is to implement the casting for value field using L8.

<?php
namespace App\Traits\Settings;
use App\Models\Setting;
use Bavix\Settings\Services\ReadableService;
trait HasGetSettings
{
/**
* @param string $key
* @param string|null $globalKey
* @param int|null $default
* @return int|null
*/
public function getSettingInt(string $key, ?string $globalKey = null, ?int $default = null): ?int
{
return app(ReadableService::class)
->getSettingInt($this, $key, $globalKey, $default);
}
/**
* @param string $key
* @param string|null $globalKey
* @param float|null $default
* @return float|null
*/
public function getSettingFloat(string $key, ?string $globalKey = null, ?float $default = null): ?float
{
return app(ReadableService::class)
->getSettingFloat($this, $key, $globalKey, $default);
}
/**
* @param string $key
* @param string|null $globalKey
* @param bool|null $default
* @return bool|null
*/
public function getSettingBool(string $key, ?string $globalKey = null, ?bool $default = null): ?bool
{
return app(ReadableService::class)
->getSettingBool($this, $key, $globalKey, $default);
}
/**
* @param string $key
* @param string|null $globalKey
* @param string|null $default
* @return string|null
*/
public function getSettingString(string $key, ?string $globalKey = null, ?string $default = null): ?string
{
return app(ReadableService::class)
->getSettingString($this, $key, $globalKey, $default);
}
/**
* @param string $key
* @param string|null $globalKey
* @param array|null $default
* @return array|null
*/
public function getSettingArray(string $key, ?string $globalKey = null, ?array $default = null): ?array
{
return app(ReadableService::class)
->getSettingArray($this, $key, $globalKey, $default);
}
/**
* @param string $key
* @param string|null $globalKey
* @return Setting|null
*/
public function getSetting(string $key, ?string $globalKey = null): ?Setting
{
return app(ReadableService::class)
->getSetting($this, $key, $globalKey);
}
}
<?php
namespace App\Services\Settings;
use Bavix\Settings\Interfaces\Settingable as Model;
use App\Models\Setting;
class ReadableService {
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @return Setting|null
*/
public function getSetting(Model $model, string $key, ?string $globalKey = null): ?Setting
{
$val = $model->settings->firstWhere('key', $key);
if(!empty($val)) {
return $val;
}
$val = $model->settings->firstWhere('key', $globalKey);
if(!empty($val)) {
return $val;
}
return Setting::where('is_global', true)->where('key', $globalKey ?? $key)->first();
}
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @param int|null $default
* @return int|null
*/
public function getSettingInt(Model $model, string $key, ?string $globalKey = null, ?int $default = null): ?int
{
return $this->getSetting($model, $key, $globalKey)->value ?? $default;
}
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @param float|null $default
* @return float|null
*/
public function getSettingFloat(Model $model, string $key, ?string $globalKey = null, ?float $default = null): ?float
{
return $this->getSetting($model, $key, $globalKey)->value ?? $default;
}
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @param bool|null $default
* @return bool|null
*/
public function getSettingBool(Model $model, string $key, ?string $globalKey = null, ?bool $default = null): ?bool
{
return $this->getSetting($model, $key, $globalKey)->value ?? $default;
}
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @param string|null $default
* @return string|null
*/
public function getSettingString(Model $model, string $key, ?string $globalKey = null, ?string $default = null): ?string
{
return $this->getSetting($model, $key, $globalKey)->value ?? $default;
}
/**
* @param Model $model
* @param string $key
* @param string|null $globalKey
* @param array|null $default
* @return array|null
*/
public function getSettingArray(Model $model, string $key, ?string $globalKey = null, ?array $default = null): ?array
{
return $this->getSetting($model, $key, $globalKey)->value ?? $default;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment