illuminatech / config

Manage Laravel configuration by persistent storage
Other
150 stars 14 forks source link

Feature Request: set configuration items multiple times #15

Closed mah484 closed 2 years ago

mah484 commented 2 years ago

I am trying to use this package as having a persistent configuration repository will be very useful for my purposes. I have created a Service Provider that on boot() will extend the configuration service class like so:

       $this->app->extend('config', function ($originConfig) {
            $storage = new StorageEloquent(ConfigurationItem::class);
            return (new PersistentRepository($originConfig, $storage))
                ->setCache($this->app->make('cache.store'))
                ->setCacheKey($this->configItemCacheKey)
                ->setCacheTtl($this->configItemCacheTtl);
        });

This works as expected. If I also call setItems() in the same statement, they are retrieved from the database using the Eloquent model.

However, I also have many other "services" with their own Service Provider. I was hoping to be able to instruct the PersistentRepository in the boot() method of each Service Provider what the configuration items (->setItems()) are for each service. Unfortunately, it seems that calling ->setItems() will completely overwrite previous calls. It would be extremely useful to call this multiple times to "build up" the list of persistent configuration items managed by the repository.

klimov-paul commented 2 years ago

There is nothing special about array manipulations: you just need to get existing items merge them with the new one and set the result back:

$this->app->extend('config', function (PersistentRepository $config) {
    $exitsingItems = $config->getItems()->all();
    $newItems = array_merge($exitsingItems, [
        // addition items
    ]);
    $config->setItems($newItems);

    return $config;
});
mah484 commented 1 year ago

I am having an issue and I do not know if it is related to this suggestion. In the service provider, I can load the persistent configuration items using setItems(). Then, in a controller, when I want to retrieve them, I will get null in some cases. Even weirder, I can do Config::get('services.service_name') which is null, but if I do Config::get('services'), the values are retrieved. For more context, I have configuration items with keys services.service_name.config_item1, services.service_name.config_item2, etc.

Running restore() after setItems(), however, resolves this issue. If I also go through the response from getItems() and iterate through checking each value using Config::get($key), then the values are loaded.

So, as best I can tell, for some reason, the values are not loaded if using dot notation for retrieving a group of configuration items.

I believe this may be a separate issue. I can open a ticket if necessary. I am just not 100% sure if it is related to this suggestion.

I can put together an example to reproduce if the issue is not immediately obvious from the behavior I have described, but it will take a while.