40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
function loadEnvironment($path) {
|
|
if (!file_exists($path)) {
|
|
throw new Exception(".env file not found at: $path");
|
|
}
|
|
|
|
# File method, parses each line as an array element
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
|
|
# Loops each line
|
|
foreach ($lines as $line) {
|
|
# First removes white spaces, then searches if the first character is a comment, skips iteration line.
|
|
// Skip comments
|
|
if (strpos(trim($line), '#') === 0) continue;
|
|
|
|
# Continues if it finds an = sign somewhere
|
|
// Parse KEY=value
|
|
if (strpos($line, '=') !== false) {
|
|
# Creates a list, array alike, the items are set by splitting (explode()) each line by the equal sign.
|
|
list($key, $value) = explode('=', $line, 2);
|
|
|
|
# Removes whitespaces and quotes
|
|
$key = trim($key);
|
|
$value = trim($value, '"\''); // Remove quotes
|
|
|
|
# Stores each value in $_ENV supergglobal
|
|
$_ENV[$key] = $value;
|
|
|
|
# Not sure what this does
|
|
putenv("$key=$value");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load at application bootstrap
|
|
# Not sure either
|
|
loadEnvironment(__DIR__ . '/.env');
|
|
|
|
// First found on ttps://levelup.gitconnected.com/how-to-securely-handle-production-credentials-in-php-without-exposing-them-in-git-237f4fd5cf91,
|
|
// Own comments using #
|