Skip to content

Commit

Permalink
feat(core): Add a database settings support
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Aug 7, 2018
1 parent 7355512 commit b7da0a1
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/Models/Setting.php
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);
}
}
132 changes: 132 additions & 0 deletions app/Support/Setting.php
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);
}
}
17 changes: 17 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,20 @@ function filterUrlStringLength(string $data, int $length = 0): string

return $value;
}

/**
* Setting helper.
*
* @param string $namespace
* @param string|null $name
* @return any
*/
function setting(string $namespace, ?string $name = null)
{
$setting = \Zhiyi\Plus\Support\Setting::create($namespace);
if ($name) {
$setting->get($name);
}

return $setting;
}
38 changes: 38 additions & 0 deletions database/migrations/2018_08_07_080904_create_settings_table.php
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');
}
}

0 comments on commit b7da0a1

Please sign in to comment.