Skip to content

Commit 15a3328

Browse files
committed
feat: add DatabaseSyncCommand artisan command
- Provides CLI for model discovery, registration, and sync status
1 parent 54ddbc8 commit 15a3328

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace LaravelDatabaseSync\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use LaravelDatabaseSync\Services\ModelDiscoveryService;
7+
use LaravelDatabaseSync\Services\AutoSyncService;
8+
9+
class DatabaseSyncCommand extends Command
10+
{
11+
protected $signature = 'db:sync {action : discover|register|status}';
12+
protected $description = 'Manage database synchronization';
13+
14+
private $discoveryService;
15+
private $autoSyncService;
16+
17+
public function __construct(
18+
ModelDiscoveryService $discoveryService,
19+
AutoSyncService $autoSyncService
20+
) {
21+
parent::__construct();
22+
$this->discoveryService = $discoveryService;
23+
$this->autoSyncService = $autoSyncService;
24+
}
25+
26+
public function handle()
27+
{
28+
$action = $this->argument('action');
29+
30+
switch ($action) {
31+
case 'discover':
32+
$this->discoverModels();
33+
break;
34+
case 'register':
35+
$this->registerModels();
36+
break;
37+
case 'status':
38+
$this->showStatus();
39+
break;
40+
default:
41+
$this->error('Invalid action. Use: discover, register, or status');
42+
}
43+
}
44+
45+
private function discoverModels()
46+
{
47+
$this->info('Discovering Eloquent models...');
48+
49+
$models = $this->discoveryService->discoverModels();
50+
51+
$this->table(['Model Class', 'Table'], array_map(function ($model) {
52+
try {
53+
$instance = new $model;
54+
return [$model, $instance->getTable()];
55+
} catch (\Exception $e) {
56+
return [$model, 'Error: ' . $e->getMessage()];
57+
}
58+
}, $models));
59+
60+
$this->info('Found ' . count($models) . ' models');
61+
}
62+
63+
private function registerModels()
64+
{
65+
$this->info('Registering models for sync...');
66+
67+
$this->autoSyncService->registerAllModels();
68+
69+
$registered = $this->autoSyncService->getRegisteredModels();
70+
$this->info('Registered ' . count($registered) . ' models for sync');
71+
}
72+
73+
private function showStatus()
74+
{
75+
$this->info('Database Sync Status:');
76+
$this->line('Enabled: ' . (config('database-sync.enabled') ? 'Yes' : 'No'));
77+
$this->line('Endpoint: ' . config('database-sync.endpoint'));
78+
79+
$registered = $this->autoSyncService->getRegisteredModels();
80+
$this->line('Registered Models: ' . count($registered));
81+
82+
if (!empty($registered)) {
83+
$this->table(['Model'], array_map(function ($model) {
84+
return [$model];
85+
}, $registered));
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)