|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ProcessMaker\Health; |
| 6 | + |
| 7 | +class CaddyPhpIni |
| 8 | +{ |
| 9 | + /** @var array<string, string> */ |
| 10 | + private const ENV_TO_INI = [ |
| 11 | + 'OCTANE_MEMORY_LIMIT' => 'memory_limit', |
| 12 | + 'OCTANE_MAX_EXECUTION_TIME' => 'max_execution_time', |
| 13 | + 'OCTANE_POST_MAX_SIZE' => 'post_max_size', |
| 14 | + 'OCTANE_UPLOAD_MAX_FILESIZE' => 'upload_max_filesize', |
| 15 | + 'OCTANE_MAX_INPUT_VARS' => 'max_input_vars', |
| 16 | + 'OCTANE_MAX_INPUT_TIME' => 'max_input_time', |
| 17 | + ]; |
| 18 | + |
| 19 | + public static function caddyfilePath(): string |
| 20 | + { |
| 21 | + return base_path('Caddyfile'); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Values from config/octane.php → caddy.env (what the Caddyfile should apply). |
| 26 | + * |
| 27 | + * @return array<string, string> |
| 28 | + */ |
| 29 | + public static function configured(): array |
| 30 | + { |
| 31 | + /** @var array<string, mixed> $env */ |
| 32 | + $env = config('octane.caddy.env', []); |
| 33 | + $php = []; |
| 34 | + |
| 35 | + foreach (self::ENV_TO_INI as $envKey => $iniKey) { |
| 36 | + if (isset($env[$envKey]) && $env[$envKey] !== '') { |
| 37 | + $php[$iniKey] = (string) $env[$envKey]; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return $php; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return array<string, string> |
| 46 | + */ |
| 47 | + public static function fromWorker(): array |
| 48 | + { |
| 49 | + $php = []; |
| 50 | + |
| 51 | + foreach (self::configured() as $directive => $_expected) { |
| 52 | + $actual = ini_get($directive); |
| 53 | + $php[$directive] = $actual !== false && $actual !== '' ? (string) $actual : ''; |
| 54 | + } |
| 55 | + |
| 56 | + return $php; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param array<string, string> $worker |
| 61 | + * |
| 62 | + * @return array<int, array{directive: string, expected: string, actual: string, matches: bool}> |
| 63 | + */ |
| 64 | + public static function verify(array $worker): array |
| 65 | + { |
| 66 | + $results = []; |
| 67 | + |
| 68 | + foreach (self::configured() as $directive => $expected) { |
| 69 | + $actual = $worker[$directive] ?? ''; |
| 70 | + $results[] = [ |
| 71 | + 'directive' => $directive, |
| 72 | + 'expected' => $expected, |
| 73 | + 'actual' => $actual !== '' ? $actual : '(missing)', |
| 74 | + 'matches' => self::valuesMatch($directive, $expected, $actual), |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + return $results; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param array<string, string> $worker |
| 83 | + */ |
| 84 | + public static function allMatch(array $worker): bool |
| 85 | + { |
| 86 | + foreach (self::verify($worker) as $entry) { |
| 87 | + if (!$entry['matches']) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $worker !== []; |
| 93 | + } |
| 94 | + |
| 95 | + public static function directiveMatches(string $directive, string $expected, string $actual): bool |
| 96 | + { |
| 97 | + return self::valuesMatch($directive, $expected, $actual); |
| 98 | + } |
| 99 | + |
| 100 | + public static function directivesEquivalent(string $directive, string $left, string $right): bool |
| 101 | + { |
| 102 | + if ($left === '' && $right === '') { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + if ($left === '' || $right === '') { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return self::normalizeValue($directive, $left) === self::normalizeValue($directive, $right); |
| 111 | + } |
| 112 | + |
| 113 | + private static function valuesMatch(string $directive, string $expected, string $actual): bool |
| 114 | + { |
| 115 | + if ($actual === '') { |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + $normalizedExpected = self::normalizeValue($directive, $expected); |
| 120 | + $normalizedActual = self::normalizeValue($directive, $actual); |
| 121 | + |
| 122 | + if ($normalizedExpected === $normalizedActual) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + if ($directive === 'max_execution_time' && $normalizedActual === '0' && (int) $normalizedExpected > 0) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + if ($directive === 'max_input_time' && $normalizedActual === '-1' && (int) $normalizedExpected > 0) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + private static function normalizeValue(string $directive, string $value): string |
| 138 | + { |
| 139 | + $value = trim(strtolower($value)); |
| 140 | + |
| 141 | + if (is_numeric($value)) { |
| 142 | + return (string) (int) (float) $value; |
| 143 | + } |
| 144 | + |
| 145 | + return $value; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return array{0: ?array<string, string>, 1: ?string, 2: ?string, 3: ?array<string, bool>} |
| 150 | + */ |
| 151 | + public static function workerSnapshot(bool $includePhpIni): array |
| 152 | + { |
| 153 | + if (!$includePhpIni) { |
| 154 | + return [null, null, null, null]; |
| 155 | + } |
| 156 | + |
| 157 | + return [ |
| 158 | + self::fromWorker(), |
| 159 | + self::caddyfilePath(), |
| 160 | + PHP_VERSION, |
| 161 | + CliPhpIni::extensionsLoaded(), |
| 162 | + ]; |
| 163 | + } |
| 164 | +} |
0 commit comments