diff --git a/CHANGELOG.md b/CHANGELOG.md index 301a189..def1798 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.1] - Unreleased + +### Changed + * ConfigurationLib will now attempt to load configuration files from Environment Variables if they are set, for example + if `com.example.application` wants to load `ExampleConfiguration` it will first check if `CONFIGLIB_EXAMPLECONFIGURATION` + is set, and if so, load that file instead of the default `example-configuration.yml` file. If the file is not found, + it will resort to its default behavior. You can either load an original json configuration file which is usually + located at `\var\ncc\data\net.nosial.configlib` as one of the .conf files, or you can load a yml which is the same + one you usually use to import or edit configurations. + + ## [1.0.0] - 2023-02-23 ### Added -* First Release + * First Release diff --git a/src/ConfigLib/Configuration.php b/src/ConfigLib/Configuration.php index 0f0c285..cf9fd7a 100644 --- a/src/ConfigLib/Configuration.php +++ b/src/ConfigLib/Configuration.php @@ -42,6 +42,11 @@ */ private $Modified; + /** + * @var bool + */ + private $FromEnvironment; + /** * Public Constructor * @@ -54,15 +59,33 @@ $name = str_replace('/', '_', $name); $name = str_replace('\\', '_', $name); $name = str_replace('.', '_', $name); + $this->FromEnvironment = false; - // Figure out the path to the configuration file - try + if(!getenv(sprintf("CONFIGLIB_%s", strtoupper($name)))) { - $this->Path = Runtime::getDataPath('net.nosial.configlib') . DIRECTORY_SEPARATOR . $name . '.conf'; + $environment_config = sprintf('CONFIGLIB_%s', strtoupper($name)); + if(file_exists($environment_config)) + { + $this->Path = $environment_config; + $this->FromEnvironment = true; + } + else + { + Log::warning('net.nosial.configlib', sprintf('Environment variable "%s" points to a non-existent file, resorting to default/builtin configuration', $environment_config)); + } } - catch (Exception $e) + + if($this->Path === null) { - throw new RuntimeException('Unable to load package "net.nosial.configlib"', $e); + // Figure out the path to the configuration file + try + { + $this->Path = Runtime::getDataPath('net.nosial.configlib') . DIRECTORY_SEPARATOR . $name . '.conf'; + } + catch (Exception $e) + { + throw new RuntimeException('Unable to load package "net.nosial.configlib"', $e); + } } // Set the name @@ -71,7 +94,6 @@ // Default Configuration $this->Modified = false; - if(file_exists($this->Path)) { try @@ -303,10 +325,19 @@ if (!$force && !$this->Modified) return; + // If the configuration file is a YAML file, import it instead + if(str_ends_with($this->Path, '.yaml') || str_ends_with($this->Path, '.yml')) + { + $this->import($this->Path); + return; + } + $fs = new Filesystem(); if (!$fs->exists($this->Path)) + { return; + } try {