diff --git a/docs/03-WordPress-Integration.md b/docs/03-WordPress-Integration.md index b5c8a51..0243faa 100644 --- a/docs/03-WordPress-Integration.md +++ b/docs/03-WordPress-Integration.md @@ -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. @@ -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`. diff --git a/src/Env/WordPressEnvBridge.php b/src/Env/WordPressEnvBridge.php index 782bb36..8423b10 100644 --- a/src/Env/WordPressEnvBridge.php +++ b/src/Env/WordPressEnvBridge.php @@ -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'; @@ -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'; } /** diff --git a/templates/wp-config.php b/templates/wp-config.php index 926f150..a8dc11f 100644 --- a/templates/wp-config.php +++ b/templates/wp-config.php @@ -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': @@ -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 @@ -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 ################################################################################################### diff --git a/tests/integration/Env/WordPressEnvBridgeTest.php b/tests/integration/Env/WordPressEnvBridgeTest.php index 6b00f8b..737a128 100644 --- a/tests/integration/Env/WordPressEnvBridgeTest.php +++ b/tests/integration/Env/WordPressEnvBridgeTest.php @@ -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); } /**