-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add a database settings support
- Loading branch information
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Zhiyi\Plus\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Setting extends Model | ||
{ | ||
/** | ||
* The table name. | ||
* @var string | ||
*/ | ||
protected $table = 'settings'; | ||
|
||
/** | ||
* Where by namespace scope. | ||
*/ | ||
public function scopeByNamespace($query, string $namespace) | ||
{ | ||
return $query->where('namespace', '$namespace'); | ||
} | ||
|
||
public function scopeByName($query, string $name) | ||
{ | ||
return $query->where('name', $name); | ||
} | ||
|
||
public function setContentsAttribute($contents) | ||
{ | ||
$this->attributes['contents'] = serialize($contents); | ||
|
||
return $this; | ||
} | ||
|
||
public function getContentsAttribute(string $contents) | ||
{ | ||
return unserialize($contents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\Support; | ||
|
||
use Zhiyi\Plus\Models\Setting as Model; | ||
|
||
class Setting | ||
{ | ||
/** | ||
* Storage database module. | ||
* @var \Zhiyi\Plus\Models\Setting | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* Storage namespace. | ||
* @var string | ||
*/ | ||
protected $namespace; | ||
|
||
/** | ||
* Create a setting namespace. | ||
* @param \Zhiyi\Plus\Models\Setting | ||
* @param string | ||
*/ | ||
public function __construct(Model $model, string $namespace) | ||
{ | ||
$this->model = $model; | ||
$this->namespace = $namespace; | ||
} | ||
|
||
/** | ||
* Create a new setting namespace. | ||
* @param string|null $namespace | ||
* @return self | ||
*/ | ||
public function new(?string $namespace = null): self | ||
{ | ||
return new static($this->model, $namespace ? $namespace : $this->namespace); | ||
} | ||
|
||
/** | ||
* Create new setting database builder. | ||
* @return mixed | ||
*/ | ||
public function query() | ||
{ | ||
return $this | ||
->model | ||
->query() | ||
->byNamespace($this->namespace); | ||
} | ||
|
||
/** | ||
* Get namespace settings or name contents. | ||
* @param string|null $name | ||
* @param any $default | ||
* @return any | ||
*/ | ||
public function get(?string $name = null, $default = null) | ||
{ | ||
if ($name) { | ||
$single = $this | ||
->query() | ||
->byName($name) | ||
->first(); | ||
return $single ? $single->contents : $default; | ||
} | ||
|
||
$collection = $this->query()->get(); | ||
|
||
return $collection->keyBy('name')->map(function ($value) { | ||
return $value; | ||
}); | ||
} | ||
|
||
/** | ||
* Set contents to namespace. | ||
* @param array|string $name | ||
* @param any $contents | ||
* @return void | ||
*/ | ||
public function set($name, $contents = null): void | ||
{ | ||
if (is_array($name)) { | ||
$callbale = [$this, __METHOD__]; | ||
$this->module->getConnection()->transaction(function () use ($name, $callbale) { | ||
foreach ($name as $name => $contents) { | ||
call_user_func($callbale, $name, $contents); | ||
} | ||
}); | ||
} | ||
|
||
$setting = $this->query()->byName($name)->first(); | ||
if (!$setting) { | ||
$setting = clone $this->model; | ||
$setting->namespace = $this->namespace; | ||
$setting->name = $name; | ||
} | ||
|
||
$setting->contents = $contents; | ||
$setting->save(); | ||
} | ||
|
||
/** | ||
* The static method create a setting namespace. | ||
* @param string $namespace | ||
* @return self | ||
*/ | ||
public static function create(string $namespace) | ||
{ | ||
return new static(new Model, $namespace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
database/migrations/2018_08_07_080904_create_settings_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateSettingsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('settings', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('namespace', 150)->comment('配置命名空间'); | ||
$table->string('name', 150)->comment('配置名称'); | ||
$table->text('contents')->nullable()->comment('配置数据'); | ||
$table->timestamps(); | ||
|
||
$table->index('namespace'); | ||
$table->index('name'); | ||
$table->unique(['namespace', 'name']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('settings'); | ||
} | ||
} |