diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e622113e..359bddd3 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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); } diff --git a/app/Services/Option.php b/app/Services/Option.php new file mode 100644 index 00000000..63466892 --- /dev/null +++ b/app/Services/Option.php @@ -0,0 +1,76 @@ +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); + } +} diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index 8a955aa6..d82a05c0 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -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)) { diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index 320f960e..b43d53a4 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -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(); } /** diff --git a/app/Services/Repositories/OptionRepository.php b/app/Services/Repositories/OptionRepository.php deleted file mode 100644 index 87ced8ff..00000000 --- a/app/Services/Repositories/OptionRepository.php +++ /dev/null @@ -1,148 +0,0 @@ -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(); - } -} diff --git a/app/Services/Repositories/Repository.php b/app/Services/Repositories/Repository.php deleted file mode 100644 index 9b0d3f4d..00000000 --- a/app/Services/Repositories/Repository.php +++ /dev/null @@ -1,66 +0,0 @@ -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; - } - } -} diff --git a/app/helpers.php b/app/helpers.php index 60388f6a..9b976e7d 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -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; } diff --git a/database/migrations/2016_11_18_134542_import_options.php b/database/migrations/2016_11_18_134542_import_options.php index 76550881..71ec4c48 100644 --- a/database/migrations/2016_11_18_134542_import_options.php +++ b/database/migrations/2016_11_18_134542_import_options.php @@ -31,8 +31,6 @@ class ImportOptions extends Migration foreach ($options as $key => $value) { Option::set($key, $value); } - - Option::save(); } /** diff --git a/tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php b/tests/ServicesTest/OptionTest.php similarity index 75% rename from tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php rename to tests/ServicesTest/OptionTest.php index d084a33d..1361a4d8 100644 --- a/tests/ServicesTest/RepositoriesTest/OptionRepositoryTest.php +++ b/tests/ServicesTest/OptionTest.php @@ -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', diff --git a/tests/ServicesTest/RepositoriesTest/RepositoryTest.php b/tests/ServicesTest/RepositoriesTest/RepositoryTest.php deleted file mode 100644 index dc9ff936..00000000 --- a/tests/ServicesTest/RepositoriesTest/RepositoryTest.php +++ /dev/null @@ -1,38 +0,0 @@ -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')); - } -}