Skip to content

Commit

Permalink
Add support for Platform.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeledy committed Jul 1, 2023
1 parent 3421f58 commit b833432
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use DrupalEnvironment\Environment;

// These all return a boolean true/false
Environment::isPantheon();
Environment::isPlatformSh();
Environment::isAcquia();
Environment::isTugboat();
Environment::isGitHubWorkflow();
Expand Down
2 changes: 2 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
87 changes: 87 additions & 0 deletions src/PlatformSh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace DrupalEnvironment;

/**
* The Platform.sh environment specifics.
*
* @see https://docs.platform.sh/environments.html
*
* @internal
*/
class PlatformSh extends DefaultEnvironment
{

/**
* The default environment variable name.
*
* @var string
*/
public const ENVIRONMENT_TYPE = 'PLATFORM_ENVIRONMENT_TYPE';

/**
* {@inheritdoc}
*/
public const ENVIRONMENT_NAME = 'PLATFORM_ENVIRONMENT';

/**
* {@inheritdoc}
*/
public const PRODUCTION = 'production';

/**
* {@inheritdoc}
*/
public const STAGING = 'staging';

/**
* {@inheritdoc}
*/
public const DEVELOPMENT = 'development';

/**
* Return the environment type.
*
* For example: "local" or "development" or "production".
*
* @return string
* The type of the environment.
*/
public static function getEnvironmentType(): string
{
return static::get(static::ENVIRONMENT_TYPE) || 'local';
}

/**
* {@inheritdoc}
*/
public static function isProduction(): bool
{
return static::getEnvironmentType() === static::PRODUCTION;
}

/**
* {@inheritdoc}
*/
public static function isStaging(): bool
{
return static::getEnvironmentType() === static::STAGING;
}

/**
* {@inheritdoc}
*/
public static function isDevelopment(): bool
{
return static::getEnvironmentType() === static::DEVELOPMENT;
}

/**
* {@inheritdoc}
*/
public static function isPreview(): bool
{
// If the branch looks like "pr-123" then it's a pull request environment.
return preg_match('/^pr-\d+$/', static::get("PLATFORM_BRANCH"));
}
}
155 changes: 155 additions & 0 deletions tests/src/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -99,6 +100,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => true,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -124,6 +126,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -145,6 +148,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => true,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -171,6 +175,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => true,
'isDevelopment' => false,
Expand All @@ -197,6 +202,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => true,
Expand All @@ -223,6 +229,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -249,6 +256,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -271,6 +279,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -285,6 +294,146 @@ 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',
],
],
],
'platformsh-local' => [
[
'PLATFORM_ENVIRONMENT' => 'lando',
],
[
'getEnvironment' => 'lando',
'getEnvironmentType' => 'local',
'isAcquia' => false,
'isCircleCi' => false,
'isGitHubWorkflow' => false,
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => true,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
'isPreview' => false,
'isCi' => false,
'isLocal' => true,
'getIndicatorConfig' => [
'name' => 'Local',
'bg_color' => '#ffffff',
'fg_color' => '#505050',
],
],
],
'tugboat' => [
[
'TUGBOAT_PREVIEW_NAME' => 'phpunit',
Expand All @@ -297,6 +446,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => true,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -323,6 +473,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -345,6 +496,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -367,6 +519,7 @@ public function providerEnvironment(): array
'isGitLabCi' => true,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -388,6 +541,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -413,6 +567,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down

0 comments on commit b833432

Please sign in to comment.