Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Platform.sh #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 && !static::isPreview();
}

/**
* {@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"));
}
}
128 changes: 128 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,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',
Expand All @@ -297,6 +419,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => true,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -323,6 +446,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -345,6 +469,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -367,6 +492,7 @@ public function providerEnvironment(): array
'isGitLabCi' => true,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -388,6 +514,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -413,6 +540,7 @@ public function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down