PHLAK / Config

PHP library for simple configuration management
https://packagist.org/packages/phlak/config
MIT License
50 stars 11 forks source link

why Config::set(), do not update the value in the config file ? #5

Open sunchayn opened 6 years ago

sunchayn commented 6 years ago

Hey there, isn't supposed that the set method update the config file ? I'm using PHP configuration and nothing seems to happen when i use set except changing the value in memory only.

PHLAK commented 6 years ago

This is the expected behavior. Your configuration files are supposed to be your source of truth and are never changed directly. Config::set() will just let you set/override a value (in memory) after it's been loaded from your config.

PHLAK commented 5 years ago

I've been thinking about this and it might be worth adding a Config::persist() method for saving changes to the config files. This might be tricky though due to the support of multiple config file formats. Reopening for now to track this.

sunchayn commented 5 years ago

Hey Chris, Actually, the project I was working on needed a 2 way config system. Unfortunately, I did not found a lightweight library for this task so I created my own package and use it in my project. I called the method sync() . Here is my package and this was my main project, I made it supports PHP based configuration only because that's what I needed, persisting JSON is easy too. It's straightforward to start with these two configurations and add the rest gradually.

PHLAK commented 5 years ago

It's straightforward to start with these two configurations and add the rest gradually.

We already support six different config file formats so persistence should support each, otherwise we start fragmenting support which makes the package harder to maintain.

xbipin commented 5 years ago

any plans on adding this feature as i need to save the config once i set something

darkstriders commented 5 years ago

Looking forward to Config::persist() method

Externaluse commented 1 year ago

Hi @PHLAK, I've been implementing a persist through Symfony/Cache. In the example below, I have hard-configured items to ignore in a config. When the system loads, it retrieves these from the cache through a callback unless the cache is expired or doesn't exist yet. That way I can add items on runtime, which will then be persisted for cacheDuration time. $cache = new FilesystemAdapter(namespace: $config->get('Api.RaceName'), defaultLifetime: 7 * 24 * 3600, directory: $config->get('Logging.Directory', 'Logs')); $alwaysIgnore = $cache->get(IGNORECACHEKEY, function (ItemInterface $item) use ($config) { $item->expiresAfter(new DateInterval('P3D')); // 3 days return $config->get("Configuration.AlwaysIgnore", []); });