From 59d7a9c4c26222da0ec9cd1c9db3fc36348673b2 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 30 Dec 2016 19:35:07 +0800 Subject: [PATCH] deprecate Option::save() --- app/Http/Middleware/SaveOptionRepository.php | 3 +- app/Services/OptionForm.php | 10 ++--- .../Repositories/OptionRepository.php | 43 +++++++++++++++++++ app/Services/Repositories/Repository.php | 2 +- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/app/Http/Middleware/SaveOptionRepository.php b/app/Http/Middleware/SaveOptionRepository.php index 32da3e70..f1d70d17 100644 --- a/app/Http/Middleware/SaveOptionRepository.php +++ b/app/Http/Middleware/SaveOptionRepository.php @@ -27,6 +27,7 @@ class SaveOptionRepository */ public function terminate($request, $response) { - app('options')->save(); + // deprecated + // app('options')->save(); } } diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index 10f0aef8..2d3b138c 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -248,7 +248,7 @@ class OptionForm protected function getValueById($id) { if (false === ($result = $this->parseIdWithOffset($id))) { - return Arr::get($this->values, $id, option($id, null, false)); + return Arr::get($this->values, $id, option($id)); } else { $option = Arr::get( $this->values, @@ -268,10 +268,6 @@ class OptionForm */ protected function assignValues() { - if (!is_null($this->alwaysCallback)) { - call_user_func($this->alwaysCallback, $this); - } - // load values for items if not set manually foreach ($this->items as $item) { if ($item instanceof OptionFormGroup) { @@ -310,6 +306,10 @@ class OptionForm */ public function render() { + if (!is_null($this->alwaysCallback)) { + call_user_func($this->alwaysCallback, $this); + } + $this->assignValues(); return view('vendor.option-form.main')->with(array_merge(get_object_vars($this)))->render(); diff --git a/app/Services/Repositories/OptionRepository.php b/app/Services/Repositories/OptionRepository.php index efbe7225..623fb8d3 100644 --- a/app/Services/Repositories/OptionRepository.php +++ b/app/Services/Repositories/OptionRepository.php @@ -64,11 +64,54 @@ class OptionRepository extends Repository } } + /** + * 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); diff --git a/app/Services/Repositories/Repository.php b/app/Services/Repositories/Repository.php index e7f4b8bd..ff9d2531 100644 --- a/app/Services/Repositories/Repository.php +++ b/app/Services/Repositories/Repository.php @@ -45,7 +45,7 @@ class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository } /** - * Set a given option value. + * Set a given item value. * * @param array|string $key * @param mixed $value