diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 311361ee..eb2010cc 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -34,6 +34,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); + $this->app->singleton('option', \App\Services\Repositories\OptionRepository::class); } } diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index 83e3aa12..97b14e6c 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use App\Services\Repositories\OptionRepository; use App\Events\PluginWasDisabled; use App\Events\PluginWasEnabled; use App\Events\PluginWasUninstalled; diff --git a/app/Services/OptionRepository.php b/app/Services/Repositories/OptionRepository.php similarity index 55% rename from app/Services/OptionRepository.php rename to app/Services/Repositories/OptionRepository.php index 5f3d8ac2..e2acc5b0 100644 --- a/app/Services/OptionRepository.php +++ b/app/Services/Repositories/OptionRepository.php @@ -1,21 +1,17 @@ items, $key); - } - /** * Get the specified option value. * @@ -82,25 +67,10 @@ class OptionRepository implements ArrayAccess, ConfigContract } /** - * Set a given option value. + * Do really save modified options to database. * - * @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); @@ -134,31 +104,11 @@ class OptionRepository implements ArrayAccess, ConfigContract } /** - * 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 the options with key in the given array. * + * @param array $array * @return array */ - public function all() - { - return $this->items; - } - public function only(Array $array) { $result = []; @@ -172,51 +122,6 @@ class OptionRepository implements ArrayAccess, ConfigContract return $result; } - /** - * 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 */ diff --git a/app/Services/Repositories/Repository.php b/app/Services/Repositories/Repository.php new file mode 100644 index 00000000..46d41b15 --- /dev/null +++ b/app/Services/Repositories/Repository.php @@ -0,0 +1,158 @@ +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 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->items_modified[] = $innerKey; + } + } else { + Arr::set($this->items, $key, $value); + $this->items_modified[] = $key; + } + } + + /** + * Push an item into the repository. + * + * @param mixed $item + * @return void + */ + public function push($item) + { + array_push($this->items, $item); + } + + /** + * Get all of the items stored in the repository. + * + * @return array + */ + public function all() + { + return $this->items; + } + + /** + * Get an item from the repository, or store the default value. + * + * @param string $key + * @param \Closure $callback + * @return mixed + */ + public function remember($key, Closure $callback) + { + // If the item exists in the repository we will just return this immediately + // otherwise we will execute the given Closure and repository the result + // of that execution for the given number of minutes in storage. + if (! is_null($value = $this->get($key))) { + return $value; + } + + $this->put($key, $value = $callback()); + + return $value; + } + + /** + * Remove an item from the repository. + * + * @param string $key + * @return bool + */ + public function forget($key) + { + Arr::forget($this->items, $key); + } + + /** + * 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->forget($key); + } +}