From e89465361be507dc95b47e36bc7a41260edb9b10 Mon Sep 17 00:00:00 2001 From: Francesco Apruzzese Date: Wed, 28 Feb 2024 12:10:12 +0100 Subject: [PATCH] Aggiornamenti al modulo DbConfig --- src/Commands/DbConfigCommand.php | 2 +- src/Commands/DbConfigUpdate.php | 84 ++++++++++++++++++++++++++++++++ src/DbConfig.php | 9 ++-- src/DbConfigServiceProvider.php | 2 + 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/Commands/DbConfigUpdate.php diff --git a/src/Commands/DbConfigCommand.php b/src/Commands/DbConfigCommand.php index 37a4218..4c75cd9 100644 --- a/src/Commands/DbConfigCommand.php +++ b/src/Commands/DbConfigCommand.php @@ -8,7 +8,7 @@ class DbConfigCommand extends Command { - public $signature = 'make:settings {name} {panel?}'; + public $signature = 'make:db_config {name} {panel?}'; public $description = 'Create a new settings'; diff --git a/src/Commands/DbConfigUpdate.php b/src/Commands/DbConfigUpdate.php new file mode 100644 index 0000000..fe917af --- /dev/null +++ b/src/Commands/DbConfigUpdate.php @@ -0,0 +1,84 @@ +files = $files; + } + + /** + * Execute the console command. + */ + public function handle(): void + { + // Add field to database table + $this->info('Updating database table'); + + // Check if table exists and has the field group + if (Schema::hasTable('db_config') && !Schema::hasColumn('db_config', 'group')) { + $this->info('Adding field group to db_config table'); + Schema::table('db_config', function ($table) { + $table->string('group')->after('id'); + $table->unique(['group', 'key']); + }); + } else { + $this->info('table db_config already has field group'); + } + + + $groups = DB::table('db_config') + ->where('group', '') + ->get(); + + if($groups->count() > 0) { + $this->info('Updating group field in db_config table'); + + foreach ($groups as $group) { + + $groupName = $group->key; + + $settings = json_decode($group->settings, true); + + foreach ($settings as $key => $setting) { + DbConfig::set("$groupName.$key", $setting); + } + + } + + DB::table('db_config') + ->where('group', '') + ->delete(); + + } else { + $this->info('No records to update'); + } + + + } + + +} diff --git a/src/DbConfig.php b/src/DbConfig.php index 180bee6..74dcb65 100644 --- a/src/DbConfig.php +++ b/src/DbConfig.php @@ -18,13 +18,12 @@ class DbConfig */ public static function get(string $key, mixed $default = null): mixed { - - $cachename = "db-config.$key"; - $keyParts = explode('.', $key); $group = array_shift($keyParts); // Prende il primo elemento dell'array e lo rimuove dall'array $setting = $keyParts[0] ?? null; + $cachename = "db-config.{$group}.{$setting}"; + // Utilizzo del caching per evitare chiamate al database multiple $data = Cache::rememberForever($cachename, function () use ($group, $setting) { @@ -58,7 +57,9 @@ public static function set(string $key, mixed $value): void $group = array_shift($keyParts); $setting = $keyParts[0] ?? null; - $data = Cache::forget("db-config.$key"); + $cachename = "db-config.{$group}.{$setting}"; + + Cache::forget($cachename); DB::table('db_config') ->updateOrInsert( diff --git a/src/DbConfigServiceProvider.php b/src/DbConfigServiceProvider.php index 1cdea0e..9d54792 100644 --- a/src/DbConfigServiceProvider.php +++ b/src/DbConfigServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Blade; use Livewire\Features\SupportTesting\Testable; use Postare\DbConfig\Commands\DbConfigCommand; +use Postare\DbConfig\Commands\DbConfigUpdate; use Postare\DbConfig\Testing\TestsDbConfig; use Spatie\LaravelPackageTools\Commands\InstallCommand; use Spatie\LaravelPackageTools\Package; @@ -85,6 +86,7 @@ protected function getCommands(): array { return [ DbConfigCommand::class, + DbConfigUpdate::class, ]; }