-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
143 lines (122 loc) · 4.76 KB
/
Copy pathbootstrap.php
File metadata and controls
143 lines (122 loc) · 4.76 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/*
* This file is part of the WP Boot package.
*
* (ɔ) Frugan <dev@frugan.it>
*
* This source file is subject to the GNU GPLv3 or later license that is bundled
* with this source code in the file LICENSE.
*/
use Env\Env;
use function Env\env;
define('WP_BOOT_ROOT', __DIR__);
require __DIR__.'/vendor/autoload.php';
/**
* Copy custom variables from $_SERVER to $_ENV for PHP-FPM clear_env=yes compatibility.
*
* When using Docker with PHP-FPM and `clear_env = yes` (default),
* environment variables passed via Docker and defined in `environment.conf`
* (e.g. /etc/php/*\/fpm/pool.d/www.conf or /opt/bitnami/php/etc/environment.conf w/ Bitnami)
* are only injected into the $_SERVER superglobal — not into $_ENV or via getenv().
*
* This happens especially when `variables_order = GPCS` (default w/ Bitnami), which excludes `E` (ENV).
* All environment variables received by PHP are strings; type casting is handled manually by the Env library.
*
* @see https://github.com/oscarotero/env/pull/6
* @see https://www.php.net/manual/en/function.getenv.php
* @see https://jolicode.com/blog/what-you-need-to-know-about-environment-variables-with-php
* @see https://stackoverflow.com/a/42389720/3929620
*
* @param array $prefixes If empty, copy from start until first system variable.
* If not empty, copy only variables with these prefixes.
* @param array $systemVars System variables that act as "stop" when $prefixes is empty
*
* @return array Copied variables
*/
function fixMissingEnvVars(
array $prefixes = [],
array $systemVars = ['PATH', 'USER', 'HOME', 'SHELL', 'PWD']
): array {
// If variables_order includes 'E', $_ENV is already populated
if (str_contains(ini_get('variables_order') ?: '', 'E')) {
return [];
}
$copied = [];
if (empty($prefixes)) {
// Sequential mode: copy from start until first system variable
foreach ($_SERVER as $key => $value) {
// If we encounter a system variable, stop
if (in_array($key, $systemVars, true)) {
break;
}
// Copy the variable if it doesn't already exist in $_ENV
if (!isset($_ENV[$key])) {
$_ENV[$key] = $value;
$copied[$key] = $value;
}
}
} else {
// Prefix mode: copy only variables with specific prefixes
foreach ($_SERVER as $key => $value) {
// Skip system variables
if (in_array($key, $systemVars, true)) {
continue;
}
// Check if the variable starts with one of the allowed prefixes
foreach ($prefixes as $prefix) {
if (str_starts_with($key, $prefix) && !isset($_ENV[$key])) {
$_ENV[$key] = $value;
$copied[$key] = $value;
break;
}
}
}
}
return $copied;
}
fixMissingEnvVars(systemVars: []);
// USE_ENV_ARRAY + CONVERT_* + STRIP_QUOTES
Env::$options = 31;
$suffix = '';
$env = '.env';
$envs = [$env];
if (defined('_APP_ENV')) {
$suffix .= '.'._APP_ENV;
$envs[] = $env.$suffix;
}
// docker -> minideb
if (!empty($_SERVER['APP_ENV'])) {
$suffix .= '.'.$_SERVER['APP_ENV'];
$envs[] = $env.$suffix;
}
$envs = array_unique(array_filter($envs));
try {
// NOTE: We use createUnsafeImmutable (not createImmutable) so that
// dotenv-loaded variables are also exposed via getenv() in addition
// to $_ENV / $_SERVER. Many WordPress plugins and libraries read
// configuration via getenv() and silently fall back to defaults when
// the variable is missing — causing hard-to-diagnose bugs.
//
// Thread-safety caveat: getenv()/putenv() are documented as not
// thread-safe in phpdotenv. This only matters on PHP ZTS builds with a
// multi-threaded SAPI (e.g. Apache mod_php with MPM worker/event).
// Standard PHP-FPM and CLI deployments — the expected targets for
// WordPress installations using wp-boot — are single-threaded per
// request and unaffected.
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__, $envs, false);
$dotenv->load();
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASSWORD']);
} catch (Exception $e) {
// https://github.com/phpro/grumphp/blob/master/doc/tasks/phpparser.md#no_exit_statements
exit($e->getMessage());
}
/**
* Set WordPress environment type based on WP_ENV
* Uses WordPress 5.5+ native WP_ENVIRONMENT_TYPE
* Accepted values: 'local', 'development', 'staging', 'production'.
*/
$wp_env = env('WP_ENV') ?: 'production';
if (!defined('WP_ENVIRONMENT_TYPE') && in_array($wp_env, ['local', 'development', 'staging', 'production'], true)) {
define('WP_ENVIRONMENT_TYPE', $wp_env);
}