Files
2026-05-23 19:05:37 +02:00

102 lines
2.9 KiB
PHP
Executable File

<?php
namespace App\Services\Diagnostics;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class SystemDiagnostic
{
/**
* @return array<DiagnosticResult>
*/
public function runAll(): array
{
return [
$this->checkPhpExtensions(),
$this->checkPhpVersion(),
$this->checkCache(),
$this->checkSession(),
$this->checkRedis(),
];
}
public function checkPhpExtensions(): DiagnosticResult
{
$required = ['pdo_mysql', 'curl', 'json', 'mbstring', 'xml', 'zip', 'bcmath'];
$missing = array_filter($required, fn ($ext) => ! extension_loaded($ext));
if ($missing !== []) {
return DiagnosticResult::error(
'PHP Extensions',
'Missing: ' . implode(', ', $missing),
'Install: sudo apt install php-' . implode(' php-', $missing),
);
}
return DiagnosticResult::ok('PHP Extensions', 'All required extensions loaded');
}
public function checkPhpVersion(): DiagnosticResult
{
$current = PHP_VERSION_ID;
$min = 80100; // PHP 8.1
if ($current < $min) {
return DiagnosticResult::error(
'PHP Version',
'Current: ' . PHP_VERSION . ' (minimum: 8.1)',
'Upgrade PHP to 8.1 or higher',
);
}
return DiagnosticResult::ok('PHP Version', PHP_VERSION);
}
public function checkCache(): DiagnosticResult
{
try {
Cache::put('diagnostic_test', 'ok', 10);
$value = Cache::get('diagnostic_test');
if ($value === 'ok') {
return DiagnosticResult::ok('Cache', 'Working (' . config('cache.default') . ')');
}
return DiagnosticResult::warning('Cache', 'Cache returned unexpected value');
} catch (\Exception $e) {
return DiagnosticResult::error('Cache', $e->getMessage());
}
}
public function checkSession(): DiagnosticResult
{
$driver = config('session.driver');
if ($driver === 'file' && app()->environment('production')) {
return DiagnosticResult::warning(
'Session',
'Using file sessions in production',
'Consider using redis or database sessions',
);
}
return DiagnosticResult::ok('Session', "Driver: {$driver}");
}
public function checkRedis(): DiagnosticResult
{
if (config('redis.default.host') === '127.0.0.1') {
try {
Redis::ping();
return DiagnosticResult::ok('Redis', 'Connected');
} catch (\Exception $e) {
return DiagnosticResult::warning('Redis', 'Not reachable: ' . $e->getMessage());
}
}
return DiagnosticResult::ok('Redis', 'Not configured (optional)');
}
}