Skip to content

Commit b793418

Browse files
committed
Add support for Platform.sh
1 parent 3421f58 commit b793418

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use DrupalEnvironment\Environment;
3030

3131
// These all return a boolean true/false
3232
Environment::isPantheon();
33+
Environment::isPlatformSh();
3334
Environment::isAcquia();
3435
Environment::isTugboat();
3536
Environment::isGitHubWorkflow();

src/Environment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @method static string getEnvironment()
99
* @method static bool isAcquia()
1010
* @method static bool isPantheon()
11+
* @method static bool isPlatformSh()
1112
* @method static bool isProduction()
1213
* @method static bool isStaging()
1314
* @method static bool isDevelopment()
@@ -28,6 +29,7 @@ class Environment
2829
public const CLASSES = [
2930
'isAcquia' => Acquia::class,
3031
'isPantheon' => Pantheon::class,
32+
'isPlatformSh' => PlatformSh::class,
3133
'isTugboat' => Tugboat::class,
3234
'isGitHubWorkflow' => GitHubWorkflow::class,
3335
'isGitLabCi' => GitLabCi::class,

src/PlatformSh.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace DrupalEnvironment;
4+
5+
/**
6+
* The Platform.sh environment specifics.
7+
*
8+
* @see https://docs.platform.sh/environments.html
9+
*
10+
* @internal
11+
*/
12+
class PlatformSh extends DefaultEnvironment
13+
{
14+
15+
/**
16+
* The default environment variable name.
17+
*
18+
* @var string
19+
*/
20+
public const ENVIRONMENT_TYPE = 'PLATFORM_ENVIRONMENT_TYPE';
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public const ENVIRONMENT_NAME = 'PLATFORM_ENVIRONMENT';
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public const PRODUCTION = 'production';
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public const STAGING = 'staging';
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public const DEVELOPMENT = 'development';
41+
42+
/**
43+
* Return the environment type.
44+
*
45+
* For example: "local" or "development" or "production".
46+
*
47+
* @return string
48+
* The type of the environment.
49+
*/
50+
public static function getEnvironmentType(): string
51+
{
52+
return static::get(static::ENVIRONMENT_TYPE) ?: 'local';
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public static function isProduction(): bool
59+
{
60+
return static::getEnvironmentType() === static::PRODUCTION;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public static function isStaging(): bool
67+
{
68+
return static::getEnvironmentType() === static::STAGING;
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public static function isDevelopment(): bool
75+
{
76+
return static::getEnvironmentType() === static::DEVELOPMENT;
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public static function isPreview(): bool
83+
{
84+
// If the branch looks like "pr-123" then it's a pull request environment.
85+
return preg_match('/^pr-\d+$/', static::get("PLATFORM_BRANCH"));
86+
}
87+
}

tests/src/EnvironmentTest.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function providerEnvironment(): array
7575
'isGitLabCi' => false,
7676
'isTugboat' => false,
7777
'isPantheon' => false,
78+
'isPlatformSh' => false,
7879
'isProduction' => false,
7980
'isStaging' => false,
8081
'isDevelopment' => false,
@@ -99,6 +100,7 @@ public function providerEnvironment(): array
99100
'isGitLabCi' => false,
100101
'isTugboat' => false,
101102
'isPantheon' => false,
103+
'isPlatformSh' => false,
102104
'isProduction' => true,
103105
'isStaging' => false,
104106
'isDevelopment' => false,
@@ -124,6 +126,7 @@ public function providerEnvironment(): array
124126
'isGitLabCi' => false,
125127
'isTugboat' => false,
126128
'isPantheon' => false,
129+
'isPlatformSh' => false,
127130
'isProduction' => false,
128131
'isStaging' => false,
129132
'isDevelopment' => false,
@@ -145,6 +148,7 @@ public function providerEnvironment(): array
145148
'isGitLabCi' => false,
146149
'isTugboat' => false,
147150
'isPantheon' => true,
151+
'isPlatformSh' => false,
148152
'isProduction' => true,
149153
'isStaging' => false,
150154
'isDevelopment' => false,
@@ -171,6 +175,7 @@ public function providerEnvironment(): array
171175
'isGitLabCi' => false,
172176
'isTugboat' => false,
173177
'isPantheon' => true,
178+
'isPlatformSh' => false,
174179
'isProduction' => false,
175180
'isStaging' => true,
176181
'isDevelopment' => false,
@@ -197,6 +202,7 @@ public function providerEnvironment(): array
197202
'isGitLabCi' => false,
198203
'isTugboat' => false,
199204
'isPantheon' => true,
205+
'isPlatformSh' => false,
200206
'isProduction' => false,
201207
'isStaging' => false,
202208
'isDevelopment' => true,
@@ -223,6 +229,7 @@ public function providerEnvironment(): array
223229
'isGitLabCi' => false,
224230
'isTugboat' => false,
225231
'isPantheon' => true,
232+
'isPlatformSh' => false,
226233
'isProduction' => false,
227234
'isStaging' => false,
228235
'isDevelopment' => false,
@@ -249,6 +256,7 @@ public function providerEnvironment(): array
249256
'isGitLabCi' => false,
250257
'isTugboat' => false,
251258
'isPantheon' => true,
259+
'isPlatformSh' => false,
252260
'isProduction' => false,
253261
'isStaging' => false,
254262
'isDevelopment' => false,
@@ -271,6 +279,7 @@ public function providerEnvironment(): array
271279
'isGitLabCi' => false,
272280
'isTugboat' => false,
273281
'isPantheon' => true,
282+
'isPlatformSh' => false,
274283
'isProduction' => false,
275284
'isStaging' => false,
276285
'isDevelopment' => false,
@@ -285,6 +294,146 @@ public function providerEnvironment(): array
285294
],
286295
],
287296
],
297+
'platformsh-prod' => [
298+
[
299+
'PLATFORM_ENVIRONMENT' => 'main-asdf123',
300+
'PLATFORM_ENVIRONMENT_TYPE' => 'production',
301+
],
302+
[
303+
'getEnvironment' => 'main-asdf123',
304+
'getEnvironmentType' => 'production',
305+
'isAcquia' => false,
306+
'isCircleCi' => false,
307+
'isGitHubWorkflow' => false,
308+
'isGitLabCi' => false,
309+
'isTugboat' => false,
310+
'isPantheon' => false,
311+
'isPlatformSh' => true,
312+
'isProduction' => true,
313+
'isStaging' => false,
314+
'isDevelopment' => false,
315+
'isPreview' => false,
316+
'isCi' => false,
317+
'isLocal' => false,
318+
'getIndicatorConfig' => [
319+
'name' => 'Production',
320+
'bg_color' => '#ffffff',
321+
'fg_color' => '#e7131a',
322+
],
323+
],
324+
],
325+
'platformsh-stage' => [
326+
[
327+
'PLATFORM_ENVIRONMENT' => 'stage-asdf123',
328+
'PLATFORM_ENVIRONMENT_TYPE' => 'staging',
329+
],
330+
[
331+
'getEnvironment' => 'stage-asdf123',
332+
'getEnvironmentType' => 'staging',
333+
'isAcquia' => false,
334+
'isCircleCi' => false,
335+
'isGitHubWorkflow' => false,
336+
'isGitLabCi' => false,
337+
'isTugboat' => false,
338+
'isPantheon' => false,
339+
'isPlatformSh' => true,
340+
'isProduction' => false,
341+
'isStaging' => true,
342+
'isDevelopment' => false,
343+
'isPreview' => false,
344+
'isCi' => false,
345+
'isLocal' => false,
346+
'getIndicatorConfig' => [
347+
'name' => 'Staging',
348+
'bg_color' => '#ffffff',
349+
'fg_color' => '#b85c00',
350+
],
351+
],
352+
],
353+
'platformsh-dev' => [
354+
[
355+
'PLATFORM_ENVIRONMENT' => 'develop-asdf123',
356+
'PLATFORM_ENVIRONMENT_TYPE' => 'development',
357+
],
358+
[
359+
'getEnvironment' => 'develop-asdf123',
360+
'getEnvironmentType' => 'development',
361+
'isAcquia' => false,
362+
'isCircleCi' => false,
363+
'isGitHubWorkflow' => false,
364+
'isGitLabCi' => false,
365+
'isTugboat' => false,
366+
'isPantheon' => false,
367+
'isPlatformSh' => true,
368+
'isProduction' => false,
369+
'isStaging' => false,
370+
'isDevelopment' => true,
371+
'isPreview' => false,
372+
'isCi' => false,
373+
'isLocal' => false,
374+
'getIndicatorConfig' => [
375+
'name' => 'Development',
376+
'bg_color' => '#ffffff',
377+
'fg_color' => '#307b24',
378+
],
379+
],
380+
],
381+
'platformsh-preview' => [
382+
[
383+
'PLATFORM_ENVIRONMENT' => 'pr-225-asdf123',
384+
'PLATFORM_BRANCH' => 'pr-225',
385+
'PLATFORM_ENVIRONMENT_TYPE' => 'development',
386+
],
387+
[
388+
'getEnvironment' => 'pr-225-asdf123',
389+
'getEnvironmentType' => 'development',
390+
'isAcquia' => false,
391+
'isCircleCi' => false,
392+
'isGitHubWorkflow' => false,
393+
'isGitLabCi' => false,
394+
'isTugboat' => false,
395+
'isPantheon' => false,
396+
'isPlatformSh' => true,
397+
'isProduction' => false,
398+
'isStaging' => false,
399+
'isDevelopment' => false,
400+
'isPreview' => true,
401+
'isCi' => false,
402+
'isLocal' => false,
403+
'getIndicatorConfig' => [
404+
'name' => 'Preview',
405+
'bg_color' => '#ffffff',
406+
'fg_color' => '#990055',
407+
],
408+
],
409+
],
410+
'platformsh-local' => [
411+
[
412+
'PLATFORM_ENVIRONMENT' => 'lando',
413+
],
414+
[
415+
'getEnvironment' => 'lando',
416+
'getEnvironmentType' => 'local',
417+
'isAcquia' => false,
418+
'isCircleCi' => false,
419+
'isGitHubWorkflow' => false,
420+
'isGitLabCi' => false,
421+
'isTugboat' => false,
422+
'isPantheon' => false,
423+
'isPlatformSh' => true,
424+
'isProduction' => false,
425+
'isStaging' => false,
426+
'isDevelopment' => false,
427+
'isPreview' => false,
428+
'isCi' => false,
429+
'isLocal' => true,
430+
'getIndicatorConfig' => [
431+
'name' => 'Local',
432+
'bg_color' => '#ffffff',
433+
'fg_color' => '#505050',
434+
],
435+
],
436+
],
288437
'tugboat' => [
289438
[
290439
'TUGBOAT_PREVIEW_NAME' => 'phpunit',
@@ -297,6 +446,7 @@ public function providerEnvironment(): array
297446
'isGitLabCi' => false,
298447
'isTugboat' => true,
299448
'isPantheon' => false,
449+
'isPlatformSh' => false,
300450
'isProduction' => false,
301451
'isStaging' => false,
302452
'isDevelopment' => false,
@@ -323,6 +473,7 @@ public function providerEnvironment(): array
323473
'isGitLabCi' => false,
324474
'isTugboat' => false,
325475
'isPantheon' => false,
476+
'isPlatformSh' => false,
326477
'isProduction' => false,
327478
'isStaging' => false,
328479
'isDevelopment' => false,
@@ -345,6 +496,7 @@ public function providerEnvironment(): array
345496
'isGitLabCi' => false,
346497
'isTugboat' => false,
347498
'isPantheon' => false,
499+
'isPlatformSh' => false,
348500
'isProduction' => false,
349501
'isStaging' => false,
350502
'isDevelopment' => false,
@@ -367,6 +519,7 @@ public function providerEnvironment(): array
367519
'isGitLabCi' => true,
368520
'isTugboat' => false,
369521
'isPantheon' => false,
522+
'isPlatformSh' => false,
370523
'isProduction' => false,
371524
'isStaging' => false,
372525
'isDevelopment' => false,
@@ -388,6 +541,7 @@ public function providerEnvironment(): array
388541
'isGitLabCi' => false,
389542
'isTugboat' => false,
390543
'isPantheon' => false,
544+
'isPlatformSh' => false,
391545
'isProduction' => false,
392546
'isStaging' => false,
393547
'isDevelopment' => false,
@@ -413,6 +567,7 @@ public function providerEnvironment(): array
413567
'isGitLabCi' => false,
414568
'isTugboat' => false,
415569
'isPantheon' => false,
570+
'isPlatformSh' => false,
416571
'isProduction' => false,
417572
'isStaging' => false,
418573
'isDevelopment' => false,

0 commit comments

Comments
 (0)