From e5e8ec2927a9d6b29d4d165e244c7624d8c3ba1f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Fri, 29 May 2020 12:28:42 -0500 Subject: [PATCH] Settings syncronization with config/settings.php ------------------------------------------------ Command settings:sync helps you keep production settings in sync with config/settings.php It doesn't override "value" field Package publishes config/settings.php --- README.md | 28 +++++----- src/SettingsServiceProvider.php | 9 ++++ src/app/Console/Commands/SettingsSync.php | 64 +++++++++++++++++++++++ src/config/settings.php | 37 +++++++++++++ 4 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 src/app/Console/Commands/SettingsSync.php create mode 100644 src/config/settings.php diff --git a/README.md b/README.md index 83ba0e6..ff4cc58 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,14 @@ composer require backpack/settings php artisan vendor:publish --provider="Backpack\Settings\SettingsServiceProvider" php artisan migrate +# change config/settings.php dummy settings + +# sync your settings to database +php artisan settings:sync + # [optional] add a menu item for it to the sidebar_content file php artisan backpack:add-sidebar-content "" -# [optional] insert some example dummy data to the database -php artisan db:seed --class="Backpack\Settings\database\seeds\SettingsTableSeeder" ``` ## Usage @@ -46,20 +49,17 @@ Setting::get('contact_email') Config::get('settings.contact_email') ``` -### Add new settings +### Change settings + +Available settings are stored in `config/settings.php` -Settings are stored in the database in the "settings" table. Its columns are: -- id (ex: 1) -- key (ex: contact_email) -- name (ex: Contact form email address) -- description (ex: The email address that all emails go to.) -- value (ex: admin@laravelbackpack.com) -- field (Backpack CRUD field configuration in JSON format. https://backpackforlaravel.com/docs/crud-fields#default-field-types) -- active (1 or 0) -- created_at -- updated_at +To syncronize with database simply run: + +``` +php artisan settings:sync +``` -There is no interface available to add new settings. They are added by the developer directly in the database, since the Backpack CRUD field configuration is a bit complicated. See the field types and their configuration code on https://backpackforlaravel.com/docs +Note: this command doesn't overwrite 'value' column in database ### Override existing configurations diff --git a/src/SettingsServiceProvider.php b/src/SettingsServiceProvider.php index aca4c9d..725e651 100644 --- a/src/SettingsServiceProvider.php +++ b/src/SettingsServiceProvider.php @@ -2,6 +2,7 @@ namespace Backpack\Settings; +use Backpack\Settings\app\Console\Commands\SettingsSync; use Backpack\Settings\app\Models\Setting as Setting; use Config; use Illuminate\Routing\Router; @@ -51,6 +52,14 @@ public function boot() // publish translation files $this->publishes([__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')], 'lang'); + + // publish config/settings.php + $this->publishes([__DIR__.'/config/settings.php' => config_path('settings.php')], 'settings.php'); + + // register commands + $this->commands([ + SettingsSync::class + ]); } /** diff --git a/src/app/Console/Commands/SettingsSync.php b/src/app/Console/Commands/SettingsSync.php new file mode 100644 index 0000000..a702f95 --- /dev/null +++ b/src/app/Console/Commands/SettingsSync.php @@ -0,0 +1,64 @@ + $setting) { + // search for existing record + $record = Setting::where('key', $setting['key'])->first(); + if ($record) { + // if exists, update all except value + $record->update([ + 'name' => $setting['name'], + 'description' => $setting['description'], + 'field' => $setting['field'], + 'active' => $setting['active'], + ]); + } else { + Setting::insert($setting); + } + } + + // purge settings not present in settings.php + Setting::whereNotIn('key', collect($settings)->pluck('key')->all()) + ->delete(); + + $this->info("Settings updated"); + } +} diff --git a/src/config/settings.php b/src/config/settings.php new file mode 100644 index 0000000..1e95953 --- /dev/null +++ b/src/config/settings.php @@ -0,0 +1,37 @@ + 'contact_email', + 'name' => 'Contact form email address', + 'description' => 'The email address that all emails from the contact form will go to.', + 'value' => 'admin@updivision.com', + 'field' => '{"name":"value", "label":"Value", "type":"email"}', + 'active' => 1, + ], + [ + 'key' => 'contact_cc', + 'name' => 'Contact form CC field', + 'description' => 'Email addresses separated by comma, to be included as CC in the email sent by the contact form.', + 'value' => '', + 'field' => '{"name":"value", "label":"Value", "type":"text"}', + 'active' => 1, + + ], + [ + 'key' => 'contact_bcc', + 'name' => 'Contact form BCC field', + 'description' => 'Email addresses separated by comma, to be included as BCC in the email sent by the contact form.', + 'value' => '', + 'field' => '{"name":"value", "label":"Value", "type":"email"}', + 'active' => 1, + ], + [ + 'key' => 'motto', + 'name' => 'Motto', + 'description' => 'Website motto', + 'value' => 'this is the value', + 'field' => '{"name":"value", "label":"Value", "type":"textarea"}', + 'active' => 1, + + ], +];