diff --git a/src/Environment/EnvironmentResolver.php b/src/Environment/EnvironmentResolver.php index ed2336a6..fcb0cafb 100644 --- a/src/Environment/EnvironmentResolver.php +++ b/src/Environment/EnvironmentResolver.php @@ -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. * @@ -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); } @@ -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'); @@ -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); } /**