From 22c26c782f8c483f9ba277f23ef005cfe3aff2f0 Mon Sep 17 00:00:00 2001 From: Combind Date: Mon, 29 Jul 2024 17:36:01 +0100 Subject: [PATCH] Initial release --- README.md | 72 ++++++++++++++----- composer.json | 3 +- config/option.php | 32 ++------- .../migrations/create_options_table.php.stub | 23 ------ src/Option.php | 29 +++++++- src/OptionServiceProvider.php | 3 +- src/helpers.php | 30 ++------ 7 files changed, 97 insertions(+), 95 deletions(-) delete mode 100644 database/migrations/create_options_table.php.stub diff --git a/README.md b/README.md index 45852b2..b9426b7 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,7 @@ [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/combindma/laravel-option/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/combindma/laravel-option/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) [![Total Downloads](https://img.shields.io/packagist/dt/combindma/laravel-option.svg?style=flat-square)](https://packagist.org/packages/combindma/laravel-option) -This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. - +This package uses [spatie valuestore](https://github.com/spatie/valuestore) under the hood. ## About Combind Agency [Combine Agency](https://combind.ma?utm_source=github&utm_medium=banner&utm_campaign=package_name) is a leading web development agency specializing in building innovative and high-performance web applications using modern technologies. Our experienced team of developers, designers, and project managers is dedicated to providing top-notch services tailored to the unique needs of our clients. @@ -22,13 +21,6 @@ You can install the package via composer: composer require combindma/laravel-option ``` -You can publish and run the migrations with: - -```bash -php artisan vendor:publish --tag="laravel-option-migrations" -php artisan migrate -``` - You can publish the config file with: ```bash @@ -39,20 +31,66 @@ This is the contents of the published config file: ```php return [ + /* + |-------------------------------------------------------------------------- + | Default disk + |-------------------------------------------------------------------------- + | + | This is the default disk that will be used to store the options file. + | + */ + + 'disk' => 'local', + /* + |-------------------------------------------------------------------------- + | Default filename + |-------------------------------------------------------------------------- + | + | This is the default filename for the options file. + | + */ + + 'filename' => 'options.json', ]; ``` -Optionally, you can publish the views using - -```bash -php artisan vendor:publish --tag="laravel-option-views" -``` - ## Usage +This package makes it easy to store and retrieve some loose values. Stored values are saved as a json file. + +It can be used like this: ```php -$option = new Combindma\Option(); -echo $option->echoPhrase('Hello, Combindma!'); +option()->put('key', 'value'); + +option()->get('key'); // Returns 'value' + +option()->has('key'); // Returns true + +// Specify a default value for when the specified key does not exist +option()->get('non existing key', 'default') // Returns 'default' + +option()->put('anotherKey', 'anotherValue'); + +// Put multiple items in one go +option()->put(['ringo' => 'drums', 'paul' => 'bass']); + +option()->all(); // Returns an array with all items + +option()->forget('key'); // Removes the item + +option()->flush(); // Empty the entire options + +option()->flushStartingWith('somekey'); // remove all items whose keys start with "somekey" + +option()->increment('number'); // option()->get('number') will return 1 +option()->increment('number'); // option()->get('number') will return 2 +option()->increment('number', 3); // option()->get('number') will return 5 + +// Option implements ArrayAccess +option()['key'] = 'value'; +option()['key']; // Returns 'value' +isset(option()['key']); // Return true +unset(option()['key']); // Equivalent to removing the value ``` ## Testing diff --git a/composer.json b/composer.json index 7fea628..5dfa801 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,9 @@ ], "require": { "php": "^8.3", + "illuminate/contracts": "^11.0", "spatie/laravel-package-tools": "^1.16", - "illuminate/contracts": "^11.0" + "spatie/valuestore": "^1.3" }, "require-dev": { "laravel/pint": "^1.0", diff --git a/config/option.php b/config/option.php index b88d499..56a9688 100644 --- a/config/option.php +++ b/config/option.php @@ -1,44 +1,24 @@ env('OPTION_DRIVER', 'file'), - + 'disk' => 'local', /* |-------------------------------------------------------------------------- - | Option Stores + | Default filename |-------------------------------------------------------------------------- | - | Here you may define all of the option "stores" for your application as - | well as their drivers. You may even define multiple stores for the - | same option driver to group types of items stored in your options. + | This is the default filename for the options file. | */ - 'stores' => [ - 'database' => [ - 'driver' => 'database', - 'table' => 'options', - 'connection' => null, - ], - - 'file' => [ - 'driver' => 'file', - 'path' => storage_path('framework/app/options'), - 'single_file' => true, - ], - ], + 'filename' => 'options.json', ]; diff --git a/database/migrations/create_options_table.php.stub b/database/migrations/create_options_table.php.stub deleted file mode 100644 index 6a53c3d..0000000 --- a/database/migrations/create_options_table.php.stub +++ /dev/null @@ -1,23 +0,0 @@ -id(); - $table->string('key')->unique(); - $table->longText('value')->nullable(); - $table->string('tag')->nullable(); - }); - } - - public function down(): void - { - Schema::drop('options'); - } -}; diff --git a/src/Option.php b/src/Option.php index 6c1f439..67569aa 100755 --- a/src/Option.php +++ b/src/Option.php @@ -2,4 +2,31 @@ namespace Combindma\Option; -class Option {} +use Illuminate\Support\Facades\Storage; +use Spatie\Valuestore\Valuestore; + +class Option +{ + public string $fileName; + + public string $disk; + + public function __construct() + { + $this->fileName = config('option.filename'); + $this->disk = config('option.disk'); + $this->init(); + } + + public function init(): void + { + if (! Storage::disk($this->disk)->exists($this->fileName)) { + Storage::disk($this->disk)->put($this->fileName, json_encode([])); + } + } + + public function make(): Valuestore + { + return Valuestore::make(Storage::disk($this->disk)->path($this->fileName)); + } +} diff --git a/src/OptionServiceProvider.php b/src/OptionServiceProvider.php index df333d9..2331dc5 100644 --- a/src/OptionServiceProvider.php +++ b/src/OptionServiceProvider.php @@ -11,7 +11,6 @@ public function configurePackage(Package $package): void { $package ->name('laravel-option') - ->hasConfigFile('option') - ->hasMigration('create_options_table'); + ->hasConfigFile('option'); } } diff --git a/src/helpers.php b/src/helpers.php index 5d8ae07..9698e70 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,31 +1,11 @@ get($key, $default); - } - - return app('armincms.option')->store(); - } -} +use Combindma\Option\Option; +use Spatie\Valuestore\Valuestore; -if (! function_exists('option_exists')) { - /** - * Check existance of option. - * - * @return bool - */ - function option_exists(string $key) +if (! function_exists('option')) { + function option(): Valuestore { - return option()->has($key); + return (new Option)->make(); } }