├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── Client.php ├── Entry.php ├── Facade.php ├── ServiceProvider.php └── migrations └── 2019_09_29_000000_create_kvstore_table.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Benjamin Gazé 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bgaze/laravel-kvstore 2 | 3 | A simple and easy to use key-value database store for Laravel 5.5+ 4 | 5 | All values are stored into database and managed using cache to avoid unecessary queries. 6 | 7 | Casting is supported to manage values type. 8 | 9 | ## Documentation 10 | 11 | Full documentation is available at [https://packages.bgaze.fr/laravel-kvstore](https://packages.bgaze.fr/laravel-kvstore) 12 | 13 | ## Quick start 14 | 15 | Install the package using composer: 16 | 17 | ``` 18 | composer install bgaze/laravel-kvstore 19 | ``` 20 | 21 | Publish the required migration: 22 | 23 | ``` 24 | php artisan vendor:publish --tag=kvstore 25 | ``` 26 | 27 | Then create the table: 28 | 29 | ``` 30 | php artisan migrate 31 | ``` 32 | 33 | The `KvStore` facade offers static methods to manage the store content. 34 | 35 | ```php 36 | // Insert some values: 37 | KvStore::set('value1', 'a string'); 38 | KvStore::set('value2', '11111', 'integer'); 39 | KvStore::set('value3', ['test' => true], 'array'); 40 | 41 | // Update value keeping cast type: 42 | KvStore::set('value3', ['test' => false]); 43 | 44 | // Update value changing cast type: 45 | KvStore::set('value3', 22222, 'integer'); 46 | 47 | // Update value removing cast type. 48 | KvStore::set('value3', 22222, false); 49 | 50 | // Get value by key: 51 | $value1 = KvStore::get('value1'); 52 | 53 | // Get value by key, passing a default value: 54 | $value2 = KvStore::get('value2', 'default value'); 55 | 56 | // Remove an entry by key: 57 | KvStore::remove('value1'); 58 | 59 | // Remove multiple entries by key: 60 | KvStore::remove(['value1', 'value2']); 61 | ``` 62 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bgaze/laravel-kvstore", 3 | "description": "A simple and easy to use key-value store for Laravel 5.5+", 4 | "require": { 5 | "laravel/framework": ">=5.5.0" 6 | }, 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Bgaze", 11 | "email": "benjamin@bgaze.fr" 12 | } 13 | ], 14 | "extra": { 15 | "laravel": { 16 | "providers": [ 17 | "Bgaze\\KvStore\\ServiceProvider" 18 | ], 19 | "aliases": { 20 | "KvStore": "Bgaze\\KvStore\\Facade" 21 | } 22 | } 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Bgaze\\KvStore\\": "src" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | $key]); 30 | 31 | if ($type === false) { 32 | $entry->type = null; 33 | } elseif (!empty($type)) { 34 | $entry->type = $type; 35 | } 36 | 37 | $entry->value = $value; 38 | 39 | $entry->save(); 40 | 41 | self::refresh(); 42 | } 43 | 44 | /** 45 | * Get a setting value by key. 46 | * 47 | * @param string $key 48 | * @param mixed $default 49 | * @return mixed 50 | */ 51 | public static function get($key, $default = null) { 52 | return self::all()->get($key, $default); 53 | } 54 | 55 | /** 56 | * Remove a setting by key. 57 | * 58 | * @param string|array $keys 59 | * @return void 60 | */ 61 | public static function remove($keys) { 62 | Store::destroy($keys); 63 | self::refresh(); 64 | } 65 | 66 | /** 67 | * Load store content from cache, creating it if needed. 68 | * 69 | * @return \Illuminate\Support\Collection 70 | */ 71 | public static function all() { 72 | return Cache::rememberForever(self::CACHE, function () { 73 | return Store::all()->pluck('value', 'key'); 74 | }); 75 | } 76 | 77 | /** 78 | * Force cache refresh. 79 | * 80 | * @return void 81 | */ 82 | public static function refresh() { 83 | Cache::forget(self::CACHE); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/Entry.php: -------------------------------------------------------------------------------- 1 | type) ? [] : ['value' => $this->type]; 63 | } 64 | 65 | /** 66 | * Set a given attribute on the model. 67 | * 68 | * @param string $key 69 | * @param mixed $value 70 | * @return mixed 71 | */ 72 | public function setAttribute($key, $value) { 73 | if ($this->isJsonCastable($key) && is_null($value)) { 74 | $value = []; 75 | } 76 | 77 | return parent::setAttribute($key, $value); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class ServiceProvider extends Base { 14 | 15 | /** 16 | * Bootstrap the application services. 17 | * 18 | * @return void 19 | */ 20 | public function boot() { 21 | // Publish migrations. 22 | $this->publishes([__DIR__ . '/migrations' => database_path('migrations')], 'kvstore'); 23 | 24 | // Register kvstore service. 25 | $this->app->bind('kvstore.client', Client::class); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/migrations/2019_09_29_000000_create_kvstore_table.php: -------------------------------------------------------------------------------- 1 | string('key')->unique()->primary(); 17 | $table->string('type')->nullable(); 18 | $table->text('value')->nullable(); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() { 28 | Schema::dropIfExists('kvstore'); 29 | } 30 | 31 | } 32 | --------------------------------------------------------------------------------