69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Setting;
|
|
use App\Support\Maintenance;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* The public site's maintenance mode — the panel's maintenance screen, as two
|
|
* calls. Only the public site goes down; the panel and this API keep working.
|
|
*/
|
|
class MaintenanceController extends Controller
|
|
{
|
|
public function show(): JsonResponse
|
|
{
|
|
$this->authorize('settings.view');
|
|
|
|
return $this->state();
|
|
}
|
|
|
|
/**
|
|
* `enabled` switches it; `message` (language → text) and `until` are kept
|
|
* either way, so the wording can be prepared before the site goes down.
|
|
*/
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
$this->authorize('settings.maintenance');
|
|
|
|
$data = $request->validate([
|
|
'enabled' => ['required', 'boolean'],
|
|
'message' => ['sometimes', 'nullable', 'array'],
|
|
'message.*' => ['nullable', 'string', 'max:500'],
|
|
'until' => ['sometimes', 'nullable', 'date', 'after:now'],
|
|
]);
|
|
|
|
$message = array_key_exists('message', $data)
|
|
? array_filter((array) $data['message'], fn ($text) => filled($text))
|
|
: Maintenance::messages();
|
|
|
|
if ($data['enabled']) {
|
|
Maintenance::enable(
|
|
message: $message,
|
|
until: filled($data['until'] ?? null) ? CarbonImmutable::parse($data['until']) : null,
|
|
);
|
|
} else {
|
|
// Switching it off must not take the prepared wording with it.
|
|
Setting::set(Maintenance::KEY, [...Maintenance::state(), 'enabled' => false, 'until' => null, 'message' => $message]);
|
|
}
|
|
|
|
return $this->state();
|
|
}
|
|
|
|
protected function state(): JsonResponse
|
|
{
|
|
return response()->json(['data' => [
|
|
'enabled' => Maintenance::isEnabled(),
|
|
'message' => (object) Maintenance::messages(),
|
|
'until' => Maintenance::until()?->toIso8601String(),
|
|
'started_at' => Maintenance::startedAt()?->toIso8601String(),
|
|
]]);
|
|
}
|
|
}
|