Files
2026-06-03 23:12:10 +03:00

53 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core;
final class Env
{
public static function load(string $path): void
{
if (!is_file($path)) {
return;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
return;
}
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
[$key, $value] = array_pad(explode('=', $line, 2), 2, '');
$key = trim($key);
$value = self::normalizeValue(trim($value));
if ($key === '') {
continue;
}
$_ENV[$key] = $value;
putenv($key . '=' . $value);
}
}
private static function normalizeValue(string $value): string
{
if (
strlen($value) >= 2
&& (($value[0] === '"' && $value[-1] === '"') || ($value[0] === "'" && $value[-1] === "'"))
) {
return substr($value, 1, -1);
}
return $value;
}
}