Umsetzung von docs/specs/self-updater.md für Laravel: - Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress, selbstheilender Wartungsmodus in storage/.maintenance - Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects, ZIP-Download per Stream mit PK- und SHA-256-Prüfung - Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset - Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert - Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal - Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt, Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis - Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck - Audit-Log für Updates, Kanalwechsel und Lizenzänderungen - Version 0.2.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Update;
|
||
|
||
/**
|
||
* Fortschritt eines laufenden Updates in storage/.update-progress – das Frontend
|
||
* fragt ihn alle ~2 Sekunden ab.
|
||
*/
|
||
class UpdateProgress
|
||
{
|
||
/** Ohne Lebenszeichen gilt ein Lauf nach dieser Zeit als abgestürzt. */
|
||
public const STALE_AFTER_SECONDS = 600;
|
||
|
||
public function __construct(private readonly ?string $file = null) {}
|
||
|
||
public function set(int $pct, string $step, string $text = ''): void
|
||
{
|
||
AtomicFile::write($this->path(), json_encode([
|
||
'pct' => $pct,
|
||
'step' => $step,
|
||
'text' => $text,
|
||
'ts' => time(),
|
||
], JSON_UNESCAPED_UNICODE));
|
||
}
|
||
|
||
/**
|
||
* Fehlerzustand: pct = -1 unterscheidet ihn eindeutig von „0 % erledigt“.
|
||
*/
|
||
public function fail(string $text): void
|
||
{
|
||
$this->set(-1, 'error', $text);
|
||
}
|
||
|
||
/**
|
||
* @return array{pct: int, step: string, text: string, ts: int}
|
||
*/
|
||
public function get(): array
|
||
{
|
||
$data = json_decode((string) @file_get_contents($this->path()), true);
|
||
|
||
if (! is_array($data) || ! isset($data['pct'], $data['step'])) {
|
||
return ['pct' => 0, 'step' => 'idle', 'text' => '', 'ts' => 0];
|
||
}
|
||
|
||
return [
|
||
'pct' => (int) $data['pct'],
|
||
'step' => (string) $data['step'],
|
||
'text' => (string) ($data['text'] ?? ''),
|
||
'ts' => (int) ($data['ts'] ?? 0),
|
||
];
|
||
}
|
||
|
||
public function isRunning(): bool
|
||
{
|
||
$progress = $this->get();
|
||
|
||
return ! in_array($progress['step'], ['idle', 'done', 'error'], true)
|
||
&& time() - $progress['ts'] < self::STALE_AFTER_SECONDS;
|
||
}
|
||
|
||
public function clear(): void
|
||
{
|
||
@unlink($this->path());
|
||
}
|
||
|
||
private function path(): string
|
||
{
|
||
return $this->file ?? storage_path('.update-progress');
|
||
}
|
||
}
|