Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Log::DEBUG
*/

$CONFIG = [
return [
"info" => [
"log_level" => Log::WARNING,
"version" => "3.0",
Expand Down
31 changes: 23 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/Mesamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/config/config.default.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$CONFIG = [
return [
'test_section1' => [
'key1' => 'test_value',
'key2' => 'default_value',
Expand Down
8 changes: 4 additions & 4 deletions tests/resources/config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$CONFIG = array(
'test_section1' => array(
return [
'test_section1' => [
'key2' => 'overridden_value',
),
);
],
];