diff --git a/src/Commands/TursoSyncCommand.php b/src/Commands/TursoSyncCommand.php index 1f199f5..9589cc8 100644 --- a/src/Commands/TursoSyncCommand.php +++ b/src/Commands/TursoSyncCommand.php @@ -15,15 +15,15 @@ class TursoSyncCommand extends Command public $description = 'Sync changes from the remote database to the local replica manually.'; - protected function compileRunProcess(array $config): string + protected function compileRunProcess(string $connectionName): string { return sprintf( '%s %s "%s" "%s" "%s"', $this->getNodePath(), config('turso-laravel.sync_command.script_filename'), - data_get($config, 'db_url'), - data_get($config, 'access_token'), - data_get($config, 'db_replica'), + DB::connection($connectionName)->getConfig('db_url'), + DB::connection($connectionName)->getConfig('access_token'), + DB::connection($connectionName)->getConfig('db_replica'), ); } @@ -42,7 +42,7 @@ public function handle(): int { $timeout = (int) config('turso-laravel.sync_command.timeout'); - $connectionName = (string) $this->argument('connectionName'); + $connectionName = $this->argument('connectionName') ?? DB::getDefaultConnection(); if (DB::connection($connectionName)->getConfig('driver') !== 'turso') { $this->error('The specified connection is not a Turso connection.'); @@ -58,9 +58,7 @@ public function handle(): int $result = Process::timeout($timeout) ->path(config('turso-laravel.sync_command.script_path') ?? base_path()) - ->run($this->compileRunProcess( - DB::connection($connectionName)->getConfig() - )); + ->run($this->compileRunProcess($connectionName)); if ($result->failed()) { throw new RuntimeException('Turso sync command failed: ' . $result->errorOutput()); diff --git a/src/Facades/Turso.php b/src/Facades/Turso.php index 3df0dc3..03af018 100644 --- a/src/Facades/Turso.php +++ b/src/Facades/Turso.php @@ -8,10 +8,9 @@ use RichanFongdasen\Turso\TursoManager; /** - * @see \RichanFongdasen\Turso\TursoClient + * @see \RichanFongdasen\Turso\TursoManager * * @mixin \RichanFongdasen\Turso\TursoManager - * @mixin \RichanFongdasen\Turso\TursoClient */ class Turso extends Facade { diff --git a/src/TursoLaravelServiceProvider.php b/src/TursoLaravelServiceProvider.php index 56386fe..858bad6 100644 --- a/src/TursoLaravelServiceProvider.php +++ b/src/TursoLaravelServiceProvider.php @@ -4,11 +4,8 @@ namespace RichanFongdasen\Turso; -use Illuminate\Console\Events\CommandFinished; -use Illuminate\Console\Events\CommandStarting; use Illuminate\Database\Connection; use Illuminate\Database\DatabaseManager; -use Illuminate\Support\Facades\Event; use RichanFongdasen\Turso\Commands\TursoSyncCommand; use RichanFongdasen\Turso\Database\TursoConnection; use RichanFongdasen\Turso\Database\TursoConnector; @@ -22,22 +19,8 @@ public function boot(): void { parent::boot(); - Event::listen(function (CommandStarting $event) { - if (! app()->bound('running-artisan-command')) { - app()->instance('running-artisan-command', data_get($event, 'command')); - } - }); - - Event::listen(function (CommandFinished $event) { - if (data_get($event, 'command') === 'turso:sync') { - return; - } - - if ( - (app('running-artisan-command') === data_get($event, 'command')) - ) { - Turso::sync(); - } + app()->terminating(function () { + Turso::sync(); }); }