Skip to content

Commit

Permalink
Always use WP-compatible value in WP_ENVIRONMENT_TYPE
Browse files Browse the repository at this point in the history
Defaulting to production in case can't be custom value can't be mapped.

Also:
- ensure WP_ENV is always defined
- when mapping custom env to WP, check non-word characters, e.g. "mydev" won't map to "development", "my-dev" will.
  • Loading branch information
gmazzap committed Dec 17, 2020
1 parent 1b95daa commit 874c80f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
14 changes: 6 additions & 8 deletions docs/03-WordPress-Integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ WordPress does not allow arbitrary values for `WP_ENVIRONMENT_TYPE`, in fact it
- `"staging"`
- `"production"`

Unlike WordPress, WP Starter does not limit `WP_ENVIRONMENT_TYPE` (`WP_ENV` / `WORDPRESS_ENV`) to specific values, and in the case a valuenon supported by WordPress is used, to maximize compatibility, WP Starter will try to "map" different values to one of those supported by WP.
Unlike WordPress, WP Starter does not limit environment to specific values, and in the case a value not supported by WordPress is used, to maximize compatibility, WP Starter will try to "map" different values to one of those supported by WP.

For example, setting `WP_ENVIRONMENT_TYPE` to `"develop"` WP Starter will define a `WP_ENVIRONMENT_TYPE` constant having `"development"` as value.
For example, setting `WP_ENVIRONMENT_TYPE` env variable to `"develop"` WP Starter will define a `WP_ENVIRONMENT_TYPE` constant having `"development"` as value.

The original `"develop"` value will be available in a constant named `WP_ENV`.
The original `"develop"` value will be available in the `WP_ENV` constant.

In the case WP Starter is not able to map an environment to a value supported by WordPress, the original value will be available in both `WP_ENVIRONMENT_TYPE` and `WP_ENV`, but the WordPress [`wp_get_environment_type`](https://developer.wordpress.org/reference/functions/wp_get_environment_type/) function will return `"production"` because that is te default value used by WordPress.

Expand Down Expand Up @@ -199,15 +199,13 @@ WP_ENV=something_very_custom
WP Starter will declare:

```php
define('WP_ENVIRONMENT_TYPE', 'something_very_custom');
define('WP_ENVIRONMENT_TYPE', 'production');
define('WP_ENV', 'something_very_custom');
```

WP Starter was not able to map `"something_very_custom"` to any of the four environment types supported by WordPress, so stored it as-is in `WP_ENVIRONMENT_TYPE` constant.
WP Starter was not able to map `"something_very_custom"` to any of the four environment types supported by WordPress, so stored "production" in `WP_ENVIRONMENT_TYPE` constant, because in any case WordPress would have defaulted to that value when calling `wp_get_environment_type()`.

However, because `"something_very_custom"` is not a value supported by WordPress `wp_get_environment_type()` will return `"production"`.

This is why we suggest referring to the constant `WP_ENVIRONMENT_TYPE` instead to the function `wp_get_environment_type()` when there's the desire (or the need) to use environment types that are not one the four supported by WordPress.
This is why we suggest referring to the constant `WP_ENV` instead to the `WP_ENVIRONMENT_TYPE` constant or the function `wp_get_environment_type()` when there's the desire (or the need) to use environment types that are not one the four supported by WordPress.

Finally, it must be noted that is possible to use a custom WP Starter-specific environment and a WordPress compatible environment by setting _both_ `WP_ENV` and `WP_ENVIRONMENT_TYPE`.

Expand Down
8 changes: 6 additions & 2 deletions src/Env/WordPressEnvBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ public function setupConstants()
}

$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';
Expand Down Expand Up @@ -696,12 +700,12 @@ private function determineWpEnvType(string $envType): string
}

foreach (self::ENV_TYPES as $envTypeName => $envTypeMapped) {
if (strpos($envType, $envTypeName) !== false) {
if (preg_match("~(?:^|[^a-z]+){$envTypeName}(?:[^a-z]+|$)~", $envType)) {
return $envTypeMapped;
}
}

return $envType;
return 'production';
}

/**
Expand Down
14 changes: 7 additions & 7 deletions templates/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@

DEFAULT_ENV : {
/** Environment-aware settings. Be creative, but avoid having sensitive settings here. */
$defaultEnv = defined('WP_ENVIRONMENT_TYPE') ? WP_ENVIRONMENT_TYPE : WP_ENV;
switch ($defaultEnv) {
defined('WP_ENVIRONMENT_TYPE') or define('WP_ENVIRONMENT_TYPE', 'production');
switch (WP_ENVIRONMENT_TYPE) {
case 'local':
defined('WP_LOCAL_DEV') or define('WP_LOCAL_DEV', true);
case 'development':
Expand All @@ -191,10 +191,10 @@
defined('SCRIPT_DEBUG') or define('SCRIPT_DEBUG', false);
break;
}
$debugInfo['default-env-type'] = [
'label' => 'Env type for defaults',
'value' => $defaultEnv,
'debug' => $defaultEnv,
$debugInfo['wp-env-type'] = [
'label' => 'WordPress env type (used for defaults)',
'value' => WP_ENVIRONMENT_TYPE,
'debug' => WP_ENVIRONMENT_TYPE,
];
} #@@/DEFAULT_ENV

Expand Down Expand Up @@ -296,7 +296,7 @@ static function ($name) use ($envLoader) {
} #@@/BEFORE_BOOTSTRAP

CLEAN_UP : {
unset($debugInfo, $envType, $envLoader, $cacheEnv, $defaultEnv);
unset($debugInfo, $envType, $envLoader);
} #@@/CLEAN_UP

###################################################################################################
Expand Down
64 changes: 58 additions & 6 deletions tests/integration/Env/WordPressEnvBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,32 +565,84 @@ public function testLoadWpEnvironmentTypeFromWpEnvWithAlias()
* @test
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
*/
public function testLoadWpEnvironmentTypeFromWpEnvWhenContainingValue()
public function testSetDifferentEnvForWpAndWpStarter()
{
$_ENV['WP_ENVIRONMENT_TYPE'] = 'PREPROD-US';
$_ENV['WP_ENV'] = 'something_very_custom';
$_ENV['WP_ENVIRONMENT_TYPE'] = 'development';
$bridge = new WordPressEnvBridge();
$bridge->load('example.env', $this->fixturesPath());
$bridge->setupConstants();

static::assertSame('preprod-us', $bridge->determineEnvType());
static::assertTrue(defined('WP_ENV'));
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
static::assertSame('something_very_custom', WP_ENV);
static::assertSame('development', WP_ENVIRONMENT_TYPE);
}

/**
* @test
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
*/
public function testLoadWpEnvironmentTypeFromWpEnvWhenStartingWithValue()
{
$_ENV['WP_ENVIRONMENT_TYPE'] = 'PREPROD-US-1';
$bridge = new WordPressEnvBridge();
$bridge->load('example.env', $this->fixturesPath());
$bridge->setupConstants();

static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
static::assertTrue(defined('WP_ENV'));
static::assertSame('preprod-us-1', WP_ENV);
static::assertSame('preprod-us-1', $bridge->determineEnvType());
static::assertSame('staging', WP_ENVIRONMENT_TYPE);
}

/**
* @test
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
*/
public function testLoadWpEnvironmentTypeFromWpEnvWhenEndingWithValue()
{
$_ENV['WP_ENVIRONMENT_TYPE'] = 'My.Production';
$bridge = new WordPressEnvBridge();
$bridge->load('example.env', $this->fixturesPath());
$bridge->setupConstants();

static::assertSame('my.production', $bridge->determineEnvType());
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
static::assertSame('production', WP_ENVIRONMENT_TYPE);
}

/**
* @test
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
*/
public function testLoadWpEnvironmentTypeFromWpEnvWhenValueInTheMiddle()
{
$_ENV['WP_ENVIRONMENT_TYPE'] = 'my_dev_one';
$bridge = new WordPressEnvBridge();
$bridge->load('example.env', $this->fixturesPath());
$bridge->setupConstants();

static::assertSame('my_dev_one', $bridge->determineEnvType());
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
static::assertSame('development', WP_ENVIRONMENT_TYPE);
}

/**
* @test
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
*/
public function testCustomWpEnvironmentThatCantBeMapped()
{
$_ENV['WP_ENVIRONMENT_TYPE'] = 'THIS_CANT_BE_MAPPED';
$_ENV['WP_ENVIRONMENT_TYPE'] = 'my_devone';
$bridge = new WordPressEnvBridge();
$bridge->load('example.env', $this->fixturesPath());
$bridge->setupConstants();

static::assertSame('this_cant_be_mapped', $bridge->determineEnvType());
static::assertSame('my_devone', $bridge->determineEnvType());
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
static::assertSame('this_cant_be_mapped', WP_ENVIRONMENT_TYPE);
static::assertSame('production', WP_ENVIRONMENT_TYPE);
}

/**
Expand Down

0 comments on commit 874c80f

Please sign in to comment.