Repository navigation
Expand file tree
/
Copy pathenv.php
More file actions
54 lines (47 loc) · 1.31 KB
/
Copy pathenv.php
File metadata and controls
54 lines (47 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Environment Configuration Loader
* Loads environment variables from .env file
*/
function loadEnv($path = __DIR__ . '/.env')
{
if (!file_exists($path)) {
die("ERROR: .env file not found. Please copy .env.example to .env and configure your credentials.");
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
// Skip comments
if (strpos(trim($line), '#') === 0) {
continue;
}
// Parse key=value pairs
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
// Remove quotes if present
if (preg_match('/^(["\'])(.*)\\1$/', $value, $matches)) {
$value = $matches[2];
}
// Set as environment variable and in $_ENV
if (!array_key_exists($key, $_ENV)) {
putenv("$key=$value");
$_ENV[$key] = $value;
}
}
}
}
/**
* Get environment variable with optional default
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
$value = $_ENV[$key] ?? $default;
}
return $value;
}
// Load environment variables
loadEnv();
?>