Skip to content

Commit

Permalink
support loading env variables from systemd's $CREDENTIALS_DIRECTORY
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Mar 29, 2024
1 parent 07fc5a5 commit 3b26180
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ function get_magic_quotes_gpc(): bool {

$autoloader = require __DIR__ . '/../vendor/autoload.php';


function getEnvVar(string $name): string {
$var = getenv($name) ?: '';
if (str_contains($var, '$CREDENTIALS_DIRECTORY')) {
$credentialsDirectory = getenv('CREDENTIALS_DIRECTORY') ?? '';

Check failure on line 19 in src/init.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Expression on left side of ?? is not nullable.
$path = str_replace('$CREDENTIALS_DIRECTORY', $credentialsDirectory, $var);
$var = file_get_contents($path);
}
return trim($var);
}

if (!getenv('DB_TYPE')) {
Dotenv::createImmutable(__DIR__ . '/../')->safeLoad();
}
Expand All @@ -27,13 +38,13 @@ function get_magic_quotes_gpc(): bool {
}
/** @var key-of<DriverManager::DRIVER_MAP> $driver */

$dbPassword = getenv('DB_PASSWORD') ?: '';
$dbPassword = getEnvVar('DB_PASSWORD');

$connectionParams = [
'dbname' => getenv('DB_DATABASE') ?: '',
'user' => getenv('DB_USERNAME') ?: '',
'host' => getenv('DB_HOST') ?: '',
'port' => (int) getenv('DB_PORT'),
'dbname' => getEnvVar('DB_DATABASE'),
'user' => getEnvVar('DB_USERNAME'),
'host' => getEnvVar('DB_HOST'),
'port' => (int) getEnvVar('DB_PORT'),
'driver' => $driver,
];

Expand All @@ -42,13 +53,13 @@ function get_magic_quotes_gpc(): bool {
}

$db = DriverManager::getConnection($connectionParams);
$host = getenv('BASE_HOST') ?: '';
$storeRoot = getenv('DEMO_ROOT') ?: '';
$storeHost = getenv('DEMO_HOST') ?: '';
$parserPath = getenv('PARSER_PATH') ?: '';
$appRoot = getenv('APP_ROOT') ?: '';
$editKey = getenv('EDIT_SECRET') ?: '';
$uploadKey = getenv('UPLOAD_KEY') ?: '';
$host = getEnvVar('BASE_HOST');
$storeRoot = getEnvVar('DEMO_ROOT');
$storeHost = getEnvVar('DEMO_HOST');
$parserPath = getEnvVar('PARSER_PATH');
$appRoot = getEnvVar('APP_ROOT');
$editKey = getEnvVar('EDIT_SECRET');
$uploadKey = getEnvVar('UPLOAD_KEY');

$factory = new \RandomLib\Factory();
$generator = $factory->getMediumStrengthGenerator();
Expand Down

0 comments on commit 3b26180

Please sign in to comment.