From 81e9e15bb30157de57b5dc207124464b7b639c96 Mon Sep 17 00:00:00 2001 From: roquie Date: Mon, 11 Apr 2022 17:27:46 +0300 Subject: [PATCH] Ensure that configuration is loaded before using it --- src/Configuration.php | 18 ++++++++++++++++++ src/Exception/ConfigurationException.php | 18 +++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 68cc3a4..4dcde2e 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -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(); } @@ -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(); + } + } } diff --git a/src/Exception/ConfigurationException.php b/src/Exception/ConfigurationException.php index dc16045..85842e5 100644 --- a/src/Exception/ConfigurationException.php +++ b/src/Exception/ConfigurationException.php @@ -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 @@ -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