Add docker entrypoint and env loader
This commit is contained in:
59
kngil/bbs/env.php
Normal file
59
kngil/bbs/env.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user