From 0c889a2848613e8c3d3c7529982d51318245195f Mon Sep 17 00:00:00 2001 From: Aaron Feledy Date: Sat, 1 Jul 2023 17:35:23 -0500 Subject: [PATCH 1/4] Add support for Platform.sh --- README.md | 1 + src/Environment.php | 2 + src/PlatformSh.php | 87 +++++++++++++++++++++++ tests/src/EnvironmentTest.php | 128 ++++++++++++++++++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 src/PlatformSh.php diff --git a/README.md b/README.md index d3cfc2a..10f92eb 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ use DrupalEnvironment\Environment; // These all return a boolean true/false Environment::isPantheon(); +Environment::isPlatformSh(); Environment::isAcquia(); Environment::isTugboat(); Environment::isGitHubWorkflow(); diff --git a/src/Environment.php b/src/Environment.php index f7feca0..3c8961d 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -8,6 +8,7 @@ * @method static string getEnvironment() * @method static bool isAcquia() * @method static bool isPantheon() + * @method static bool isPlatformSh() * @method static bool isProduction() * @method static bool isStaging() * @method static bool isDevelopment() @@ -28,6 +29,7 @@ class Environment public const CLASSES = [ 'isAcquia' => Acquia::class, 'isPantheon' => Pantheon::class, + 'isPlatformSh' => PlatformSh::class, 'isTugboat' => Tugboat::class, 'isGitHubWorkflow' => GitHubWorkflow::class, 'isGitLabCi' => GitLabCi::class, diff --git a/src/PlatformSh.php b/src/PlatformSh.php new file mode 100644 index 0000000..18847b0 --- /dev/null +++ b/src/PlatformSh.php @@ -0,0 +1,87 @@ + false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -99,6 +100,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => true, 'isStaging' => false, 'isDevelopment' => false, @@ -124,6 +126,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -145,6 +148,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => true, 'isStaging' => false, 'isDevelopment' => false, @@ -171,6 +175,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => true, 'isDevelopment' => false, @@ -197,6 +202,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => true, @@ -223,6 +229,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -249,6 +256,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -271,6 +279,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => true, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -285,6 +294,119 @@ public function providerEnvironment(): array ], ], ], + 'platformsh-prod' => [ + [ + 'PLATFORM_ENVIRONMENT' => 'main-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'production', + ], + [ + 'getEnvironment' => 'main-asdf123', + 'getEnvironmentType' => 'production', + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isPlatformSh' => true, + 'isProduction' => true, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => [ + 'name' => 'Production', + 'bg_color' => '#ffffff', + 'fg_color' => '#e7131a', + ], + ], + ], + 'platformsh-stage' => [ + [ + 'PLATFORM_ENVIRONMENT' => 'stage-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'staging', + ], + [ + 'getEnvironment' => 'stage-asdf123', + 'getEnvironmentType' => 'staging', + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isPlatformSh' => true, + 'isProduction' => false, + 'isStaging' => true, + 'isDevelopment' => false, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => [ + 'name' => 'Staging', + 'bg_color' => '#ffffff', + 'fg_color' => '#b85c00', + ], + ], + ], + 'platformsh-dev' => [ + [ + 'PLATFORM_ENVIRONMENT' => 'develop-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + ], + [ + 'getEnvironment' => 'develop-asdf123', + 'getEnvironmentType' => 'development', + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isPlatformSh' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => true, + 'isPreview' => false, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => [ + 'name' => 'Development', + 'bg_color' => '#ffffff', + 'fg_color' => '#307b24', + ], + ], + ], + 'platformsh-preview' => [ + [ + 'PLATFORM_ENVIRONMENT' => 'pr-225-asdf123', + 'PLATFORM_BRANCH' => 'pr-225', + 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + ], + [ + 'getEnvironment' => 'pr-225-asdf123', + 'getEnvironmentType' => 'development', + 'isAcquia' => false, + 'isCircleCi' => false, + 'isGitHubWorkflow' => false, + 'isGitLabCi' => false, + 'isTugboat' => false, + 'isPantheon' => false, + 'isPlatformSh' => true, + 'isProduction' => false, + 'isStaging' => false, + 'isDevelopment' => false, + 'isPreview' => true, + 'isCi' => false, + 'isLocal' => false, + 'getIndicatorConfig' => [ + 'name' => 'Preview', + 'bg_color' => '#ffffff', + 'fg_color' => '#990055', + ], + ], + ], 'tugboat' => [ [ 'TUGBOAT_PREVIEW_NAME' => 'phpunit', @@ -297,6 +419,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => true, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -323,6 +446,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -345,6 +469,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -367,6 +492,7 @@ public function providerEnvironment(): array 'isGitLabCi' => true, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -388,6 +514,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, @@ -413,6 +540,7 @@ public function providerEnvironment(): array 'isGitLabCi' => false, 'isTugboat' => false, 'isPantheon' => false, + 'isPlatformSh' => false, 'isProduction' => false, 'isStaging' => false, 'isDevelopment' => false, From 044dfba93ca64b7be3b558637c23cf5b67733389 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 27 Mar 2025 16:13:45 -0400 Subject: [PATCH 2/4] Update EnvironmentTest.php --- tests/src/EnvironmentTest.php | 42 +++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/tests/src/EnvironmentTest.php b/tests/src/EnvironmentTest.php index 94c3cad..d6b5ae7 100644 --- a/tests/src/EnvironmentTest.php +++ b/tests/src/EnvironmentTest.php @@ -359,8 +359,10 @@ public static function providerEnvironment(): array ], 'platformsh-prod' => [ [ - 'PLATFORM_ENVIRONMENT' => 'main-asdf123', - 'PLATFORM_ENVIRONMENT_TYPE' => 'production', + 'ENV' => [ + 'PLATFORM_ENVIRONMENT' => 'main-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'production', + ], ], [ 'getEnvironment' => 'main-asdf123', @@ -380,15 +382,17 @@ public static function providerEnvironment(): array 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Production', - 'bg_color' => '#ffffff', - 'fg_color' => '#e7131a', + 'bg_color' => '#e7131a', + 'fg_color' => '#ffffff', ], ], ], 'platformsh-stage' => [ [ - 'PLATFORM_ENVIRONMENT' => 'stage-asdf123', - 'PLATFORM_ENVIRONMENT_TYPE' => 'staging', + 'ENV' => [ + 'PLATFORM_ENVIRONMENT' => 'stage-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'staging', + ], ], [ 'getEnvironment' => 'stage-asdf123', @@ -408,15 +412,17 @@ public static function providerEnvironment(): array 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Staging', - 'bg_color' => '#ffffff', - 'fg_color' => '#b85c00', + 'bg_color' => '#b85c00', + 'fg_color' => '#ffffff', ], ], ], 'platformsh-dev' => [ [ - 'PLATFORM_ENVIRONMENT' => 'develop-asdf123', - 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + 'ENV' => [ + 'PLATFORM_ENVIRONMENT' => 'develop-asdf123', + 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + ], ], [ 'getEnvironment' => 'develop-asdf123', @@ -436,16 +442,18 @@ public static function providerEnvironment(): array 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Development', - 'bg_color' => '#ffffff', - 'fg_color' => '#307b24', + 'fg_color' => '#ffffff', + 'bg_color' => '#307b24', ], ], ], 'platformsh-preview' => [ [ - 'PLATFORM_ENVIRONMENT' => 'pr-225-asdf123', - 'PLATFORM_BRANCH' => 'pr-225', - 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + 'ENV' => [ + 'PLATFORM_ENVIRONMENT' => 'pr-225-asdf123', + 'PLATFORM_BRANCH' => 'pr-225', + 'PLATFORM_ENVIRONMENT_TYPE' => 'development', + ], ], [ 'getEnvironment' => 'pr-225-asdf123', @@ -465,8 +473,8 @@ public static function providerEnvironment(): array 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Preview', - 'bg_color' => '#ffffff', - 'fg_color' => '#990055', + 'bg_color' => '#990055', + 'fg_color' => '#ffffff', ], ], ], From 6a90981224a89394dbfac09fb8138b5739bbdea0 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 27 Mar 2025 16:14:36 -0400 Subject: [PATCH 3/4] Update EnvironmentTest.php --- tests/src/EnvironmentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/EnvironmentTest.php b/tests/src/EnvironmentTest.php index d6b5ae7..ae87244 100644 --- a/tests/src/EnvironmentTest.php +++ b/tests/src/EnvironmentTest.php @@ -442,8 +442,8 @@ public static function providerEnvironment(): array 'isLocal' => false, 'getIndicatorConfig' => [ 'name' => 'Development', - 'fg_color' => '#ffffff', 'bg_color' => '#307b24', + 'fg_color' => '#ffffff', ], ], ], From 207a6cfb0c9a3ac54dd501305ef7cb93c1187939 Mon Sep 17 00:00:00 2001 From: Aaron Feledy Date: Sat, 29 Mar 2025 23:36:00 -0500 Subject: [PATCH 4/4] Refactor PlatformSh to use getEnvironment() for environment type and add getEnvironmentName() method Update based on PR feedback to separate environment type and name, ensuring consistent behavior across platforms. - Renamed getEnvironmentType() to getEnvironment() in PlatformSh for consistency - Added getEnvironmentName() method to return user-defined environment name - Updated tests to reflect the new API design --- src/DefaultEnvironment.php | 15 +++++++++++++-- src/Environment.php | 1 + src/PlatformSh.php | 26 ++++++++++++++++---------- tests/src/EnvironmentTest.php | 16 ++++++++-------- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/DefaultEnvironment.php b/src/DefaultEnvironment.php index c75824b..ca7d8fd 100644 --- a/src/DefaultEnvironment.php +++ b/src/DefaultEnvironment.php @@ -68,17 +68,28 @@ public static function get(string $name) } /** - * Return the environment name. + * Return the environment type. * * For example: "local" or "ci" or "dev" or "prod". * * @return string|bool - * The name of the environment. + * The type of the environment. */ public static function getEnvironment(): string|bool { return static::get(static::ENVIRONMENT_NAME); } + + /** + * Return the user-defined environment name. + * + * @return string|bool + * The name of the environment. + */ + public static function getEnvironmentName(): string|bool + { + return static::getEnvironment(); + } /** * Determine if this is a production environment. diff --git a/src/Environment.php b/src/Environment.php index 935ebc6..6f487d9 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -7,6 +7,7 @@ * Helpers for working with the Drupal environment. * * @method static string getEnvironment() + * @method static string|bool getEnvironmentName() * @method static bool isAcquia() * @method static bool isPantheon() * @method static bool isPlatformSh() diff --git a/src/PlatformSh.php b/src/PlatformSh.php index 18847b0..923bd1a 100644 --- a/src/PlatformSh.php +++ b/src/PlatformSh.php @@ -40,24 +40,30 @@ class PlatformSh extends DefaultEnvironment public const DEVELOPMENT = 'development'; /** - * Return the environment type. - * - * For example: "local" or "development" or "production". - * - * @return string - * The type of the environment. + * {@inheritdoc} */ - public static function getEnvironmentType(): string + public static function getEnvironment(): string { return static::get(static::ENVIRONMENT_TYPE) ?: 'local'; } + /** + * Return the user-defined environment name. + * + * @return string|bool + * The name of the environment. + */ + public static function getEnvironmentName(): string|bool + { + return static::get(static::ENVIRONMENT_NAME); + } + /** * {@inheritdoc} */ public static function isProduction(): bool { - return static::getEnvironmentType() === static::PRODUCTION; + return static::getEnvironment() === static::PRODUCTION; } /** @@ -65,7 +71,7 @@ public static function isProduction(): bool */ public static function isStaging(): bool { - return static::getEnvironmentType() === static::STAGING; + return static::getEnvironment() === static::STAGING; } /** @@ -73,7 +79,7 @@ public static function isStaging(): bool */ public static function isDevelopment(): bool { - return static::getEnvironmentType() === static::DEVELOPMENT && !static::isPreview(); + return static::getEnvironment() === static::DEVELOPMENT && !static::isPreview(); } /** diff --git a/tests/src/EnvironmentTest.php b/tests/src/EnvironmentTest.php index ae87244..f7db443 100644 --- a/tests/src/EnvironmentTest.php +++ b/tests/src/EnvironmentTest.php @@ -365,8 +365,8 @@ public static function providerEnvironment(): array ], ], [ - 'getEnvironment' => 'main-asdf123', - 'getEnvironmentType' => 'production', + 'getEnvironment' => 'production', + 'getEnvironmentName' => 'main-asdf123', 'isAcquia' => false, 'isCircleCi' => false, 'isGitHubWorkflow' => false, @@ -395,8 +395,8 @@ public static function providerEnvironment(): array ], ], [ - 'getEnvironment' => 'stage-asdf123', - 'getEnvironmentType' => 'staging', + 'getEnvironment' => 'staging', + 'getEnvironmentName' => 'stage-asdf123', 'isAcquia' => false, 'isCircleCi' => false, 'isGitHubWorkflow' => false, @@ -425,8 +425,8 @@ public static function providerEnvironment(): array ], ], [ - 'getEnvironment' => 'develop-asdf123', - 'getEnvironmentType' => 'development', + 'getEnvironment' => 'development', + 'getEnvironmentName' => 'develop-asdf123', 'isAcquia' => false, 'isCircleCi' => false, 'isGitHubWorkflow' => false, @@ -456,8 +456,8 @@ public static function providerEnvironment(): array ], ], [ - 'getEnvironment' => 'pr-225-asdf123', - 'getEnvironmentType' => 'development', + 'getEnvironment' => 'development', + 'getEnvironmentName' => 'pr-225-asdf123', 'isAcquia' => false, 'isCircleCi' => false, 'isGitHubWorkflow' => false,