diff --git a/documentation/environment-resolver.md b/documentation/environment-resolver.md index 3caf4b6..ec4a868 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/ProjectRoleEnum.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 in 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..993ca71 --- /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], + ]; + } + +} 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'],