Skip to content

Commit 81e9e15

Browse files
committed
Ensure that configuration is loaded before using it
1 parent 426d9db commit 81e9e15

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Configuration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,28 @@ public static function auto(?string $stage = null): Configuration
107107
* @param string $key
108108
* @param mixed|null $default
109109
*
110+
* @throws ConfigurationException
111+
*
110112
* @return mixed
111113
*/
112114
public function get(string $key, mixed $default = null): mixed
113115
{
116+
$this->ensureThatConfigurationIsLoaded();
117+
114118
return $this->config->get($key, $default)->getValue();
115119
}
116120

117121
/**
118122
* Gets all the tree config.
119123
*
124+
* @throws ConfigurationException
125+
*
120126
* @return array
121127
*/
122128
public function all(): array
123129
{
130+
$this->ensureThatConfigurationIsLoaded();
131+
124132
return $this->config->toArray();
125133
}
126134

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

412420
throw ConfigurationException::autoFindConfigurationDirFailed(self::$possibleLocations);
413421
}
422+
423+
/**
424+
* @throws ConfigurationException
425+
*/
426+
private function ensureThatConfigurationIsLoaded(): void
427+
{
428+
if (!isset($this->config)) {
429+
throw ConfigurationException::configurationNotLoaded();
430+
}
431+
}
414432
}

src/Exception/ConfigurationException.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ public function __construct(string $message)
2020
parent::__construct(sprintf("%s\n\nDocumentation available here: %s", $message, self::SPECIFICATION_URI));
2121
}
2222

23-
/**
24-
* @return self
25-
*/
2623
public static function operationNotAllowed(): self
2724
{
2825
return new self('Operation not allowed.');
2926
}
3027

28+
public static function configurationNotLoaded(): self
29+
{
30+
return new self('Configuration not loaded. Method `load` called?');
31+
}
32+
3133
/**
3234
* @param string $pattern
3335
* @param string $path
@@ -43,16 +45,6 @@ public static function filesNotFound(string $pattern, string $path, string $stag
4345
return new self("$one\n$two");
4446
}
4547

46-
/**
47-
* @param string $path
48-
*
49-
* @return self
50-
*/
51-
public static function pathNotFound(string $path): self
52-
{
53-
return new self("Path [$path] not found.");
54-
}
55-
5648
/**
5749
* @param string $stage
5850
* @param string $top

0 commit comments

Comments
 (0)