60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
// .env 파일을 한 번만 읽어 환경변수로 주입합니다.
|
|
function kngil_load_env_once(string $path): void
|
|
{
|
|
static $loaded = false;
|
|
if ($loaded) {
|
|
return;
|
|
}
|
|
|
|
$loaded = true;
|
|
|
|
if (!is_file($path) || !is_readable($path)) {
|
|
return;
|
|
}
|
|
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES);
|
|
if ($lines === false) {
|
|
return;
|
|
}
|
|
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || str_starts_with($line, '#')) {
|
|
continue;
|
|
}
|
|
|
|
if (str_starts_with($line, 'export ')) {
|
|
$line = substr($line, 7);
|
|
}
|
|
|
|
$pos = strpos($line, '=');
|
|
if ($pos === false) {
|
|
continue;
|
|
}
|
|
|
|
$name = trim(substr($line, 0, $pos));
|
|
$value = trim(substr($line, $pos + 1));
|
|
|
|
if ($name === '') {
|
|
continue;
|
|
}
|
|
|
|
if (
|
|
(str_starts_with($value, '"') && str_ends_with($value, '"')) ||
|
|
(str_starts_with($value, "'") && str_ends_with($value, "'"))
|
|
) {
|
|
$value = substr($value, 1, -1);
|
|
}
|
|
|
|
$current = getenv($name);
|
|
if ($current !== false && $current !== '') {
|
|
continue;
|
|
}
|
|
|
|
putenv($name . '=' . $value);
|
|
$_ENV[$name] = $value;
|
|
$_SERVER[$name] = $value;
|
|
}
|
|
}
|