path(), json_encode([ 'since' => time(), 'until' => time() + $maxSeconds, ])); } public function deactivate(): void { $data = json_decode((string) @file_get_contents($this->path()), true); @unlink($this->path()); // Zeitraum des letzten Updates merken (siehe covers()) if (is_array($data) && isset($data['since'])) { AtomicFile::write($this->lastPath(), json_encode(['since' => (int) $data['since'], 'until' => time()])); } } /** * Dateiaustausch ohne Wartungsmodus (z. B. ein Plugin wird aktualisiert): Die Seite bleibt * erreichbar, Ladefehler in dieser Zeit schalten aber kein Plugin ab. */ public function beginQuietUpdate(): void { AtomicFile::write($this->windowPath(), json_encode(['since' => time()])); } public function endQuietUpdate(): void { $data = json_decode((string) @file_get_contents($this->windowPath()), true); @unlink($this->windowPath()); if (is_array($data) && isset($data['since'])) { AtomicFile::write($this->lastPath(), json_encode(['since' => (int) $data['since'], 'until' => time()])); } } /** * Läuft gerade ein Update – oder ist eines erst eben zu Ende gegangen? */ public function isUpdating(): bool { return $this->isActive() || $this->covers(time()); } /** * Fiel dieser Zeitpunkt in ein Update (einschließlich der Nachlaufzeit)? */ public function covers(int $timestamp): bool { foreach ([$this->path(), $this->windowPath(), $this->lastPath()] as $file) { $data = json_decode((string) @file_get_contents($file), true); if (! is_array($data) || ! isset($data['since'])) { continue; } $until = match ($file) { // Läuft noch (ein abgebrochener Plugin-Austausch zählt höchstens zehn Minuten) $this->path() => max(time(), (int) ($data['until'] ?? 0)), $this->windowPath() => min(time(), (int) $data['since'] + 600), default => (int) ($data['until'] ?? 0), }; if ($timestamp >= (int) $data['since'] && $timestamp <= $until + self::GRACE_SECONDS) { return true; } } return false; } public function isActive(): bool { if (! is_file($this->path())) { return false; } $data = json_decode((string) @file_get_contents($this->path()), true); $until = is_array($data) ? (int) ($data['until'] ?? 0) : 0; if ($until <= time()) { $this->deactivate(); return false; } return true; } public function retryAfter(): int { $data = json_decode((string) @file_get_contents($this->path()), true); $remaining = is_array($data) ? (int) ($data['until'] ?? 0) - time() : 0; return max(5, min(60, $remaining)); } private function path(): string { return $this->file ?? storage_path('.maintenance'); } private function windowPath(): string { return $this->path().'-quiet'; } private function lastPath(): string { return $this->path().'-last'; } }