Commit a7998dcd7e07fe3f4c7dd1f5a0c41985512ad51d

Authored by 谭苏航
1 parent d610bef6

feat: add manual .env parser fallback

Showing 1 changed file with 26 additions and 0 deletions
... ... @@ -24,6 +24,32 @@ if ($hasDotenv && $hasEnvFile) {
24 24 } catch (Exception $e) {
25 25 $logMsg = "[" . date('Y-m-d H:i:s') . "] Startup - Error loading .env: " . $e->getMessage() . "\n";
26 26 }
  27 +} elseif ($hasEnvFile) {
  28 + // Fallback: Manual Parse (If composer dependency is missing)
  29 + $logMsg = "[" . date('Y-m-d H:i:s') . "] Startup - Dotenv library not found. Using manual fallback parser.\n";
  30 + $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  31 + foreach ($lines as $line) {
  32 + $line = trim($line);
  33 + if ($line === '' || strpos($line, '#') === 0)
  34 + continue;
  35 +
  36 + $parts = explode('=', $line, 2);
  37 + if (count($parts) !== 2)
  38 + continue;
  39 +
  40 + $name = trim($parts[0]);
  41 + $value = trim($parts[1]);
  42 +
  43 + // Remove quotes if present
  44 + if (preg_match('/^"(.*)"$/', $value, $m))
  45 + $value = $m[1];
  46 + elseif (preg_match("/^'(.*)'$/", $value, $m))
  47 + $value = $m[1];
  48 +
  49 + putenv("{$name}={$value}");
  50 + $_ENV[$name] = $value;
  51 + $_SERVER[$name] = $value;
  52 + }
27 53 } else {
28 54 $logMsg = "[" . date('Y-m-d H:i:s') . "] Startup - Skipping .env load. Dotenv installed: " . ($hasDotenv ? 'YES' : 'NO') . ", .env exists: " . ($hasEnvFile ? 'YES' : 'NO') . "\n";
29 55 }
... ...
Please register or login to post a comment