From 6405e7d344a4d4ccf23ba818bdba7db7fe9f2f8b Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Mon, 2 Jan 2023 21:52:13 +0100 Subject: [PATCH] Fix WP_ENVIRONMENT_TYPE != WP_ENV on cached env See #122 --- src/Env/WordPressEnvBridge.php | 31 ++++++++++++++++++++----------- templates/wp-config.php | 19 ++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/Env/WordPressEnvBridge.php b/src/Env/WordPressEnvBridge.php index 1481ab3..6618f01 100644 --- a/src/Env/WordPressEnvBridge.php +++ b/src/Env/WordPressEnvBridge.php @@ -580,21 +580,11 @@ public function setupConstants() } $done = true; - $names = []; + $names = $this->setupEnvConstants(); foreach (array_keys(self::WP_CONSTANTS) as $key) { $this->defineConstantFromVar($key) and $names[] = $key; } - $envType = $this->determineEnvType(); - if (!defined('WP_ENV')) { - define('WP_ENV', $envType); - $names[] = 'WP_ENV'; - } - if (!defined('WP_ENVIRONMENT_TYPE')) { - define('WP_ENVIRONMENT_TYPE', $this->determineWpEnvType($envType)); - $names[] = 'WP_ENVIRONMENT_TYPE'; - } - $customVarsToSetStr = (string)$this->read(self::CUSTOM_ENV_TO_CONST_VAR_NAME); $customVarsToSet = explode(',', $customVarsToSetStr); foreach ($customVarsToSet as $customVarToSetStr) { @@ -614,6 +604,25 @@ public function setupConstants() $this->wordPressSetup = count(array_intersect($names, ['DB_NAME', 'DB_USER'])) === 2; } + /** + * @return array + */ + public function setupEnvConstants(): array + { + $names = []; + $envType = $this->determineEnvType(); + if (!defined('WP_ENV')) { + define('WP_ENV', $envType); + $names[] = 'WP_ENV'; + } + if (!defined('WP_ENVIRONMENT_TYPE')) { + define('WP_ENVIRONMENT_TYPE', $this->determineWpEnvType($envType)); + $names[] = 'WP_ENVIRONMENT_TYPE'; + } + + return $names; + } + /** * @return bool */ diff --git a/templates/wp-config.php b/templates/wp-config.php index 7865988..8ee094b 100644 --- a/templates/wp-config.php +++ b/templates/wp-config.php @@ -63,9 +63,15 @@ if ($envType !== 'example') { $envLoader->loadAppended("{{{ENV_FILE_NAME}}}.{$envType}", WPSTARTER_PATH); } - $envLoader->setupConstants(); } - + /** + * Core wp_get_environment_type() only supports a pre-defined list of environments types. + * WP Starter tries to map different environments to values supported by core, for example + * "dev" (or "develop", or even "develop-1") will be mapped to "development" accepted by WP. + * In that case, `wp_get_environment_type()` will return "development", but `WP_ENV` will still + * be "dev" (or "develop", or "develop-1"). + */ + $envIsCached ? $envLoader->setupEnvConstants() : $envLoader->setupConstants(); isset($envType) or $envType = $envLoader->determineEnvType(); $debugInfo['env-cache-file'] = [ @@ -91,15 +97,6 @@ unset($envCacheEnabled, $envIsCached); - /** - * Core wp_get_environment_type() only supports a pre-defined list of environments types. - * WP Starter tries to map different environments to values supported by core, for example - * "dev" (or "develop", or even "develop-1") will be mapped to "development" accepted by WP. - * In that case, `wp_get_environment_type()` will return "development", but `WP_ENV` will still - * be "dev" (or "develop", or "develop-1"). - */ - defined('WP_ENV') or define('WP_ENV', $envType); - $phpEnvFilePath = WPSTARTER_PATH . "/{$envType}.php"; $hasPhpEnvFile = file_exists($phpEnvFilePath) && is_readable($phpEnvFilePath); if ($hasPhpEnvFile) {