Skip to content

Commit

Permalink
Merge pull request #188 from City-of-Helsinki/UHF-X-project-roles
Browse files Browse the repository at this point in the history
Added service to determine currently active project roles, such as "core" instance
  • Loading branch information
tuutti authored Nov 23, 2024
2 parents 0a92ddf + 218f5d4 commit 8ff114d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
29 changes: 29 additions & 0 deletions documentation/environment-resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
```

1 change: 1 addition & 0 deletions helfi_api_base.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
52 changes: 52 additions & 0 deletions src/Environment/ActiveProjectRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_api_base\Environment;

/**
* A service to determine the roles of currently active project.
*/
final class ActiveProjectRoles {

public function __construct(private readonly EnvironmentResolverInterface $environmentResolver) {
}

/**
* Checks if the current instance has the given role.
*
* @return bool
* TRUE if the project has given role.
*/
public function hasRole(ProjectRoleEnum $role): bool {
return in_array($role, $this->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;
}

}
2 changes: 2 additions & 0 deletions src/Environment/EnvironmentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Environment/ProjectRoleEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_api_base\Environment;

/**
* Defines the project features.
*/
enum ProjectRoleEnum {
case Core;
}
47 changes: 47 additions & 0 deletions tests/src/Unit/Environment/ActiveProjectRolesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_api_base\Unit\Environment;

use Drupal\Tests\UnitTestCase;
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait;
use Drupal\helfi_api_base\Environment\ActiveProjectRoles;
use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_api_base\Environment\ProjectRoleEnum;

/**
* Tests ActiveProjectType.
*
* @group helfi_api_base
*/
class ActiveProjectRolesTest extends UnitTestCase {

use EnvironmentResolverTrait;

/**
* Tests ::isCoreInstance().
*
* @dataProvider isCoreInstanceData
*/
public function testIsCoreInstance(bool $expected, ?string $projectName, ?EnvironmentEnum $env): void {
$sut = new ActiveProjectRoles($this->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],
];
}

}
1 change: 1 addition & 0 deletions tests/src/Unit/Environment/EnvironmentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 8ff114d

Please sign in to comment.