CaiWuXiTong/app/Console/Commands/ApplicationReady.php

62 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Utilities\SystemDatabaseConfiguration;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class ApplicationReady extends Command
{
public const EXIT_NOT_INSTALLED = 20;
public const EXIT_RUNTIME_NOT_WRITABLE = 21;
public const EXIT_DATABASE_CONNECTION_FAILED = 22;
protected $signature = 'app:ready';
protected $description = '检查应用安装、运行目录和数据库是否已经可用';
public function handle(SystemDatabaseConfiguration $configuration): int
{
if (! config('app.installed')) {
return $this->failed('APP_INSTALLED 未被 Laravel 加载。', self::EXIT_NOT_INSTALLED);
}
foreach ([
storage_path('app'),
storage_path('framework/views'),
storage_path('logs'),
base_path('bootstrap/cache'),
base_path('.env'),
] as $writablePath) {
if (! is_writable($writablePath)) {
return $this->failed('Laravel 运行目录不可写。', self::EXIT_RUNTIME_NOT_WRITABLE);
}
}
try {
DB::selectOne('SELECT 1');
} catch (Throwable) {
return $this->failed('Laravel 数据库连接失败。', self::EXIT_DATABASE_CONNECTION_FAILED);
}
if ($configuration->current()['mode'] === 'bootstrap') {
$this->components->info('本地网页和临时引导库已就绪;团队共享数据尚未接入。');
} else {
$this->components->info('应用运行正常,当前数据库可查询。团队数据接入状态请运行 system-database:verify 检查。');
}
return self::SUCCESS;
}
private function failed(string $message, int $exitCode): int
{
$this->components->error($message);
return $exitCode;
}
}