mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2024-12-21 06:19:38 +08:00
77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use DB;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Database\QueryException;
|
|
|
|
class Option
|
|
{
|
|
protected $items;
|
|
|
|
public function __construct()
|
|
{
|
|
try {
|
|
$this->items = DB::table('options')->get()->mapWithKeys(function ($item) {
|
|
return [$item->option_name => $item->option_value];
|
|
});
|
|
} catch (QueryException $e) {
|
|
$this->items = collect();
|
|
}
|
|
}
|
|
|
|
public function get($key, $default = null, $raw = false)
|
|
{
|
|
if (! $this->items->has($key) && Arr::has(config('options'), $key)) {
|
|
$this->set($key, config("options.$key"));
|
|
}
|
|
|
|
$value = $this->items->get($key, $default);
|
|
if ($raw) {
|
|
return $value;
|
|
}
|
|
|
|
switch (strtolower($value)) {
|
|
case 'true':
|
|
case '(true)':
|
|
return true;
|
|
|
|
case 'false':
|
|
case '(false)':
|
|
return false;
|
|
|
|
case 'null':
|
|
case '(null)':
|
|
return null;
|
|
|
|
default:
|
|
return $value;
|
|
}
|
|
}
|
|
|
|
public function set($key, $value = null)
|
|
{
|
|
if (is_array($key)) {
|
|
foreach ($key as $k => $v) {
|
|
$this->set($k, $v);
|
|
}
|
|
} else {
|
|
$this->items->put($key, $value);
|
|
try {
|
|
DB::table('options')->updateOrInsert(
|
|
['option_name' => $key],
|
|
['option_value' => $value]
|
|
);
|
|
} catch (QueryException $e) {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
|
|
public function has($key)
|
|
{
|
|
return $this->items->has($key);
|
|
}
|
|
}
|