From ca775a280983086edb0d57bfe1acd3978100169b Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 28 Aug 2016 22:15:09 +0800 Subject: [PATCH] add options to service container --- app/Providers/AppServiceProvider.php | 1 + app/Services/{ => Facades}/Database.php | 2 +- app/Services/Facades/Option.php | 18 +++ app/Services/Option.php | 76 ---------- app/Services/OptionRepository.php | 185 ++++++++++++++++++++++++ config/app.php | 4 +- 6 files changed, 207 insertions(+), 79 deletions(-) rename app/Services/{ => Facades}/Database.php (89%) create mode 100644 app/Services/Facades/Option.php delete mode 100644 app/Services/Option.php create mode 100644 app/Services/OptionRepository.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 1d618803..07419136 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -24,5 +24,6 @@ class AppServiceProvider extends ServiceProvider public function register() { $this->app->singleton('database', \App\Services\Database\Database::class); + $this->app->singleton('option', \App\Services\OptionRepository::class); } } diff --git a/app/Services/Database.php b/app/Services/Facades/Database.php similarity index 89% rename from app/Services/Database.php rename to app/Services/Facades/Database.php index 1552a862..655f7908 100644 --- a/app/Services/Database.php +++ b/app/Services/Facades/Database.php @@ -1,6 +1,6 @@ first(); - - if (!$option) { - if (!is_null($default_value)) { - return $default_value; - } else { - $options = require BASE_DIR."/config/options.php"; - - if (array_key_exists($key, $options)) { - self::add($key, $options[$key]); - return $options[$key]; - } - throw new Exception('Unexistent option.', 1); - } - } - - return $option->option_value; - } - - public static function set($key, $value) - { - $option = OptionModel::where('option_name', $key)->first(); - - if (!$option) - throw new Exception('Unexistent option.', 1); - - $option->option_value = $value; - return $option->save(); - } - - public static function add($key, $value) - { - if (self::has($key)) - return true; - - $option = new OptionModel; - $option->option_name = $key; - $option->option_value = $value; - $option->save(); - } - - public static function has($key) - { - try { - OptionModel::where('option_name', $key)->firstOrFail(); - } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { - return false; - } - return true; - } - - public static function delete($key) - { - OptionModel::where('option_name', $key)->delete(); - } - -} - -class OptionModel extends Model -{ - protected $table = 'options'; - public $timestamps = false; - - protected $fillable = ['option_value']; -} diff --git a/app/Services/OptionRepository.php b/app/Services/OptionRepository.php new file mode 100644 index 00000000..db829723 --- /dev/null +++ b/app/Services/OptionRepository.php @@ -0,0 +1,185 @@ +get(); + + foreach ($options as $option) { + $this->items[$option->option_name] = $option->option_value; + } + + } + + /** + * Determine if the given option value exists. + * + * @param string $key + * @return bool + */ + public function has($key) + { + return Arr::has($this->items, $key); + } + + /** + * Get the specified option value. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function get($key, $default = null) + { + return Arr::get($this->items, $key, $default); + } + + /** + * Set a given option value. + * + * @param array|string $key + * @param mixed $value + * @return void + */ + public function set($key, $value = null) + { + if (is_array($key)) { + foreach ($key as $innerKey => $innerValue) { + Arr::set($this->items, $innerKey, $innerValue); + $this->items_modified[] = $innerKey; + } + } else { + Arr::set($this->items, $key, $value); + $this->items_modified[] = $key; + } + } + + protected function save() + { + $this->items_modified = array_unique($this->items_modified); + + foreach ($this->items_modified as $key) { + DB::table('options') + ->where('option_name', $key) + ->update(['option_value' => $this[$key]]); + } + } + + /** + * Prepend a value onto an array option value. + * + * @param string $key + * @param mixed $value + * @return void + */ + public function prepend($key, $value) + { + $array = $this->get($key); + + array_unshift($array, $value); + + $this->set($key, $array); + } + + /** + * Push a value onto an array option value. + * + * @param string $key + * @param mixed $value + * @return void + */ + public function push($key, $value) + { + $array = $this->get($key); + + $array[] = $value; + + $this->set($key, $array); + } + + /** + * Get all of the option items for the application. + * + * @return array + */ + public function all() + { + return $this->items; + } + + /** + * Determine if the given option option exists. + * + * @param string $key + * @return bool + */ + public function offsetExists($key) + { + return $this->has($key); + } + + /** + * Get a option option. + * + * @param string $key + * @return mixed + */ + public function offsetGet($key) + { + return $this->get($key); + } + + /** + * Set a option option. + * + * @param string $key + * @param mixed $value + * @return void + */ + public function offsetSet($key, $value) + { + $this->set($key, $value); + } + + /** + * Unset a option option. + * + * @param string $key + * @return void + */ + public function offsetUnset($key) + { + $this->set($key, null); + } + + /** + * Save all modified options into database + */ + public function __destruct() + { + $this->save(); + } + +} diff --git a/config/app.php b/config/app.php index 1d2a261b..fc6c8fab 100644 --- a/config/app.php +++ b/config/app.php @@ -215,12 +215,12 @@ return [ * Blessing Skin */ 'View' => App\Services\View::class, - 'Option' => App\Services\Option::class, + 'Option' => App\Services\Facades\Option::class, 'Utils' => App\Services\Utils::class, 'Minecraft' => App\Services\Minecraft::class, 'Validate' => App\Services\Validate::class, 'Updater' => App\Services\Updater::class, - 'Database' => App\Services\Database::class, + 'Database' => App\Services\Facades\Database::class, 'Http' => App\Services\Http::class ],