deprecate Option::save()

This commit is contained in:
printempw 2016-12-30 19:35:07 +08:00
parent 098d0ed7b8
commit 59d7a9c4c2
4 changed files with 51 additions and 7 deletions

View File

@ -27,6 +27,7 @@ class SaveOptionRepository
*/ */
public function terminate($request, $response) public function terminate($request, $response)
{ {
app('options')->save(); // deprecated
// app('options')->save();
} }
} }

View File

@ -248,7 +248,7 @@ class OptionForm
protected function getValueById($id) protected function getValueById($id)
{ {
if (false === ($result = $this->parseIdWithOffset($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 { } else {
$option = Arr::get( $option = Arr::get(
$this->values, $this->values,
@ -268,10 +268,6 @@ class OptionForm
*/ */
protected function assignValues() protected function assignValues()
{ {
if (!is_null($this->alwaysCallback)) {
call_user_func($this->alwaysCallback, $this);
}
// load values for items if not set manually // load values for items if not set manually
foreach ($this->items as $item) { foreach ($this->items as $item) {
if ($item instanceof OptionFormGroup) { if ($item instanceof OptionFormGroup) {
@ -310,6 +306,10 @@ class OptionForm
*/ */
public function render() public function render()
{ {
if (!is_null($this->alwaysCallback)) {
call_user_func($this->alwaysCallback, $this);
}
$this->assignValues(); $this->assignValues();
return view('vendor.option-form.main')->with(array_merge(get_object_vars($this)))->render(); return view('vendor.option-form.main')->with(array_merge(get_object_vars($this)))->render();

View File

@ -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. * Do really save modified options to database.
* *
* @return void * @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() public function save()
{ {
$this->itemsModified = array_unique($this->itemsModified); $this->itemsModified = array_unique($this->itemsModified);

View File

@ -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 array|string $key
* @param mixed $value * @param mixed $value