Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tokens/parameters? #85

Open
Zyles opened this issue Sep 1, 2016 · 7 comments
Open

Support for tokens/parameters? #85

Zyles opened this issue Sep 1, 2016 · 7 comments
Labels

Comments

@Zyles
Copy link

Zyles commented Sep 1, 2016

Did I overlook something or is there support for using tokens with replace like Symfony does in config files.

For example you have a config file:

config.yml:

database:
    host: %hostname%
    user: %username%
    password: %password%

Then you have a file for example parameters.yml

parameters.yml:

database:
    host: example.com
    user: example
    password: password

Then you only need to replace the parameters.yml on each environment and keep one config file complete. Instead of having multiple config files you need to edit if you add or remove values.

@DavidePastore
Copy link
Collaborator

Hi @Zyles,
yes, you can do something like this. For example if you have

config.yml

database:
    host: %hostname%
    user: %username%
    password: %password%
api:
    limit: 10

and parameters.yml

database:
    host: example.com
    user: example
    password: password

you can just load them using the multiple files syntax:

$conf = new Config(['config.yml', 'parameters.yml']);

In this example you'll preserve api.limit key and the config will just overwrite all the already defined keys (database.host, database.user and database.password).

@Zyles
Copy link
Author

Zyles commented Oct 12, 2016

Yes sure that kind of works. But I also have another use case which is a bit more advanced.

For example two modules from two different developers need access to the same variables. Instead of repeating the same information twice it would be nice if you could reference them inside config files.

Example scenario where two independent modules need access to the database and there is no default setting for that so they both use their own config keys. But the user setting up the config could reference between the files.

Module 1 config:

module1:
    database:
        host: localhost
        user: username
        password: password

Now instead of repeating itself the second module config could reference the first.

Module 2 config:

module2:
    database:
        host: %module1.database.host%
        user: %module1.database.user%
        password: %module1.database.password%

Now most frameworks etc. probably have database connection defined by default. But this could be used for other commonly shared keys.

@DavidePastore
Copy link
Collaborator

@Zyles This has sense. The only problem with this is: how can I handle the case where I have a configuration file with the value of "%module1.database.host%" and I want it as a string?

@Zyles
Copy link
Author

Zyles commented Oct 12, 2016

@DavidePastore

Maybe if you enclose it in quotes?

host: "%module1.database.host%"

@nunocodex
Copy link

nunocodex commented Oct 12, 2016

SUGGESTION

Sorry guys but is not much better pass the replacements on config costruct?

$config = new Config([
    'default.yml',
    'dev.yml'
],
[
    'db' => [
        'hostname' => 'localhost'
    ]
]);

With dotted array db.hostname you can use in the yml file %db.hostname%.

This make more sense, if you want use yml file instead of simple php array you can parse with yaml the file Yaml::parse('parameters.yml').

@TiMESPLiNTER
Copy link

@namaless the problem with this solution would be that you can't define paramters inside a configuration file (if this is a requirement at all).

@leroy0211
Copy link

leroy0211 commented Nov 30, 2016

This should do the trick, I use this often and it's something like how Symfony does it.

$content contains the complete configuration array!

if(isset($content['parameters'])){
    foreach($content['parameters'] as $param){
        foreach($param as $key => $value){
            $parameters[$key] = $value;
        }
    }
}

array_walk_recursive($content, function(&$val, $key) use ($parameters){
    $matches = null;
    preg_match('/\%(.*?)\%/', $val, $matches);
    $param = isset($matches[1]) ? $matches[1] : false;
    if($param){
        if (isset($parameters[$param])) {
           $val = str_replace("%$param%", $parameters[$param], $val);
        }
    }
});

You should place this somewhere you loop through the configuration files.
This way all parameters are encapsulated inside a parameters key in the yaml file.

parameters:
    - john:  doe


lastname: %john%  #will result in 'doe'

In case you don't want the parameters to be an array, drop the foreach($content['parameters'] as $param){ and change the foreach($param as $key => $value){ to foreach($content['parameters'] as $key => $value){

If necessary I will create a PR later, but I'm not at my own desktop right now.

peter279k pushed a commit to open-source-contributions/config that referenced this issue May 8, 2021
Bumps [phpstan/phpstan](https://github.com/phpstan/phpstan) from 0.12.73 to 0.12.74.
- [Release notes](https://github.com/phpstan/phpstan/releases)
- [Commits](phpstan/phpstan@0.12.73...0.12.74)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants