*/ public array $latestCommits = []; public function mount(): void { $this->reloadDashboardData(); } public static function canAccess(): bool { $user = Auth::user(); if (! $user) { return false; } // Pagina informativa: accessibile a chi accede al pannello. return true; } public function canRunUpdate(): bool { $user = Auth::user(); return $user && method_exists($user, 'hasAnyRole') && $user->hasAnyRole(['super-admin', 'admin', 'amministratore']); } public function reloadDashboardData(): void { $this->loadVersionInfo(); $this->loadLatestCommits(); } public function runUpdate(): void { if (! $this->canRunUpdate()) { Notification::make() ->title('Permessi insufficienti') ->body('Solo super-admin/admin/amministratore possono lanciare gli aggiornamenti.') ->danger() ->send(); return; } $params = [ '--channel' => $this->updateChannel, '--apply' => true, ]; if ($this->updateDryRun) { $params['--dry-run'] = true; } if ($this->updateForce) { $params['--force'] = true; } try { $exitCode = Artisan::call('netgescon:update', $params); $output = trim((string) Artisan::output()); $this->lastUpdateExitCode = $exitCode; $this->lastUpdateOutput = $output; $this->lastUpdateAt = now()->format('d/m/Y H:i:s'); $this->reloadDashboardData(); Notification::make() ->title($exitCode === 0 ? 'Aggiornamento completato' : 'Aggiornamento terminato con errori') ->body($exitCode === 0 ? 'Procedura conclusa. Versione e commit aggiornati.' : 'Controlla il log tecnico nella sezione output.') ->{$exitCode === 0 ? 'success' : 'danger'}() ->send(); } catch (Throwable $e) { $this->lastUpdateExitCode = 1; $this->lastUpdateAt = now()->format('d/m/Y H:i:s'); $this->lastUpdateOutput = 'Errore durante esecuzione update: ' . $e->getMessage(); Notification::make() ->title('Errore durante update') ->body($e->getMessage()) ->danger() ->send(); } } private function loadVersionInfo(): void { $versionFile = base_path('VERSION'); if (File::exists($versionFile)) { $fileVersion = trim((string) File::get($versionFile)); if ($fileVersion !== '') { $this->currentVersion = $fileVersion; $this->versionSource = 'VERSION'; return; } } $this->currentVersion = (string) config('netgescon.version', '0.0.0'); $this->versionSource = 'config'; } private function loadLatestCommits(): void { $this->latestCommits = []; $format = '%H|%h|%an|%ad|%s'; $command = 'git log -n 20 --date=format:"%d/%m/%Y %H:%M" --pretty=format:"' . $format . '"'; $result = Process::path(base_path())->run($command); if (! $result->successful()) { return; } $lines = preg_split('/\r\n|\r|\n/', trim($result->output())) ?: []; foreach ($lines as $line) { $parts = explode('|', $line, 5); if (count($parts) !== 5) { continue; } $this->latestCommits[] = [ 'hash' => (string) $parts[0], 'short_hash' => (string) $parts[1], 'author' => (string) $parts[2], 'date' => (string) $parts[3], 'message' => (string) $parts[4], ]; } } }