Skip to content

Commit

Permalink
Aggiornamenti al modulo DbConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
inerba committed Feb 28, 2024
1 parent deb9c22 commit e894653
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Commands/DbConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
84 changes: 84 additions & 0 deletions src/Commands/DbConfigUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Postare\DbConfig\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Pluralizer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Postare\DbConfig\DbConfig;

class DbConfigUpdate extends Command
{
public $signature = 'db_config:update';

public $description = 'Create a new settings';

/**
* Filesystem instance
*/
protected Filesystem $files;

/**
* Create a new command instance.
*/
public function __construct(Filesystem $files)
{
parent::__construct();

$this->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');
}


}


}
9 changes: 5 additions & 4 deletions src/DbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/DbConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +86,7 @@ protected function getCommands(): array
{
return [
DbConfigCommand::class,
DbConfigUpdate::class,
];
}

Expand Down

0 comments on commit e894653

Please sign in to comment.