Skip to content

Commit

Permalink
Ensure that configuration is loaded before using it
Browse files Browse the repository at this point in the history
  • Loading branch information
roquie committed Apr 11, 2022
1 parent 426d9db commit 81e9e15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,28 @@ public static function auto(?string $stage = null): Configuration
* @param string $key
* @param mixed|null $default
*
* @throws ConfigurationException
*
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
$this->ensureThatConfigurationIsLoaded();

return $this->config->get($key, $default)->getValue();
}

/**
* Gets all the tree config.
*
* @throws ConfigurationException
*
* @return array
*/
public function all(): array
{
$this->ensureThatConfigurationIsLoaded();

return $this->config->toArray();
}

Expand Down Expand Up @@ -411,4 +419,14 @@ private static function findDirectories(): string

throw ConfigurationException::autoFindConfigurationDirFailed(self::$possibleLocations);
}

/**
* @throws ConfigurationException
*/
private function ensureThatConfigurationIsLoaded(): void
{
if (!isset($this->config)) {
throw ConfigurationException::configurationNotLoaded();
}
}
}
18 changes: 5 additions & 13 deletions src/Exception/ConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ public function __construct(string $message)
parent::__construct(sprintf("%s\n\nDocumentation available here: %s", $message, self::SPECIFICATION_URI));
}

/**
* @return self
*/
public static function operationNotAllowed(): self
{
return new self('Operation not allowed.');
}

public static function configurationNotLoaded(): self
{
return new self('Configuration not loaded. Method `load` called?');
}

/**
* @param string $pattern
* @param string $path
Expand All @@ -43,16 +45,6 @@ public static function filesNotFound(string $pattern, string $path, string $stag
return new self("$one\n$two");
}

/**
* @param string $path
*
* @return self
*/
public static function pathNotFound(string $path): self
{
return new self("Path [$path] not found.");
}

/**
* @param string $stage
* @param string $top
Expand Down

0 comments on commit 81e9e15

Please sign in to comment.