Skip to content

Add support for Platform.sh #4

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

Open
wants to merge 6 commits 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
15 changes: 13 additions & 2 deletions src/DefaultEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
* 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()
* @method static bool isProduction()
* @method static bool isStaging()
* @method static bool isDevelopment()
Expand All @@ -29,6 +31,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
93 changes: 93 additions & 0 deletions src/PlatformSh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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';

/**
* {@inheritdoc}
*/
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::getEnvironment() === static::PRODUCTION;
}

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

/**
* {@inheritdoc}
*/
public static function isDevelopment(): bool
{
return static::getEnvironment() === 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"));
}
}
136 changes: 136 additions & 0 deletions tests/src/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -131,6 +132,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => true,
'isStaging' => false,
'isDevelopment' => false,
Expand Down Expand Up @@ -158,6 +160,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -184,6 +187,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => true,
'isStaging' => false,
'isDevelopment' => false,
Expand Down Expand Up @@ -216,6 +220,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => true,
'isDevelopment' => false,
Expand Down Expand Up @@ -248,6 +253,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => true,
Expand Down Expand Up @@ -280,6 +286,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down Expand Up @@ -309,6 +316,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -334,6 +342,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => true,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -348,6 +357,127 @@ public static function providerEnvironment(): array
],
],
],
'platformsh-prod' => [
[
'ENV' => [
'PLATFORM_ENVIRONMENT' => 'main-asdf123',
'PLATFORM_ENVIRONMENT_TYPE' => 'production',
],
],
[
'getEnvironment' => 'production',
'getEnvironmentName' => 'main-asdf123',
'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' => '#e7131a',
'fg_color' => '#ffffff',
],
],
],
'platformsh-stage' => [
[
'ENV' => [
'PLATFORM_ENVIRONMENT' => 'stage-asdf123',
'PLATFORM_ENVIRONMENT_TYPE' => 'staging',
],
],
[
'getEnvironment' => 'staging',
'getEnvironmentName' => 'stage-asdf123',
'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' => '#b85c00',
'fg_color' => '#ffffff',
],
],
],
'platformsh-dev' => [
[
'ENV' => [
'PLATFORM_ENVIRONMENT' => 'develop-asdf123',
'PLATFORM_ENVIRONMENT_TYPE' => 'development',
],
],
[
'getEnvironment' => 'development',
'getEnvironmentName' => 'develop-asdf123',
'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' => '#307b24',
'fg_color' => '#ffffff',
],
],
],
'platformsh-preview' => [
[
'ENV' => [
'PLATFORM_ENVIRONMENT' => 'pr-225-asdf123',
'PLATFORM_BRANCH' => 'pr-225',
'PLATFORM_ENVIRONMENT_TYPE' => 'development',
],
],
[
'getEnvironment' => 'development',
'getEnvironmentName' => 'pr-225-asdf123',
'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' => '#990055',
'fg_color' => '#ffffff',
],
],
],
'tugboat' => [
[
'ENV' => [
Expand All @@ -362,6 +492,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => true,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down Expand Up @@ -390,6 +521,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -414,6 +546,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -438,6 +571,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => true,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand All @@ -461,6 +595,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down Expand Up @@ -488,6 +623,7 @@ public static function providerEnvironment(): array
'isGitLabCi' => false,
'isTugboat' => false,
'isPantheon' => false,
'isPlatformSh' => false,
'isProduction' => false,
'isStaging' => false,
'isDevelopment' => false,
Expand Down