43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
/*
|
|
* Return a configuration value or secret.
|
|
*
|
|
* Values come from the environment first (for example Apache SetEnv),
|
|
* then from the .env file, then from $default. The .env file is the one
|
|
* named by the XXX_ENV_FILE environment variable, or else the .env file
|
|
* next to this one. Git does not track .env; see .env.example.
|
|
*
|
|
* Each line of .env is NAME=value. Blank lines and lines that start
|
|
* with # are skipped, and quotes around a value are removed.
|
|
*/
|
|
function config($key, $default = NULL) {
|
|
static $values = NULL;
|
|
|
|
$env = getenv($key);
|
|
if ($env !== FALSE)
|
|
return $env;
|
|
|
|
if ($values === NULL) {
|
|
$values = array();
|
|
$file = getenv('XXX_ENV_FILE') ?: __DIR__ . '/.env';
|
|
if (is_readable($file)) {
|
|
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
|
|
$line = trim($line);
|
|
if ($line === '' || $line[0] === '#' || strpos($line, '=') === FALSE)
|
|
continue;
|
|
list($name, $value) = explode('=', $line, 2);
|
|
$name = trim(preg_replace('/^export\s+/', '', $name));
|
|
$value = trim($value);
|
|
if (strlen($value) >= 2 &&
|
|
($value[0] === '"' || $value[0] === "'") &&
|
|
substr($value, -1) === $value[0])
|
|
$value = substr($value, 1, -1);
|
|
$values[$name] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
return array_key_exists($key, $values) ? $values[$key] : $default;
|
|
}
|
|
?>
|