131 lines
2.5 KiB
PHP
131 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Config\Repositories;
|
|
|
|
use App\Core\Repository;
|
|
use App\Models\Config\Config as Model;
|
|
|
|
class ConfigRepository extends Repository
|
|
{
|
|
/**
|
|
* 是否关闭缓存
|
|
*
|
|
* @var boolean
|
|
*/
|
|
protected $cacheSkip = false;
|
|
|
|
/**
|
|
* 是否开启数据转化
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $needTransform = false;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fieldSearchable = [
|
|
'id' => '=',
|
|
'created_at' => 'like',
|
|
];
|
|
|
|
public function model()
|
|
{
|
|
return Model::class;
|
|
}
|
|
|
|
/**
|
|
* 数据格式化
|
|
*
|
|
* @param mixed $result
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function transform($model)
|
|
{
|
|
return $model->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get the specified configuration value.
|
|
*
|
|
* @param string $key
|
|
* @param mixed $default
|
|
* @return mixed
|
|
*/
|
|
public function configGet($key = null, $default = null)
|
|
{
|
|
$this->model = $this->model;
|
|
|
|
if (empty($key)) {
|
|
return $this->get()->pluck('value', 'key')->toArray();
|
|
}
|
|
|
|
list($name, $key) = $this->parse($key);
|
|
|
|
$this->model = $this->model->where('name', $name);
|
|
|
|
$config = $this->first();
|
|
|
|
if (!$config) {
|
|
return $default;
|
|
}
|
|
|
|
if (!$key) {
|
|
return $config->value;
|
|
}
|
|
|
|
return array_get($config->value, $key, $default);
|
|
}
|
|
|
|
/**
|
|
* Set a given configuration value.
|
|
*
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function configSet($key, $value)
|
|
{
|
|
list($name, $key) = $this->parse($key);
|
|
|
|
if ($key) {
|
|
$arr = $this->configGet($name);
|
|
array_set($arr, $key, $value);
|
|
$value = $arr;
|
|
}
|
|
|
|
if ($model = $this->model->where('name', $name)->first()) {
|
|
$this->setModel($model)->update(['value' => $value]);
|
|
} else {
|
|
$this->create([
|
|
'name' => $name,
|
|
'value' => $value
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 解析key值 key可由.分隔
|
|
*
|
|
* @param string $key
|
|
* @return array
|
|
*/
|
|
protected function parse($key)
|
|
{
|
|
$keys = str_to_array($key, '.');
|
|
$name = $keys[0];
|
|
unset($keys[0]);
|
|
|
|
if (empty($keys)) {
|
|
$keys = null;
|
|
} else {
|
|
$keys = implode($keys, '.');
|
|
}
|
|
|
|
return [$name, $keys];
|
|
}
|
|
}
|