Overwriting configurations

Hey guys, :slight_smile:

I am new here and I hope you can give me some food for thoughts. I am developing a php system which can be extended by various extensions. Each extension has its own extension directory with a bundle of different classes (e.g. service classes) and sub directories. One of the sub directory is the configuration directory of the extension. Each extension may provide custom configurations and should be able to overwrite maybe a small set of configuration items of another extension. I am sure this concept may cause some snares but it is a requirement. A configuration file is a php file which returns an array. Let us be more precise and consider the following example.

cache.php
[php]return [
‘cache’ => [
‘lifetime’ => 3600,
‘logger’ => [
‘cacheEntryAddedLogger’
]
]
];[/php]

This simple configuration is provided by an extension x. What if another extension y requires the extension x and must overwrite the provided configuration file? The other extension should provide a configuration file with the same name but with different contents.

cache.php
[php]return [
‘cache’ => [
‘lifetime’ => 3600,
‘logger’ => [
‘cacheEntryRetrievalLogger’
]
]
];[/php]

In case of the ‘lifetime’ parameter its quite simple. Just replace the value (replace the array value of the key). In case of the ‘logger’ parameter I am not sure how to manage this with arrays. A developer could have at least two options of intensions.

a) Append the ‘logger’ array entries from the overwriting configuration file
b) Replace the ‘logger’ array with the array from the overwriting configuration file

Do you have any idea how I can manage this to support both the appending and the replacement of array configuration items? I hope I illustrated my problem understandable.

Thank you for your help :slight_smile:

Each extension may provide custom configurations and should be able to overwrite maybe a small set of configuration items of another extension. I am sure this concept may cause some snares but it is a requirement.

You are right. If one extension_a needs something to be X and and extension_c sets it to R, the system will fail. The best idea, is that each extension has it’s own config file, and none of them can touch actual system settings. If you do a merge, it will add values, however, to overwrite, and this gets quite hairy, you would need to loop thru them and pick which version to use.

Indeed, you have realized the problem. I have recently seen the same principle within the kohana framework (see https://kohanaframework.org/3.2/guide/kohana/files/config#merge). The documentation says that you can overwrite perhaps a single entry without repeating other entries that will not be modified. Unfortunately the documentation does not consider the case of overwriting arrays as I mentioned in my previous post.

I really don’t know how to help other than stating what I already have. I haven’t touched that framework either, I have stuck to more widely used for the scalability aspect of keeping things moving.

Sponsor our Newsletter | Privacy Policy | Terms of Service