|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is licensed under MIT License. |
| 5 | + * |
| 6 | + * Copyright (c) 2026 WebFiori Framework |
| 7 | + * |
| 8 | + * For more information on the license, please visit: |
| 9 | + * https://github.com/WebFiori/.github/blob/main/LICENSE |
| 10 | + * |
| 11 | + */ |
| 12 | +namespace WebFiori\Framework\Health; |
| 13 | + |
| 14 | +/** |
| 15 | + * Registry and runner for health checks. |
| 16 | + * |
| 17 | + * Supports both class-based checks (HealthCheckInterface) and callable checks. |
| 18 | + */ |
| 19 | +class HealthCheck { |
| 20 | + /** |
| 21 | + * @var array Registered checks indexed by name. |
| 22 | + */ |
| 23 | + private static array $checks = []; |
| 24 | + /** |
| 25 | + * Register a health check. |
| 26 | + * |
| 27 | + * Accepts either a HealthCheckInterface instance or a name + callable pair. |
| 28 | + * |
| 29 | + * @param HealthCheckInterface|string $check An instance or a check name. |
| 30 | + * @param callable|null $callable Required if $check is a string. |
| 31 | + */ |
| 32 | + public static function register($check, ?callable $callable = null): void { |
| 33 | + if ($check instanceof HealthCheckInterface) { |
| 34 | + self::$checks[$check->getName()] = $check; |
| 35 | + } else if (is_string($check) && $callable !== null) { |
| 36 | + self::$checks[$check] = $callable; |
| 37 | + } |
| 38 | + } |
| 39 | + /** |
| 40 | + * Run all registered health checks. |
| 41 | + * |
| 42 | + * @return array Aggregate result with 'status', 'timestamp', and 'checks'. |
| 43 | + */ |
| 44 | + public static function runAll(): array { |
| 45 | + $results = []; |
| 46 | + $allOk = true; |
| 47 | + |
| 48 | + foreach (self::$checks as $name => $check) { |
| 49 | + if ($check instanceof HealthCheckInterface) { |
| 50 | + $result = $check->check(); |
| 51 | + } else { |
| 52 | + $raw = $check(); |
| 53 | + |
| 54 | + if ($raw instanceof HealthCheckResult) { |
| 55 | + $result = $raw; |
| 56 | + } else { |
| 57 | + $status = $raw['status'] ?? 'fail'; |
| 58 | + $result = $status === 'ok' |
| 59 | + ? HealthCheckResult::ok($raw) |
| 60 | + : HealthCheckResult::fail($raw['reason'] ?? 'Unknown', $raw); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $results[$name] = $result->toArray(); |
| 65 | + |
| 66 | + if ($result->getStatus() !== 'ok') { |
| 67 | + $allOk = false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return [ |
| 72 | + 'status' => $allOk ? 'ok' : 'fail', |
| 73 | + 'timestamp' => date('c'), |
| 74 | + 'checks' => $results, |
| 75 | + ]; |
| 76 | + } |
| 77 | + /** |
| 78 | + * Remove all registered checks. |
| 79 | + */ |
| 80 | + public static function reset(): void { |
| 81 | + self::$checks = []; |
| 82 | + } |
| 83 | + /** |
| 84 | + * Returns the number of registered checks. |
| 85 | + * |
| 86 | + * @return int |
| 87 | + */ |
| 88 | + public static function getCheckCount(): int { |
| 89 | + return count(self::$checks); |
| 90 | + } |
| 91 | +} |
0 commit comments