Skip to content

Commit

Permalink
allows default settings on get method
Browse files Browse the repository at this point in the history
  • Loading branch information
hackeresq committed Apr 23, 2021
1 parent bbad153 commit d17d230
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/Facades/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @method static object force(bool|null $force = true)
* @method static object tenant(string|null $tenant = null)
* @method static mixed get(mixed $key = null)
* @method static mixed get(mixed $key = null, mixed $default = null)
* @method static boolean has(string|array $needle)
* @method static boolean set(array $changes)
*
Expand Down
88 changes: 49 additions & 39 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Settings
* @param string $tenant (optional)
* @return Settings $this
*/
public function tenant(string $tenant='')
public function tenant(string $tenant = '')
{
$this->tenant = $tenant;

Expand All @@ -28,7 +28,7 @@ public function tenant(string $tenant='')
* @param bool $force (optional)
* @return Settings $this
*/
public function force(bool $force=true)
public function force(bool $force = true)
{
$this->force = $force;

Expand All @@ -39,19 +39,22 @@ public function force(bool $force=true)
* Get settings from the database
* @return array
*/
private function resolveDB()
private function resolveDB()
{
return DB::table(config('settings.table','settings'))->where('tenant',$this->tenant)->pluck('value', 'key')->toArray();
return DB::table(config('settings.table', 'settings'))
->where('tenant', $this->tenant)
->pluck('value', 'key')
->toArray();
}

/**
* Get settings from the cache
* @return array
*/
private function resolveCache()
private function resolveCache()
{
if (config('settings.cache',true)) {
return Cache::rememberForever('settings'.$this->tenant, function () {
if (config('settings.cache', true)) {
return Cache::rememberForever('settings' . $this->tenant, function () {
return $this->resolveDB();
});
}
Expand All @@ -64,11 +67,11 @@ private function resolveCache()
* @param array $settings
* @return array
*/
private function decryptHandler(array $settings)
private function decryptHandler(array $settings)
{
// DO WE NEED TO DECRYPT ANYTHING?
foreach ($settings as $key => $value) {
if ( in_array( $key, config('settings.encrypt',[]) ) && !empty($value) ) {
if (in_array($key, config('settings.encrypt', [])) && !empty($value)) {
Arr::set($settings, $key, decrypt($value));
}
}
Expand All @@ -80,11 +83,11 @@ private function decryptHandler(array $settings)
* @param array $settings
* @return array
*/
private function encryptHandler(array $settings)
private function encryptHandler(array $settings)
{
// DO WE NEED TO ENCRYPT ANYTHING?
foreach ($settings as $key => $value) {
if ( in_array($key, config('settings.encrypt',[]) ) && !empty($value)) {
if (in_array($key, config('settings.encrypt', [])) && !empty($value)) {
Arr::set($settings, $key, encrypt($value));
}
}
Expand All @@ -99,48 +102,55 @@ private function encryptHandler(array $settings)
*/
private function upsert(string $key, $value)
{
DB::table(config('settings.table','settings'))->updateOrInsert([
'key'=>$key,
'tenant'=>$this->tenant
],
[
'key'=>$key,
'value'=>$value,
'tenant'=>$this->tenant
]);
DB::table(config('settings.table', 'settings'))->updateOrInsert(
[
'key' => $key,
'tenant' => $this->tenant
],
[
'key' => $key,
'value' => $value,
'tenant' => $this->tenant
]
);
}

/**
* Get value of settings by key
* @param mixed $key (optional)
* @param string|array $key (optional)
* @param string|array $default (optional)
* @return mixed string
*/
public function get(mixed $key = NULL)
public function get(string|array $key = NULL, string|array $default = NULL)

This comment has been minimized.

Copy link
@dz-id

dz-id Jul 12, 2021

This syntax only working with php 8

{
$settings = $this->decryptHandler($this->resolveCache());

// no key passed, assuming get all settings
if ($key == NULL)
if (is_null($key)) {
// are we hiding everything?
return (config('settings.hidden',[]) == ['*'])
return (config('settings.hidden', []) == ['*'])
? [] // then return nothing.
: Arr::except($settings,config('settings.hidden',[])); // else, return everything else

: array_merge(
$default ?? [],
Arr::except($settings, config('settings.hidden', []))
);
}

// array of keys passed, return those settings only
if (is_array($key)) {
foreach ($key as $key) {
$result[$key] = $settings[$key] ?? null;
$result[$key] = $settings[$key] ?? $default[$key] ?? NULL;
}
return $result;
}

// single key passed, return that setting only
if (array_key_exists($key, $settings)) {

return $settings[$key];
}
return $settings[$key];
}

return;
return $default;
}

/**
Expand All @@ -153,7 +163,7 @@ public function has(mixed $needle)
$settings = $this->decryptHandler($this->resolveCache());

if (is_array($needle)) {
foreach($needle as $item) {
foreach ($needle as $item) {
if (!array_key_exists($item, $settings)) return false;
}
return true;
Expand All @@ -171,19 +181,19 @@ public function set(array $changes)
$changes = $this->encryptHandler($changes);

// Extracts only fillable key/values from array using fillable config
if (config('settings.fillable',[]) != ['*'] && !$this->force) {
$changes = Arr::only($changes, config('settings.fillable',[]));
}
if (config('settings.fillable', []) != ['*'] && !$this->force) {
$changes = Arr::only($changes, config('settings.fillable', []));
}

foreach ($changes as $key => $value) {
$this->upsert($key,$value);
$this->upsert($key, $value);
}

// reset cache
if (config('settings.cache',true)) {
Cache::forget('settings'.$this->tenant);
if (config('settings.cache', true)) {
Cache::forget('settings' . $this->tenant);
}

return true;
}
}
}
2 changes: 1 addition & 1 deletion src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function register () {

// bind 'settings' to the class named 'settings' in the IOC container
$this->app->singleton('settings', function ($app) {
return new \HackerESQ\Settings\Settings;
return new \HackerESQ\Settings\Settings ($app);
});

}
Expand Down
Loading

0 comments on commit d17d230

Please sign in to comment.