-
Notifications
You must be signed in to change notification settings - Fork 137
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
Comments
Hi @Zyles, 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 |
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:
Now instead of repeating itself the second module config could reference the first. Module 2 config:
Now most frameworks etc. probably have database connection defined by default. But this could be used for other commonly shared keys. |
@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 |
Maybe if you enclose it in quotes?
|
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 This make more sense, if you want use yml file instead of simple php array you can parse with yaml the file |
@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). |
This should do the trick, I use this often and it's something like how Symfony does it.
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. parameters:
- john: doe
lastname: %john% #will result in 'doe' In case you don't want the parameters to be an array, drop the If necessary I will create a PR later, but I'm not at my own desktop right now. |
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] <[email protected]> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
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:
Then you have a file for example parameters.yml
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.
The text was updated successfully, but these errors were encountered: