CaiWuXiTong/app/Console/Commands/ApplicationReady.php

57 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
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(): 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);
}
$this->components->info('应用已就绪。');
return self::SUCCESS;
}
private function failed(string $message, int $exitCode): int
{
$this->components->error($message);
return $exitCode;
}
}