config icon indicating copy to clipboard operation
config copied to clipboard

Request: Enhancement (group by file name when Multiple File load)

Open tmgast opened this issue 9 years ago • 4 comments

Regarding an old issue #18:

It was asked how to handle when multiple config files share a variable name.

If you're loading in multiple config files, wouldn't it make more sense to contain each loaded file as its own array within the config container?

loading files:
- config/environment.php
- config/services.php
- config/database.php

These should be accessible in the following way:

$app->config = new Config(__DIR__ . '/../config');
$app->config->get('environment.name'); // the name variable is returned from the environment config
$app->config->get('database.server'); // the server variable is returned from the database config

Currently, all the config options are mashed together in a single array, which overwrites any duplicates.

Example: server.php

<?php

return [
    "system" => "debian",
    "apache" => [
        "location" => "/usr/bin/apache"
    ]
];

environments.php

<?php

return [
    "local" => [
        "domain" => "dev"
    ],
    "production" => [
        "domain" => "domain.com"
    ]
];

Outputs:

{
    "local":{
        "domain":"dev"
    },
    "production":{
        "domain":"domain.com"
    },
    "system":"debian",
    "apache":{
        "location":"\/usr\/bin\/apache"
    }
}

However, I would like it to output:

{
    "environments":{
        "local":{
            "domain":"dev"
        },
        "production":{
            "domain":"domain.com"
        },
    },
    "server": {
        "system":"debian",
        "apache":{
            "location":"\/usr\/bin\/apache"
        }
    }
}

tmgast avatar Mar 04 '16 14:03 tmgast

Hi, currently it should work if you manually pass in each file to the constructor, passing directories is not currently supported. Good idea for the future though.

hassankhan avatar Mar 04 '16 15:03 hassankhan

@tmgast You can do this with https://github.com/m1/vars fyi

marcuschaplais avatar Mar 05 '16 20:03 marcuschaplais

As I said before, would be a great idea for the next version. Another idea is you could have a main config.php that is something like this:

<?php

return [
    'server' => require_once('server.php'),
    'environments' => require_once('environments.php')
]

hassankhan avatar Mar 05 '16 22:03 hassankhan

You might want to take a look at https://github.com/samrap/gestalt .

It is a lot more flexible on how it loads configurations from files and even allows you to define custom loaders using an interface. It solves the multiple variable names by using the filename(s) as the root key(s) and nesting the values inside.

samrap avatar Sep 30 '16 22:09 samrap