-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathload_db_secrets.php
64 lines (53 loc) · 1.91 KB
/
load_db_secrets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
// This file fetches credentials on the fly from files in etc.
// These settings can later be overridden by Symfony files
// (in order of precedence): .env.local.php .env.local .env.
use Symfony\Component\Dotenv\Dotenv;
require 'autoload.php';
function get_db_url(): string
{
// Allow .env.local to override the DATABASE_URL since it can contain the
// proper serverVersion, which is needed for automatically creating migrations.
$localEnvFile = WEBAPPDIR . '/.env.local';
if (file_exists($localEnvFile)) {
$dotenv = (new Dotenv())->usePutenv(false);
$localEnvData = $dotenv->parse(file_get_contents($localEnvFile));
if (isset($localEnvData['DATABASE_URL'])) {
return $localEnvData['DATABASE_URL'];
}
}
$dbsecretsfile = ETCDIR . '/dbpasswords.secret';
$db_credentials = @file($dbsecretsfile);
if (!$db_credentials) {
# Make sure that this fails with a clear error in Symfony.
return 'mysql://cannot_read_dbpasswords_secret:@localhost:3306/';
}
foreach ($db_credentials as $line) {
if ($line[0] == '#') {
continue;
}
list($_, $host, $db, $user, $pass, $port) = array_pad(explode(':', trim($line)), 6, null);
break;
}
return sprintf(
'mysql://%s:%s@%s:%d/%s?serverVersion=5.7.0',
rawurlencode($user), rawurlencode($pass), rawurlencode($host),
$port ?? 3306, rawurlencode($db)
);
}
function get_app_secret(): string
{
$appsecretsfile = ETCDIR . '/symfony_app.secret';
$contents = file_get_contents($appsecretsfile);
if ($contents === false) {
return '';
}
return trim($contents);
}
$env = [
'APP_SECRET' => get_app_secret(),
'DATABASE_URL' => get_db_url(),
];
foreach ($env as $k => $v) {
$_ENV[$k] = $_ENV[$k] ?? (isset($_SERVER[$k]) && !str_starts_with($k, 'HTTP_') ? $_SERVER[$k] : $v);
}