Skip to content

Commit

Permalink
Fix WP_ENVIRONMENT_TYPE != WP_ENV on cached env
Browse files Browse the repository at this point in the history
See #122
  • Loading branch information
gmazzap committed Jan 2, 2023
1 parent 380f308 commit 6405e7d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
31 changes: 20 additions & 11 deletions src/Env/WordPressEnvBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
*/
Expand Down
19 changes: 8 additions & 11 deletions templates/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [
Expand All @@ -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) {
Expand Down

0 comments on commit 6405e7d

Please sign in to comment.