From 88b6b2548e3a45922e1c54fbeae3aa7a943e9299 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:12:01 +0200 Subject: [PATCH 1/6] Added service to determine currently active project roles, such as "core" instance --- documentation/environment-resolver.md | 29 +++++++++++ helfi_api_base.services.yml | 1 + src/Environment/ActiveProjectRoles.php | 52 +++++++++++++++++++ src/Environment/EnvironmentTrait.php | 2 + src/Environment/ProjectRoleEnum.php | 12 +++++ .../Environment/ActiveProjectRolesTest.php | 47 +++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 src/Environment/ActiveProjectRoles.php create mode 100644 src/Environment/ProjectRoleEnum.php create mode 100644 tests/src/Unit/Environment/ActiveProjectRolesTest.php diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index 3caf4b6..755aec6 100644 --- a/documentation/environment-resolver.md +++ b/documentation/environment-resolver.md @@ -52,3 +52,32 @@ $environment = $service->getActiveEnvironment(); // Failure will throw an InvalidArgumentException. $project = $service->getActiveProject(); ``` + +### Active project roles + +This can be used to determine what "roles" the currently active project has. + +See [\Drupal\helfi_api_base\Environment\ProjectRoleEnum](src/Environment/ProjectRolesEnum.php) for available roles. + +```php +/** @var \Drupal\helfi_api_base\Environment\ActiveProjectRoles $activeProjectRoles */ +$activeProjectRoles = \Drupal::service(\Drupal\helfi_api_base\Environment\ActiveProjectRoles::class); +// A boolean indicating whether the current instance has the given role. +$activeProjectRoles->hasRole(\Drupal\helfi_api_base\Environment\ProjectRoleEnum::Core); +``` + +#### Determine if the current instance is a "core" instance + +The instance is considered as "core" instance if the instance is a part of the main-navigation structure. Currently, this includes all Drupal instances under www.hel.fi domain. + +This can be used to conditionally run code only in core instances, such as enabling a module. + +```php +/** @var \Drupal\helfi_api_base\Environment\ActiveProjectRoles $activeProjectRoles */ +$activeProjectRoles = \Drupal::service(\Drupal\helfi_api_base\Environment\ActiveProjectRoles::class); + +if ($activeProjectRoles->hasRole(\Drupal\helfi_api_base\Environment\ProjectRoleEnum::Core)) { + // Do something only on "core" instances. +} +``` + diff --git a/helfi_api_base.services.yml b/helfi_api_base.services.yml index ad4d726..3842e57 100644 --- a/helfi_api_base.services.yml +++ b/helfi_api_base.services.yml @@ -28,6 +28,7 @@ services: Drupal\helfi_api_base\EventSubscriber\DisableUserPasswordSubscriber: ~ Drupal\helfi_api_base\Features\FeatureManager: ~ + Drupal\helfi_api_base\Environment\ActiveProjectRoles: ~ Drupal\helfi_api_base\Environment\EnvironmentResolverInterface: '@helfi_api_base.environment_resolver' Drupal\helfi_api_base\Environment\EnvironmentResolver: '@helfi_api_base.environment_resolver' helfi_api_base.environment_resolver: diff --git a/src/Environment/ActiveProjectRoles.php b/src/Environment/ActiveProjectRoles.php new file mode 100644 index 0000000..de18b39 --- /dev/null +++ b/src/Environment/ActiveProjectRoles.php @@ -0,0 +1,52 @@ +getRoles()); + } + + /** + * Gets the project features. + * + * @return array<\Drupal\helfi_api_base\Environment\ProjectRoleEnum> + * The features. + */ + private function getRoles(): array { + $roles = []; + + try { + // Instance is considered as a "core" if it's included in main-navigation + // structure, so basically all Drupal instances under www.hel.fi domain. + // + // Currently, only core instances are defined in EnvironmentResolver, so + // we can use ::getActiveProject() to determine if the current instance is + // a core instance. + // @todo Include all instances in EnvironmentResolver and include + // Project role data in Project object. + $this->environmentResolver->getActiveProject(); + + $roles[] = ProjectRoleEnum::Core; + } + catch (\InvalidArgumentException) { + } + return $roles; + } + +} diff --git a/src/Environment/EnvironmentTrait.php b/src/Environment/EnvironmentTrait.php index cc6fd42..4ecb690 100644 --- a/src/Environment/EnvironmentTrait.php +++ b/src/Environment/EnvironmentTrait.php @@ -28,6 +28,8 @@ protected function normalizeEnvironmentName(string $environment) : ? string { 'testing' => 'test', 'staging' => 'stage', 'production' => 'prod', + // Make sure CI resolves to a known environment. + 'ci' => 'test', ]; if (array_key_exists($environment, $environments)) { diff --git a/src/Environment/ProjectRoleEnum.php b/src/Environment/ProjectRoleEnum.php new file mode 100644 index 0000000..0a846b7 --- /dev/null +++ b/src/Environment/ProjectRoleEnum.php @@ -0,0 +1,12 @@ +getEnvironmentResolver($projectName, $env)); + $this->assertEquals($expected, $sut->hasRole(ProjectRoleEnum::Core)); + } + + /** + * A data provider. + * + * @return array[] + * The data. + */ + public function isCoreInstanceData(): array { + return [ + [FALSE, NULL, NULL], + [TRUE, Project::ASUMINEN, EnvironmentEnum::Local], + [FALSE, 'non-existent', NULL], + ]; + } + +} From caa5e05d6dcf92cc69b560321fa4534223bccbb5 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:13:34 +0200 Subject: [PATCH 2/6] Comment fixes --- documentation/environment-resolver.md | 2 +- src/Environment/ActiveProjectRoles.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index 755aec6..5fbb163 100644 --- a/documentation/environment-resolver.md +++ b/documentation/environment-resolver.md @@ -77,7 +77,7 @@ This can be used to conditionally run code only in core instances, such as enabl $activeProjectRoles = \Drupal::service(\Drupal\helfi_api_base\Environment\ActiveProjectRoles::class); if ($activeProjectRoles->hasRole(\Drupal\helfi_api_base\Environment\ProjectRoleEnum::Core)) { - // Do something only on "core" instances. + // Do something only in core instances. } ``` diff --git a/src/Environment/ActiveProjectRoles.php b/src/Environment/ActiveProjectRoles.php index de18b39..ba9f76c 100644 --- a/src/Environment/ActiveProjectRoles.php +++ b/src/Environment/ActiveProjectRoles.php @@ -5,7 +5,7 @@ namespace Drupal\helfi_api_base\Environment; /** - * A service to determine the type of currently active project. + * A service to determine the roles of currently active project. */ final readonly class ActiveProjectRoles { From 3bf8f4d587669cee5415554d45d3210efdd9eede Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:14:50 +0200 Subject: [PATCH 3/6] Fixed link --- documentation/environment-resolver.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index 5fbb163..a67ef28 100644 --- a/documentation/environment-resolver.md +++ b/documentation/environment-resolver.md @@ -57,7 +57,7 @@ $project = $service->getActiveProject(); This can be used to determine what "roles" the currently active project has. -See [\Drupal\helfi_api_base\Environment\ProjectRoleEnum](src/Environment/ProjectRolesEnum.php) for available roles. +See [\Drupal\helfi_api_base\Environment\ProjectRoleEnum](/src/Environment/ProjectRolesEnum.php) for available roles. ```php /** @var \Drupal\helfi_api_base\Environment\ActiveProjectRoles $activeProjectRoles */ From 03f4bee4a92434af0ee00909fc32d7108e45feec Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:15:55 +0200 Subject: [PATCH 4/6] Fixed link again --- documentation/environment-resolver.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index a67ef28..ec4a868 100644 --- a/documentation/environment-resolver.md +++ b/documentation/environment-resolver.md @@ -57,7 +57,7 @@ $project = $service->getActiveProject(); This can be used to determine what "roles" the currently active project has. -See [\Drupal\helfi_api_base\Environment\ProjectRoleEnum](/src/Environment/ProjectRolesEnum.php) for available roles. +See [\Drupal\helfi_api_base\Environment\ProjectRoleEnum](/src/Environment/ProjectRoleEnum.php) for available roles. ```php /** @var \Drupal\helfi_api_base\Environment\ActiveProjectRoles $activeProjectRoles */ From e80dc0892373ee39ef930dce01f414f670861487 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:19:16 +0200 Subject: [PATCH 5/6] Support php 8.1 --- src/Environment/ActiveProjectRoles.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Environment/ActiveProjectRoles.php b/src/Environment/ActiveProjectRoles.php index ba9f76c..993ca71 100644 --- a/src/Environment/ActiveProjectRoles.php +++ b/src/Environment/ActiveProjectRoles.php @@ -7,9 +7,9 @@ /** * A service to determine the roles of currently active project. */ -final readonly class ActiveProjectRoles { +final class ActiveProjectRoles { - public function __construct(private EnvironmentResolverInterface $environmentResolver) { + public function __construct(private readonly EnvironmentResolverInterface $environmentResolver) { } /** From 218f5d4c98089cc8226522c7f69cc2a255c11847 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 19 Nov 2024 12:54:23 +0200 Subject: [PATCH 6/6] Test ci environment name --- tests/src/Unit/Environment/EnvironmentResolverTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/Unit/Environment/EnvironmentResolverTest.php b/tests/src/Unit/Environment/EnvironmentResolverTest.php index 02f0d97..8acb7c5 100644 --- a/tests/src/Unit/Environment/EnvironmentResolverTest.php +++ b/tests/src/Unit/Environment/EnvironmentResolverTest.php @@ -88,6 +88,7 @@ public function testEnvironmentMap(string $envName, string $expected) : void { */ public function environmentMapData() : array { return [ + ['ci', 'test'], ['testing', 'test'], ['production', 'prod'], ['staging', 'stage'],