Skip to content

Commit

Permalink
Merge pull request #3 from davereid/refactor-class-names
Browse files Browse the repository at this point in the history
Rename classes and namespace; Proper Tugboat support
  • Loading branch information
davereid authored Jun 27, 2023
2 parents 7eb1971 + 4b60b5d commit be45ffb
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 238 deletions.
7 changes: 7 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
plugins:
phpcodesniffer:
enabled: true
config:
standard: "PSR1,PSR2"
phpmd:
enabled: false
exclude_patterns:
- "tests/"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- run: composer diagnose || true
- run: composer install
- run: composer audit
- run: composer run lint
- run: composer run test
- name: Test & publish code coverage
if: ${{ matrix.php == '8.2' && matrix.os == 'ubuntu-latest' }}
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"roave/security-advisories": "dev-master",
"symfony/phpunit-bridge": "^6.3"
"symfony/phpunit-bridge": "^6.3",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
"Davereid\\DrupalEnvironment\\": "src/"
"DrupalEnvironment\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Davereid\\DrupalEnvironment\\Tests\\": "tests/src/"
"DrupalEnvironment\\Tests\\": "tests/src/"
}
},
"extra": {
Expand All @@ -45,10 +46,14 @@
}
},
"scripts": {
"lint": [
"@phpcs",
"@phpstan"
],
"test": [
"@phpstan",
"@phpunit"
],
"phpcs": "vendor/bin/phpcs --standard=PSR1,PSR2 src tests",
"phpstan": "vendor/bin/phpstan analyse src tests --level 5",
"phpunit": "vendor/bin/phpunit tests/src --verbose --coverage-text"
},
Expand Down
7 changes: 4 additions & 3 deletions src/AcquiaEnvironment.php → src/Acquia.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The Acquia environment specifics.
Expand All @@ -11,13 +11,14 @@
* @todo Add isPreview() support.
* @todo Should the 'ide' environment be detected as local?
* @todo Add support for https://docs.acquia.com/ra/environment.html
*
* @internal
*/
class AcquiaEnvironment extends DefaultEnvironment
class Acquia extends DefaultEnvironment
{

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

}
7 changes: 4 additions & 3 deletions src/CircleCiEnvironment.php → src/CircleCi.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The CircleCI environment specifics.
*
* @see https://circleci.com/docs/variables/
*
* @internal
*/
class CircleCiEnvironment extends DefaultEnvironment
class CircleCi extends DefaultEnvironment
{

/**
Expand All @@ -22,5 +24,4 @@ public static function getEnvironment(): string
{
return static::CI;
}

}
48 changes: 41 additions & 7 deletions src/DefaultEnvironment.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The standard environment.
*
* @internal
*/
class DefaultEnvironment
{
Expand Down Expand Up @@ -36,13 +38,34 @@ class DefaultEnvironment
*/
public const DEVELOPMENT = 'dev';

/**
* The default preview environment name.
*
* @var string
*/
public const PREVIEW = 'preview';

/**
* The default CI environment name.
*
* @var string
*/
public const CI = 'ci';

/**
* Get an environment variable.
*
* @param string $name
* The name of the environment variable to retrieve.
*
* @return mixed
* The environment variable, if it's set.
*/
public static function get(string $name)
{
return Environment::get($name);
}

/**
* Return the environment name.
*
Expand All @@ -53,7 +76,7 @@ class DefaultEnvironment
*/
public static function getEnvironment(): string
{
return Environment::get(static::ENVIRONMENT_NAME);
return static::get(static::ENVIRONMENT_NAME);
}

/**
Expand Down Expand Up @@ -97,7 +120,7 @@ public static function isDevelopment(): bool
*/
public static function isPreview(): bool
{
return Environment::isTugboat();
return static::getEnvironment() === static::PREVIEW;
}

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

/**
* Determine if this is a local environment.
*
* @return bool
* TRUE if this is local.
*/
public static function isLocal(): bool
{
return Environment::isLocal();
}

/**
* Get the environment_indicator configuration. for this environment.
*
Expand All @@ -119,7 +153,8 @@ public static function isCi(): bool
*
* @see https://architecture.lullabot.com/adr/20210609-environment-indicator/
*/
public static function getIndicatorConfig(): ?array {
public static function getIndicatorConfig(): ?array
{
if (static::isProduction()) {
return [
'name' => 'Production',
Expand Down Expand Up @@ -148,7 +183,7 @@ public static function getIndicatorConfig(): ?array {
'fg_color' => '#990055',
];
}
if (Environment::isLocal()) {
if (static::isLocal()) {
return [
'name' => 'Local',
'bg_color' => '#ffffff',
Expand All @@ -157,7 +192,6 @@ public static function getIndicatorConfig(): ?array {
}

// Unknown environment condition.
return NULL;
return null;
}

}
39 changes: 12 additions & 27 deletions src/Environment.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* Helpers for working with the Drupal environment.
*
* @todo Add Platform.sh support
*
* @method static string getEnvironment()
* @method static bool isAcquia()
* @method static bool isPantheon()
* @method static bool isProduction()
* @method static bool isStaging()
* @method static bool isDevelopment()
* @method static bool isPreview()
* @method static bool isTugboat()
* @method static bool isCi()
* @method static bool isGitHubWorkflow()
* @method static bool isGitLabCi()
Expand All @@ -27,11 +26,12 @@ class Environment
* The currently supported environment classes.
*/
public const CLASSES = [
'isAcquia' => AcquiaEnvironment::class,
'isPantheon' => PantheonEnvironment::class,
'isGitHubWorkflow' => GitHubWorkflowEnvironment::class,
'isGitLabCi' => GitLabCiEnvironment::class,
'isCircleCi' => CircleCiEnvironment::class,
'isAcquia' => Acquia::class,
'isPantheon' => Pantheon::class,
'isTugboat' => Tugboat::class,
'isGitHubWorkflow' => GitHubWorkflow::class,
'isGitLabCi' => GitLabCi::class,
'isCircleCi' => CircleCi::class,
null => DefaultEnvironment::class,
];

Expand All @@ -41,14 +41,13 @@ class Environment
* @return string
* The class name.
*/
public static function getEnvironmentClass()
public static function getEnvironmentClass(): string
{
static $class;
if (!isset($class)) {
if ($class = static::get('DRUPAL_ENVIRONMENT_CLASS')) {
// Do nothing. The class was assigned in the if.
}
else {
} else {
// Intentionally re-assigning the class variable here so that a match
// breaks the foreach loop, or we fall back to the default class.
foreach (static::CLASSES as $class) {
Expand Down Expand Up @@ -131,19 +130,6 @@ public static function isLando(): bool
return (bool)static::get('LANDO_INFO');
}

/**
* Determine if this is a Tugboat environment.
*
* @return bool
* TRUE if this is a Tugboat environment.
*
* @todo Should this be its own environment class?
*/
public static function isTugboat(): bool
{
return static::getEnvironment() === 'tugboat';
}

/**
* Determines whether the current request is a command-line one.
*
Expand Down Expand Up @@ -190,8 +176,7 @@ public static function getComposerFilename(): string
*/
public static function getComposerLockFilename(): string
{
$composer_filename = static::getComposerFilename();
return pathinfo($composer_filename, PATHINFO_FILENAME) . '.lock';
$filename = static::getComposerFilename();
return pathinfo($filename, PATHINFO_FILENAME) . '.lock';
}

}
7 changes: 4 additions & 3 deletions src/GitHubWorkflowEnvironment.php → src/GitHubWorkflow.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The GitHub Workflow environment specifics.
*
* @see https://docs.github.com/en/actions/learn-github-actions/variables
*
* @internal
*/
class GitHubWorkflowEnvironment extends DefaultEnvironment
class GitHubWorkflow extends DefaultEnvironment
{

/**
Expand All @@ -22,5 +24,4 @@ public static function getEnvironment(): string
{
return static::CI;
}

}
7 changes: 4 additions & 3 deletions src/GitLabCiEnvironment.php → src/GitLabCi.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The GitlabCi environment specifics.
*
* @see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
*
* @internal
*/
class GitLabCiEnvironment extends DefaultEnvironment
class GitLabCi extends DefaultEnvironment
{

/**
Expand All @@ -22,5 +24,4 @@ public static function getEnvironment(): string
{
return static::CI;
}

}
9 changes: 5 additions & 4 deletions src/PantheonEnvironment.php → src/Pantheon.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

namespace Davereid\DrupalEnvironment;
namespace DrupalEnvironment;

/**
* The Pantheon environment specifics.
*
* @see https://docs.pantheon.io/pantheon-workflow
* @see https://docs.pantheon.io/guides/multidev
*
* @internal
*/
class PantheonEnvironment extends DefaultEnvironment
class Pantheon extends DefaultEnvironment
{

/**
Expand Down Expand Up @@ -43,7 +45,6 @@ public static function isMultidev(): bool
&& !static::isStaging()
&& !static::isDevelopment()
&& !static::isCi()
&& !Environment::isLocal();
&& !static::isLocal();
}

}
Loading

0 comments on commit be45ffb

Please sign in to comment.