From d9f344f1a332ad06ebab95386e0f3ed03cbaf52e Mon Sep 17 00:00:00 2001 From: Creak Date: Tue, 18 Nov 2025 14:39:44 -0500 Subject: [PATCH] refactor: use better design pattern for loading config files --- config/config.default.php | 2 +- src/Config.php | 31 +++++++++++++++++------ src/Mesamatrix.php | 10 ++++---- tests/resources/config/config.default.php | 2 +- tests/resources/config/config.php | 8 +++--- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/config/config.default.php b/config/config.default.php index 84c4701..e419a58 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -16,7 +16,7 @@ * Log::DEBUG */ -$CONFIG = [ +return [ "info" => [ "log_level" => Log::WARNING, "version" => "3.0", diff --git a/src/Config.php b/src/Config.php index 9585e10..fc1744f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -27,32 +27,47 @@ class Config public function __construct(string $configDir) { - $this->readData($configDir . '/config.default.php'); + if (!file_exists($configDir)) { + Mesamatrix::$logger->error( + "Could not load the config files: the config directory \"{$configDir}\" doesn't exist." + ); + return; + } + + if (!$this->readData($configDir . '/config.default.php')) { + Mesamatrix::$logger->warning( + "Could not load the default config file: the file \"{${$configDir . '/config.default.php'}}\" doesn't exist." + ); + } + + // Read optional overriding config file. $this->readData($configDir . '/config.php'); } - public function getValue(string $section, string $key, $default = null) + public function getValue(string $section, string $key, mixed $default = null): mixed { if (isset($this->cache[$section])) { if (isset($this->cache[$section][$key])) { return $this->cache[$section][$key]; } } - $logMsg = 'Unable to find config value for ' . $section . '.' . $key; + if (is_null($default)) { - Mesamatrix::$logger->error($logMsg); + Mesamatrix::$logger->error("Unable to find config value for {$section}.{$key}"); } return $default; } - private function readData(string $configFile) + private function readData(string $configFile): bool { if (file_exists($configFile)) { Mesamatrix::$logger->info('Loading configuration file ' . $configFile); - @include $configFile; - if (isset($CONFIG) && is_array($CONFIG)) { - foreach ($CONFIG as $section => $sectionConfig) { + + $config = require($configFile); + + if (isset($config) && is_array($config)) { + foreach ($config as $section => $sectionConfig) { if (array_key_exists($section, $this->cache)) { $this->cache[$section] = array_merge($this->cache[$section], $sectionConfig); diff --git a/src/Mesamatrix.php b/src/Mesamatrix.php index e8d2f1e..1035007 100644 --- a/src/Mesamatrix.php +++ b/src/Mesamatrix.php @@ -29,11 +29,11 @@ class Mesamatrix { - public static $serverRoot; // Path to root of installation - public static $configDir; // Path to configuration directory - public static $config; // Config object - public static $logger; // Logger - public static $request; // HTTP request object + public static string $serverRoot; // Path to root of installation + public static string $configDir; // Path to configuration directory + public static Config $config; + public static Logger $logger; + public static HTTPRequest $request; public static function init() { diff --git a/tests/resources/config/config.default.php b/tests/resources/config/config.default.php index a4efdb8..ea225b3 100644 --- a/tests/resources/config/config.default.php +++ b/tests/resources/config/config.default.php @@ -1,6 +1,6 @@ [ 'key1' => 'test_value', 'key2' => 'default_value', diff --git a/tests/resources/config/config.php b/tests/resources/config/config.php index f1c0965..3ef5d07 100644 --- a/tests/resources/config/config.php +++ b/tests/resources/config/config.php @@ -1,7 +1,7 @@ array( +return [ + 'test_section1' => [ 'key2' => 'overridden_value', - ), -); + ], +];