fn_b/app/Console/Commands/QuietMode.php

43 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\SiteMode;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
final class QuietMode extends Command
{
protected $signature = 'site:quiet {state=status : on, off, or status}';
protected $description = 'Pause free-node publishing and hide airport promotions without deleting data';
public function handle(): int
{
$state = $this->argument('state');
if (! in_array($state, ['on','off','status'], true)) {
$this->error('Use on, off, or status.');
return self::FAILURE;
}
if ($state !== 'status') {
$path = config('site_mode.state_path');
if (! is_dir(dirname($path))) mkdir(dirname($path), 0700, true);
$temporary = $path.'.'.Str::uuid().'.tmp';
try {
$handle = fopen($temporary, 'x');
if (! $handle) throw new \RuntimeException('Cannot create mode state.');
chmod($temporary, 0600);
$payload = json_encode(['quiet' => $state === 'on', 'changed_at' => now()->toIso8601String()]);
try {
if (fwrite($handle, $payload) !== strlen($payload) || ! fflush($handle)) {
throw new \RuntimeException('Cannot save mode state.');
}
} finally { fclose($handle); }
if (! rename($temporary, $path)) throw new \RuntimeException('Cannot activate mode state.');
} finally { if (is_file($temporary)) unlink($temporary); }
\Spatie\ResponseCache\Facades\ResponseCache::clear();
}
$this->info('暂避风头模式:'.(SiteMode::quiet() ? '开启(节点更新暂停,机场广告隐藏)' : '关闭(正常模式)'));
return self::SUCCESS;
}
}