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

Need a global way to set config values dynamically #1661

Open
lonnieezell opened this Issue Jan 11, 2019 · 5 comments

Comments

Projects
None yet
3 participants
@lonnieezell
Copy link
Collaborator

lonnieezell commented Jan 11, 2019

Currently it's intended that config values are either set in the config file, the .env file, or controller sets an instance and passes it to chlldren. Need to be able to set a config value dynamically and have it set for all cached instances.

This became apparent in this thread, where they were trying to set the language at runtime and the wrong language was being used.

Could be that it needs to be handled in a hook prior to negotiation happens, but need to investigate and make sure it works otherwise.

@jim-parry

This comment has been minimized.

Copy link
Collaborator

jim-parry commented Jan 11, 2019

Things can get even more complicated ... wanting to set config options (locale or ???) from a database table :-/
The reasons make sense, but the reasoning could be circular :-S

@lonnieezell

This comment has been minimized.

Copy link
Collaborator

lonnieezell commented Jan 11, 2019

That's already been asked for :)

But it makes sense what they're saying here, though I'm not sure if what that particular post is asking for is even possible within the controller, and In don't think it is. I gave them a potential solution in the forums, but I still think we should investigate what's possible here and at least add to the docs the caveats and limitations.

@natanfelles

This comment has been minimized.

Copy link
Collaborator

natanfelles commented Jan 12, 2019

What about to instruct them to use the Config's constructors?

The thread has a post with a Controller where it wants to change the locale on the fly, but the Service already is loaded. Maybe it must be done via \Config\Services::language()->setLocale('ru') and not by a config property.


For the database use case, it could to use the constructors. But, too many SQL queries needed, I think.

Maybe an hook, Event, available just after the DotEnv load.

$env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$env->load();

Events::trigger('config'); // pre-pre_system ?

The first thought is that it will continues needing to use config('Foo')->var = 'bar';.

In the Event function, the developer could do a loop setting the items given from a database table, cache, API, etc.

Then, all configurations are set when the app run.

@natanfelles

This comment has been minimized.

Copy link
Collaborator

natanfelles commented Jan 12, 2019

Database Table:

site_id class property value
x App baseURL site1.com
x App defaultLocale kgp

app/Config/Events.php:

Events::on('config', function () {
	$configs = Database::connect(...)
                    ->query('SELECT * FROM config_table WHERE site_id = "x"')
                    ->getResult();

	foreach($configs as $config)
	{
	    config($config->class)->{$config->property} = $config->value;
	}
});

Where the site_id can be a custom $_SERVER var, the Host, etc.


Most important would be the "config" Event. Executed in the bootstrap file, before the current pre_system. (Or the pre_system could be moved?)

@lonnieezell

This comment has been minimized.

Copy link
Collaborator

lonnieezell commented Jan 12, 2019

As far as that specific post goes - I forgot that IncomingRequest has a setLocale() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment