diff --git a/docs/4-Configuration.md b/docs/4-Configuration.md index 27e86f64..f88e3989 100644 --- a/docs/4-Configuration.md +++ b/docs/4-Configuration.md @@ -1011,7 +1011,3 @@ For example, the following command set the website’s `baseurl`: ```bash export CECIL_BASEURL="https://example.com/" ``` - -:::important -Only existing configuration options can be overridden: you can’t create new configuration options with environment variables. -::: diff --git a/src/Config.php b/src/Config.php index f9392c04..edaa8828 100644 --- a/src/Config.php +++ b/src/Config.php @@ -72,8 +72,8 @@ public function __construct(?array $config = null) public function import(array $config, $mode = self::MERGE): void { $this->data->import($config, $mode); + $this->setFromEnv(); $this->validate(); - $this->override(); } /** @@ -540,30 +540,15 @@ public function isCacheDirIsAbsolute(): bool } /** - * Overrides configuration with environment variables. + * Set configuration from environment variables. */ - private function override(): void + private function setFromEnv(): void { - $data = $this->data; - $applyEnv = function ($array) use ($data) { - $iterator = new \RecursiveIteratorIterator( - new \RecursiveArrayIterator($array), - \RecursiveIteratorIterator::SELF_FIRST - ); - $iterator->rewind(); - while ($iterator->valid()) { - $path = []; - foreach (range(0, $iterator->getDepth()) as $depth) { - $path[] = $iterator->getSubIterator($depth)->key(); - } - $sPath = implode('_', $path); - if ($getEnv = getenv('CECIL_' . strtoupper($sPath))) { - $data->set(str_replace('_', '.', strtolower($sPath)), $this->castSetValue($getEnv)); - } - $iterator->next(); + foreach (getenv() as $key => $value) { + if (str_starts_with($key, 'CECIL_')) { + $this->data->set(str_replace(['cecil_', '_'], ['', '.'], strtolower($key)), $this->castSetValue($value)); } - }; - $applyEnv($data->export()); + } } /** diff --git a/tests/IntegrationTests.php b/tests/IntegrationTests.php index c36e6795..d456be67 100644 --- a/tests/IntegrationTests.php +++ b/tests/IntegrationTests.php @@ -42,8 +42,8 @@ public function tearDown(): void public function testBuid() { putenv('CECIL_DEBUG=true'); - putenv('CECIL_TITLE=Cecil'); - putenv('CECIL_DESCRIPTION=Description (env. variable)'); + putenv('CECIL_TITLE=Cecil (env)'); + putenv('CECIL_DESCRIPTION=Description (env)'); echo "\n"; Builder::create( require($this->config), diff --git a/tests/fixtures/website/config.php b/tests/fixtures/website/config.php index 1a051f19..4a7d563a 100644 --- a/tests/fixtures/website/config.php +++ b/tests/fixtures/website/config.php @@ -1,7 +1,6 @@ true, 'title' => 'Cecil test', 'taxonomies' => [ 'tests' => 'disabled',