Rework Option

This commit is contained in:
Pig Fang 2019-03-23 15:44:16 +08:00
parent 2267a2cadb
commit e71e74cd5b
10 changed files with 87 additions and 272 deletions

View File

@ -11,7 +11,6 @@ use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;
use App\Exceptions\PrettyPageException;
use Illuminate\Support\ServiceProvider;
use App\Services\Repositories\OptionRepository;
class AppServiceProvider extends ServiceProvider
{
@ -65,7 +64,7 @@ class AppServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton('cipher', 'App\Services\Cipher\\'.config('secure.cipher'));
$this->app->singleton('options', OptionRepository::class);
$this->app->singleton('options', \App\Services\Option::class);
$this->app->singleton('parsedown', \Parsedown::class);
}

76
app/Services/Option.php Normal file
View File

@ -0,0 +1,76 @@
<?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);
}
}

View File

@ -275,7 +275,7 @@ class OptionForm
foreach ($postOptionQueue as $item) {
if ($item instanceof OptionFormCheckbox && ! isset($allPostData[$item->id])) {
// preset value for checkboxes which are not checked
$allPostData[$item->id] = 'false';
$allPostData[$item->id] = false;
}
// Str::is('*[*]', $item->id)
@ -295,7 +295,7 @@ class OptionForm
}
foreach ($arrayOptionQueue as $key => $value) {
Option::set($key, serialize($value));
Option::set($key, $value);
}
if (! is_null($this->hookAfter)) {

View File

@ -4,6 +4,7 @@ namespace App\Services;
use Storage;
use App\Events;
use App\Services\Option;
use Composer\Semver\Semver;
use Illuminate\Support\Arr;
use Composer\Semver\Comparator;
@ -11,7 +12,6 @@ use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use App\Exceptions\PrettyPageException;
use Illuminate\Contracts\Events\Dispatcher;
use App\Services\Repositories\OptionRepository;
use Illuminate\Contracts\Foundation\Application;
class PluginManager
@ -22,7 +22,7 @@ class PluginManager
protected $app;
/**
* @var OptionRepository
* @var Option
*/
protected $option;
@ -48,7 +48,7 @@ class PluginManager
public function __construct(
Application $app,
OptionRepository $option,
Option $option,
Dispatcher $dispatcher,
Filesystem $filesystem
) {
@ -309,9 +309,6 @@ class PluginManager
protected function saveEnabled()
{
$this->option->set('plugins_enabled', $this->enabled->values()->toJson());
// ensure to save options
$this->option->save();
}
/**

View File

@ -1,148 +0,0 @@
<?php
namespace App\Services\Repositories;
use DB;
use Illuminate\Support\Arr;
use Illuminate\Database\QueryException;
class OptionRepository extends Repository
{
protected $items = [];
/**
* Create a new option repository.
*
* @return void
*/
public function __construct()
{
try {
$options = DB::table('options')->get();
} catch (QueryException $e) {
$options = [];
}
foreach ($options as $option) {
$this->items[$option->option_name] = $option->option_value;
}
}
/**
* Get the specified option value.
*
* @param string $key
* @param mixed $default
* @param bool $raw Return raw value without convertion.
* @return mixed
*/
public function get($key, $default = null, $raw = false)
{
if (! $this->has($key) && Arr::has(config('options'), $key)) {
$this->set($key, config("options.$key"));
}
$value = Arr::get($this->items, $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;
default:
return $value;
break;
}
}
/**
* Set a given option value.
*
* @param array|string $key
* @param mixed $value
* @return void
*/
public function set($key, $value = null)
{
if (is_array($key)) {
// If given key is an array
foreach ($key as $innerKey => $innerValue) {
Arr::set($this->items, $innerKey, $innerValue);
$this->doSetOption($innerKey, $innerValue);
}
} else {
Arr::set($this->items, $key, $value);
$this->doSetOption($key, $value);
}
}
/**
* Do really save modified options to database.
*
* @return void
*/
protected function doSetOption($key, $value)
{
try {
if (! DB::table('options')->where('option_name', $key)->first()) {
DB::table('options')
->insert(['option_name' => $key, 'option_value' => $value]);
} else {
DB::table('options')
->where('option_name', $key)
->update(['option_value' => $value]);
}
} catch (QueryException $e) {
return;
}
}
/**
* Do really save modified options to database.
*
* @deprecated
* @return void
*/
public function save()
{
$this->itemsModified = array_unique($this->itemsModified);
try {
foreach ($this->itemsModified as $key) {
if (! DB::table('options')->where('option_name', $key)->first()) {
DB::table('options')
->insert(['option_name' => $key, 'option_value' => $this[$key]]);
} else {
DB::table('options')
->where('option_name', $key)
->update(['option_value' => $this[$key]]);
}
}
// Clear the list
$this->itemsModified = [];
} catch (QueryException $e) {
return;
}
}
/**
* Save all modified options into database.
*/
public function __destruct()
{
$this->save();
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace App\Services\Repositories;
use Illuminate\Support\Arr;
class Repository
{
/**
* All of the items.
*
* @var array
*/
protected $items = [];
/**
* All of the option items that is modified.
*
* @var array
*/
protected $itemsModified = [];
/**
* Determine if an item exists in the repository.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return Arr::has($this->items, $key);
}
/**
* Retrieve an item from the repository by key.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
return Arr::get($this->items, $key, $default);
}
/**
* Set a given item value.
*
* @param array|string $key
* @param mixed $value
* @return void
*/
public function set($key, $value = null)
{
if (is_array($key)) {
// If given key is an array
foreach ($key as $innerKey => $innerValue) {
Arr::set($this->items, $innerKey, $innerValue);
$this->itemsModified[] = $innerKey;
}
} else {
Arr::set($this->items, $key, $value);
$this->itemsModified[] = $key;
}
}
}

View File

@ -285,10 +285,7 @@ if (! function_exists('option')) {
}
if (is_array($key)) {
foreach ($key as $innerKey => $innerValue) {
$options->set($innerKey, $innerValue);
}
$options->set($key);
return;
}

View File

@ -31,8 +31,6 @@ class ImportOptions extends Migration
foreach ($options as $key => $value) {
Option::set($key, $value);
}
Option::save();
}
/**

View File

@ -2,16 +2,16 @@
namespace Tests;
use App\Services\Repositories\OptionRepository;
use App\Services\Option;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class OptionRepositoryTest extends TestCase
class OptionTest extends TestCase
{
use DatabaseTransactions;
public function testGet()
{
$repo = new OptionRepository();
$repo = new Option();
$repo->set('k1', '(null)');
$this->assertNull($repo->get('k1'));
$this->assertNull(option()->get('k1'));
@ -19,7 +19,7 @@ class OptionRepositoryTest extends TestCase
public function testSet()
{
$repo = new OptionRepository();
$repo = new Option();
$repo->set([
'k1' => 'v1',
'k2' => 'v2',

View File

@ -1,38 +0,0 @@
<?php
namespace Tests;
use App\Services\Repositories\Repository;
class RepositoryTest extends TestCase
{
public function testHas()
{
$repo = new Repository();
$repo->set('a', 'b');
$this->assertTrue($repo->has('a'));
$this->assertFalse($repo->has('b'));
}
public function testGet()
{
$repo = new Repository();
$repo->set('a', 'b');
$this->assertEquals('b', $repo->get('a'));
$this->assertNull($repo->get('b'));
}
public function testSet()
{
$repo = new Repository();
$repo->set('k1', 'v1');
$this->assertEquals('v1', $repo->get('k1'));
$repo->set([
'k2' => 'v2',
'k3' => 'v3',
]);
$this->assertEquals('v2', $repo->get('k2'));
$this->assertEquals('v3', $repo->get('k3'));
}
}