padosoft / laravel-settings
Persistent settings with caching in Laravel.
Installs: 6 448
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 4
Forks: 1
Open Issues: 4
Requires
- php: >=7.4
- illuminate/contracts: >=5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/database: >=5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/encryption: >=5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/validation: >=5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- josiasmontag/laravel-redis-mock: ^1.3
- mockery/mockery: ^1.5
- orchestra/testbench: ^3.8.2|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- roave/security-advisories: dev-latest
- dev-master
- 6.3.0
- 6.2.2
- 6.2.1
- 6.2.0
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.0
- 5.5.4
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.5
- 5.4.3
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.0
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 2.0.1
- 2.0.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.7
- 1.0.6
- dev-dependabot/composer/league/commonmark-2.6.0
- dev-dependabot/composer/laravel/framework-11.31.0
- dev-dependabot/composer/symfony/process-7.1.7
- dev-dependabot/composer/symfony/http-foundation-7.1.7
- dev-develop
- dev-feature/recognizeForMigration
- dev-release/5.0.6
- dev-feature/mixValidationRules
- dev-release/5.0.5
- dev-feature/recalculateOldValidationRules/Rebase
- dev-feature/migrationOldData
- dev-feature/dynamicCast
- dev-feature/validation
- dev-circleci-project-setup
This package is auto-updated.
Last update: 2024-12-10 00:46:55 UTC
README
Persistent on database, fast in memory, application-wide settings for Laravel.
Performance are not invalidated because settings are automatic cached when retrived from database. Da completare docs.
Requirements
PHP >= 7.1.3
Laravel 5.8.*|6.*|7.*|8.*|9.*|10.* (For Laravel framework 5.6.* or 5.7.* please use v1.*)
Installation
composer require padosoft/laravel-settings
- Publish the config and migration files by running
php artisan vendor:publish --provider="Padosoft\Laravel\Settings\ServiceProvider"
. - Run
php artisan migrate
Before running the migrations be sure you have in your AppServiceProviders.php
the following lines:
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }
Installation - Laravel < 5.5
- Add
Padosoft\Laravel\Settings\ServiceProvider
to the array of providers inconfig/app.php
. - Add
'SettingsManager' => 'Padosoft\Laravel\Settings\Facade'
to the array of aliases inconfig/app.php
.
Usage
You can either access the setting store via its facade or inject it by type-hinting towards the abstract class anlutro\LaravelSettings\SettingStore
.
<?php SettingsManager::get('foo', 'default value'); SettingsManager::get('foo', 'default value',false,true); //Get cast value without validation SettingsManager::get('foo', 'default value',false,false); //Get raw value SettingsManager::get('foo', 'default value',true,false); //Get validated value without cast SettingsManager::set('set', 'value');//valid for current session SettingsManager::set('set', 'value','validationRule');//with validation rule valid for current session SettingsManager::store();//persist settings value on db SettingsManager::setAndStore('set', 'value','validationRule');//persisted on database ?>
Call Setting::store()
explicitly to save changes made.
You could also use the setting()
helper:
// Get the store instance settings(); // Get values settings('foo'); settings('foo', 'default value',true,false);//Get cast value without validation settingsRaw('foo', 'default value');//Get raw value settingsAsString('foo', 'default value');//Get value as a String Validated settings('foo', 'default value',false,true);//Get raw value settings('foo', 'default value',false,false);//Get validated value without cast settings('foo', 'default value',); settings()->get('foo');
Using in other packages
If you want to use settings manager on other packages you must provide migrations to populate settings table. For Example:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\DB; class PopulateSettings extends Migration { /** * Run the migrations. * * @return void */ public function up() { if (config('padosoft-settings.enabled',false)) { DB::table('settings')->insert([ //LOGIN ['key'=>'login.remember_me', 'value'=>'1','descr'=>'Enable/Disable remeber me feature','config_override'=>'padosoft-users.login.remember-me','validation_rules'=>'boolean','editable'=>1,'load_on_startup'=>0], ['key'=>'login.login_reset_token_lifetime', 'value'=>'30','descr'=>'Number of minutes reset token lasts','config_override'=>'auth.expire','validation_rules'=>'numeric','editable'=>1,'load_on_startup'=>0], ]); } } /** * Reverse the migrations. * * @return void */ public function down() { } }
Please take care of populate config_override column with config key you want your setting should override ##Add new type of settings with cast and Validation in Config/config.php
/* |-------------------------------------------------------------------------- | Larvel Settings Manager |-------------------------------------------------------------------------- | | This option controls if the settings manager is enabled. | This option should not be is overwritten here but using settings db table | | | | */ 'enabled' => true, 'encrypted_keys' => [], 'cast' => [ //Example new cast and validation //class = class for cast //method = method for cast //validate = rule for validation 'boolean' => ['class' => \Padosoft\Laravel\Settings\CastSettings::class, 'method' => 'boolean', 'validate' => 'boolean'], 'listPipe' => ['class' => \App\Casts\ListPipeCast::class, 'validate' => 'regex:/(^[0-9|]+$)|(^.{0}$)/'], 'booleanString' => ['class' => \App\Casts\BooleanString::class,'validate' => 'regex:/^(true|false)/'], 'booleanInt' => ['class' => \App\Casts\BooleanInt::class,'validate' => 'regex:/^(0|1)/'], ],
Events Listening
Every time a model is created,updated or deleted an event will be dispatched. You can listen these events with a simple Listener in Laravel
public function subscribe(Dispatcher $events): void { $events->listen(\Padosoft\Laravel\Settings\Events\SettingCreated::class, [SettingsEventSubscriber::class, 'handleSettingCreated']); $events->listen(\Padosoft\Laravel\Settings\Events\SettingUpdated::class, [SettingsEventSubscriber::class, 'handleSettingUpdated']); $events->listen(\Padosoft\Laravel\Settings\Events\SettingDeleted::class, [SettingsEventSubscriber::class, 'handleSettingDeleted']); }
Contact
Open an issue on GitHub if you have any problems or suggestions.
License
The contents of this repository is released under the MIT license.