Skip to content

Commit be45ffb

Browse files
authored
Merge pull request #3 from davereid/refactor-class-names
Rename classes and namespace; Proper Tugboat support
2 parents 7eb1971 + 4b60b5d commit be45ffb

12 files changed

+344
-238
lines changed

.codeclimate.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
plugins:
2+
phpcodesniffer:
3+
enabled: true
4+
config:
5+
standard: "PSR1,PSR2"
6+
phpmd:
7+
enabled: false
18
exclude_patterns:
29
- "tests/"

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- run: composer diagnose || true
3737
- run: composer install
3838
- run: composer audit
39+
- run: composer run lint
3940
- run: composer run test
4041
- name: Test & publish code coverage
4142
if: ${{ matrix.php == '8.2' && matrix.os == 'ubuntu-latest' }}

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
"phpstan/extension-installer": "^1.3",
2828
"phpstan/phpstan-deprecation-rules": "^1.1",
2929
"roave/security-advisories": "dev-master",
30-
"symfony/phpunit-bridge": "^6.3"
30+
"symfony/phpunit-bridge": "^6.3",
31+
"squizlabs/php_codesniffer": "^3.7"
3132
},
3233
"autoload": {
3334
"psr-4": {
34-
"Davereid\\DrupalEnvironment\\": "src/"
35+
"DrupalEnvironment\\": "src/"
3536
}
3637
},
3738
"autoload-dev": {
3839
"psr-4": {
39-
"Davereid\\DrupalEnvironment\\Tests\\": "tests/src/"
40+
"DrupalEnvironment\\Tests\\": "tests/src/"
4041
}
4142
},
4243
"extra": {
@@ -45,10 +46,14 @@
4546
}
4647
},
4748
"scripts": {
49+
"lint": [
50+
"@phpcs",
51+
"@phpstan"
52+
],
4853
"test": [
49-
"@phpstan",
5054
"@phpunit"
5155
],
56+
"phpcs": "vendor/bin/phpcs --standard=PSR1,PSR2 src tests",
5257
"phpstan": "vendor/bin/phpstan analyse src tests --level 5",
5358
"phpunit": "vendor/bin/phpunit tests/src --verbose --coverage-text"
5459
},
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The Acquia environment specifics.
@@ -11,13 +11,14 @@
1111
* @todo Add isPreview() support.
1212
* @todo Should the 'ide' environment be detected as local?
1313
* @todo Add support for https://docs.acquia.com/ra/environment.html
14+
*
15+
* @internal
1416
*/
15-
class AcquiaEnvironment extends DefaultEnvironment
17+
class Acquia extends DefaultEnvironment
1618
{
1719

1820
/**
1921
* {@inheritdoc}
2022
*/
2123
public const ENVIRONMENT_NAME = 'AH_SITE_ENVIRONMENT';
22-
2324
}

src/CircleCiEnvironment.php renamed to src/CircleCi.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The CircleCI environment specifics.
77
*
88
* @see https://circleci.com/docs/variables/
9+
*
10+
* @internal
911
*/
10-
class CircleCiEnvironment extends DefaultEnvironment
12+
class CircleCi extends DefaultEnvironment
1113
{
1214

1315
/**
@@ -22,5 +24,4 @@ public static function getEnvironment(): string
2224
{
2325
return static::CI;
2426
}
25-
2627
}

src/DefaultEnvironment.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The standard environment.
7+
*
8+
* @internal
79
*/
810
class DefaultEnvironment
911
{
@@ -36,13 +38,34 @@ class DefaultEnvironment
3638
*/
3739
public const DEVELOPMENT = 'dev';
3840

41+
/**
42+
* The default preview environment name.
43+
*
44+
* @var string
45+
*/
46+
public const PREVIEW = 'preview';
47+
3948
/**
4049
* The default CI environment name.
4150
*
4251
* @var string
4352
*/
4453
public const CI = 'ci';
4554

55+
/**
56+
* Get an environment variable.
57+
*
58+
* @param string $name
59+
* The name of the environment variable to retrieve.
60+
*
61+
* @return mixed
62+
* The environment variable, if it's set.
63+
*/
64+
public static function get(string $name)
65+
{
66+
return Environment::get($name);
67+
}
68+
4669
/**
4770
* Return the environment name.
4871
*
@@ -53,7 +76,7 @@ class DefaultEnvironment
5376
*/
5477
public static function getEnvironment(): string
5578
{
56-
return Environment::get(static::ENVIRONMENT_NAME);
79+
return static::get(static::ENVIRONMENT_NAME);
5780
}
5881

5982
/**
@@ -97,7 +120,7 @@ public static function isDevelopment(): bool
97120
*/
98121
public static function isPreview(): bool
99122
{
100-
return Environment::isTugboat();
123+
return static::getEnvironment() === static::PREVIEW;
101124
}
102125

103126
/**
@@ -111,6 +134,17 @@ public static function isCi(): bool
111134
return static::getEnvironment() === static::CI;
112135
}
113136

137+
/**
138+
* Determine if this is a local environment.
139+
*
140+
* @return bool
141+
* TRUE if this is local.
142+
*/
143+
public static function isLocal(): bool
144+
{
145+
return Environment::isLocal();
146+
}
147+
114148
/**
115149
* Get the environment_indicator configuration. for this environment.
116150
*
@@ -119,7 +153,8 @@ public static function isCi(): bool
119153
*
120154
* @see https://architecture.lullabot.com/adr/20210609-environment-indicator/
121155
*/
122-
public static function getIndicatorConfig(): ?array {
156+
public static function getIndicatorConfig(): ?array
157+
{
123158
if (static::isProduction()) {
124159
return [
125160
'name' => 'Production',
@@ -148,7 +183,7 @@ public static function getIndicatorConfig(): ?array {
148183
'fg_color' => '#990055',
149184
];
150185
}
151-
if (Environment::isLocal()) {
186+
if (static::isLocal()) {
152187
return [
153188
'name' => 'Local',
154189
'bg_color' => '#ffffff',
@@ -157,7 +192,6 @@ public static function getIndicatorConfig(): ?array {
157192
}
158193

159194
// Unknown environment condition.
160-
return NULL;
195+
return null;
161196
}
162-
163197
}

src/Environment.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* Helpers for working with the Drupal environment.
77
*
8-
* @todo Add Platform.sh support
9-
*
108
* @method static string getEnvironment()
119
* @method static bool isAcquia()
1210
* @method static bool isPantheon()
1311
* @method static bool isProduction()
1412
* @method static bool isStaging()
1513
* @method static bool isDevelopment()
1614
* @method static bool isPreview()
15+
* @method static bool isTugboat()
1716
* @method static bool isCi()
1817
* @method static bool isGitHubWorkflow()
1918
* @method static bool isGitLabCi()
@@ -27,11 +26,12 @@ class Environment
2726
* The currently supported environment classes.
2827
*/
2928
public const CLASSES = [
30-
'isAcquia' => AcquiaEnvironment::class,
31-
'isPantheon' => PantheonEnvironment::class,
32-
'isGitHubWorkflow' => GitHubWorkflowEnvironment::class,
33-
'isGitLabCi' => GitLabCiEnvironment::class,
34-
'isCircleCi' => CircleCiEnvironment::class,
29+
'isAcquia' => Acquia::class,
30+
'isPantheon' => Pantheon::class,
31+
'isTugboat' => Tugboat::class,
32+
'isGitHubWorkflow' => GitHubWorkflow::class,
33+
'isGitLabCi' => GitLabCi::class,
34+
'isCircleCi' => CircleCi::class,
3535
null => DefaultEnvironment::class,
3636
];
3737

@@ -41,14 +41,13 @@ class Environment
4141
* @return string
4242
* The class name.
4343
*/
44-
public static function getEnvironmentClass()
44+
public static function getEnvironmentClass(): string
4545
{
4646
static $class;
4747
if (!isset($class)) {
4848
if ($class = static::get('DRUPAL_ENVIRONMENT_CLASS')) {
4949
// Do nothing. The class was assigned in the if.
50-
}
51-
else {
50+
} else {
5251
// Intentionally re-assigning the class variable here so that a match
5352
// breaks the foreach loop, or we fall back to the default class.
5453
foreach (static::CLASSES as $class) {
@@ -131,19 +130,6 @@ public static function isLando(): bool
131130
return (bool)static::get('LANDO_INFO');
132131
}
133132

134-
/**
135-
* Determine if this is a Tugboat environment.
136-
*
137-
* @return bool
138-
* TRUE if this is a Tugboat environment.
139-
*
140-
* @todo Should this be its own environment class?
141-
*/
142-
public static function isTugboat(): bool
143-
{
144-
return static::getEnvironment() === 'tugboat';
145-
}
146-
147133
/**
148134
* Determines whether the current request is a command-line one.
149135
*
@@ -190,8 +176,7 @@ public static function getComposerFilename(): string
190176
*/
191177
public static function getComposerLockFilename(): string
192178
{
193-
$composer_filename = static::getComposerFilename();
194-
return pathinfo($composer_filename, PATHINFO_FILENAME) . '.lock';
179+
$filename = static::getComposerFilename();
180+
return pathinfo($filename, PATHINFO_FILENAME) . '.lock';
195181
}
196-
197182
}

src/GitHubWorkflowEnvironment.php renamed to src/GitHubWorkflow.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The GitHub Workflow environment specifics.
77
*
88
* @see https://docs.github.com/en/actions/learn-github-actions/variables
9+
*
10+
* @internal
911
*/
10-
class GitHubWorkflowEnvironment extends DefaultEnvironment
12+
class GitHubWorkflow extends DefaultEnvironment
1113
{
1214

1315
/**
@@ -22,5 +24,4 @@ public static function getEnvironment(): string
2224
{
2325
return static::CI;
2426
}
25-
2627
}

src/GitLabCiEnvironment.php renamed to src/GitLabCi.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The GitlabCi environment specifics.
77
*
88
* @see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
9+
*
10+
* @internal
911
*/
10-
class GitLabCiEnvironment extends DefaultEnvironment
12+
class GitLabCi extends DefaultEnvironment
1113
{
1214

1315
/**
@@ -22,5 +24,4 @@ public static function getEnvironment(): string
2224
{
2325
return static::CI;
2426
}
25-
2627
}

src/PantheonEnvironment.php renamed to src/Pantheon.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3-
namespace Davereid\DrupalEnvironment;
3+
namespace DrupalEnvironment;
44

55
/**
66
* The Pantheon environment specifics.
77
*
88
* @see https://docs.pantheon.io/pantheon-workflow
99
* @see https://docs.pantheon.io/guides/multidev
10+
*
11+
* @internal
1012
*/
11-
class PantheonEnvironment extends DefaultEnvironment
13+
class Pantheon extends DefaultEnvironment
1214
{
1315

1416
/**
@@ -43,7 +45,6 @@ public static function isMultidev(): bool
4345
&& !static::isStaging()
4446
&& !static::isDevelopment()
4547
&& !static::isCi()
46-
&& !Environment::isLocal();
48+
&& !static::isLocal();
4749
}
48-
4950
}

0 commit comments

Comments
 (0)