Skip to content

Commit

Permalink
Merge pull request #117 from City-of-Helsinki/PLATTA-4937
Browse files Browse the repository at this point in the history
compute only once for active project and active env name
  • Loading branch information
oviag authored Jun 21, 2023
2 parents af41c08 + 6c45a54 commit b178224
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Environment/EnvironmentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ final class EnvironmentResolver implements EnvironmentResolverInterface {
*/
private array $projects;

/**
* The environment name.
*
* @var string
*/
private string $activeEnvironmentName;

/**
* The project.
*
* @var \Drupal\helfi_api_base\Environment\Project
*/
private Project $activeProject;

/**
* Constructs a new instance.
*
Expand Down Expand Up @@ -117,12 +131,15 @@ private function getConfig(string $key) : ?string {
* {@inheritdoc}
*/
public function getActiveProject() : Project {
if (!empty($this->activeProject)) {
return $this->activeProject;
}
if (!$name = $this->getConfig(self::PROJECT_NAME_KEY)) {
throw new \InvalidArgumentException(
$this->configurationMissingExceptionMessage('No active project found', self::PROJECT_NAME_KEY)
);
}
return $this
return $this->activeProject = $this
->getProject($name);
}

Expand All @@ -133,6 +150,9 @@ public function getActiveProject() : Project {
* The active environment name.
*/
public function getActiveEnvironmentName() : string {
if (!empty($this->activeEnvironmentName)) {
return $this->activeEnvironmentName;
}
if (!$env = $this->getConfig(self::ENVIRONMENT_NAME_KEY)) {
// Fallback to APP_ENV env variable.
$env = getenv('APP_ENV');
Expand All @@ -142,7 +162,7 @@ public function getActiveEnvironmentName() : string {
$this->configurationMissingExceptionMessage('No active environment found', self::ENVIRONMENT_NAME_KEY)
);
}
return $this->normalizeEnvironmentName($env);
return $this->activeEnvironmentName = $this->normalizeEnvironmentName($env);
}

/**
Expand Down

0 comments on commit b178224

Please sign in to comment.