odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
439 stars 80 forks source link

Wrong statement in the configuration doc page #105

Closed jfradj closed 1 year ago

jfradj commented 1 year ago

Hello,

https://odan.github.io/slim4-skeleton/configuration.html states that:

The configuration files are loaded in this order:

Load default settings from: config/defaults.php

If the environment variable APP_ENV is defined, load the environment specific file, e.g. config/local.{env}.php

Load secret credentials (if file exists) from:

  • config/env.php
  • config/../../env.php

Which is not correct. The PHP glob() function used, returns file paths alphabetically ordered, thus the secret credentials file (env.php) will be loaded BEFORE the environment specific file (local.{env}.php)

I think the issue isn't really in the documentation page, since it's the expected behavior. The issue is in the config/settings.php file using the glob function thus loosing any kind of control on the load ordering

Regards, Johann

jfradj commented 1 year ago

The following code replacement will fix the issue.

// Overwrite default settings with environment specific local settings
$configFiles = [
    __DIR__ . "/local.{$_ENV['APP_ENV']}.php",
    __DIR__ . '/env.php',
    __DIR__ . '/../../env.php',
];

foreach ($configFiles as $configFile) {
    if (!file_exists($configFile)) {
        continue;
    }

    $local = require $configFile;
    if (is_callable($local)) {
        $settings = $local($settings);
    }
}
odan commented 1 year ago

Thanks for the feedback. Your approach looks good. I will check it.

odan commented 1 year ago

It should be fixed now. Please check the changes and give me feedback.

odan commented 1 year ago

Close this as fixed.