How to get the bootstrap configuration from a database

gregor_nette
Member | 12
+
0
-

Hi!

Is it possible to get the configuration from a database, that you usually get from a neon file in Bootstrap.php?

namespace App;

use Nette\Bootstrap\Configurator;

class Bootstrap
{
    public static function boot(): Configurator
    {
        $configurator = new Configurator();
        $appDir = dirname(__DIR__);

        $configurator->enableTracy($appDir . '/log');

        $configurator->setTimeZone('Europe/Berlin');
        $configurator->setTempDirectory($appDir . '/temp');

        $configurator->createRobotLoader()->addDirectory(__DIR__)->register();

        $configurator->addConfig($appDir . '/config/common.neon');
        $configurator->addConfig($appDir . '/config/services.neon');
        $appp_file = $appDir . '/config/local.' . $_SERVER["HTTP_HOST"] . '.neon'; //XXXX
        try {
            if (! file_exists($appp_file)) {
                // get the config from db
                $a_fix_connection = new Database_with_configuration('SELECT * FROM siteconfigs WHERE hostname =  ?',
                                      $_SERVER["HTTP_HOST"] );
                $magically_retrieved_neon = $a_fix_connection->fetch();
                file_put_contents($appp_file, unserialize($magically_retrieved_neon));
            } else {
                die("Drop dead gorgeous");
            }
            $configurator->addConfig($appp_file);
        } catch (\Throwable  $e) {
            echo  $e->getMessage();
        }
        $configurator->addConfig($appDir . '/config/local.neon');
        return $configurator;
    }
}

Is there some sort of nette helper functionality already integrated?
There is of course the way of connecting and retrieving the data directly… Something I try to avoid.

Thanks,
Gregor

dakur
Member | 337
+
0
-

@gregor_nette And the code you provided… Does it work? If not, what's wrong?

What are you trying to do actually? Why can't the configs stay in their NEON files?

petr.pavel
Member | 531
+
0
-

I second dakur's questions. I too would like to know your reasons and use case.

Re your code: Notice that $configurator->addConfig() also accepts an array. This way you can configure your app using standard PHP array (e.g. addConfig(['parameters' => [...]])) without ever using Neon. If you start with dynamic values, you don't need to encode them into Neon to have them saved into db. You can just serialize them.

gregor_nette
Member | 12
+
0
-

Hi,
The goal: I want to have the configs in the database for dynamically changing hosts.

As you can see, I try to use

 $_SERVER["HTTP_HOST"]

to distinguish the different config files, so I can add or remove the configuration in a backend on the run.
Let's say a new host gets added with a different sql configuration. By using a database I could add these hosts.
The backend would only touch the database. This could have some use in case of load balancing. I could distribute the same configuration on all nodes and they would regenerate the neon files on demand. The dynamically created files would be removed daily by a cronjob.

If you have a better solution: go ahead

Thanks
Gregor

petr.pavel
Member | 531
+
0
-

I still don't see the benefit of storing Neon content in a database.

I imagine you have a db table setting that mirrors the content of mydomain.com.neon so why don't you feed addConfig() directly with an array of those settings. Perhaps addDynamicParameters() would work better with DIC cache.

Or you can stop trying to do this with Nette DIC and create your own Config class that you can pass to your services as a dependency. This way you won't need to connect to the database before DIC is built.

If you insist on creating Neon files then you could handle their update separately. Not in Bootstrap – trying to build them before the request is served – but at any time later (e.g. on App/shutdown) updating them for the next request. So, the first request would be served using an outdated version (or some default version for the very first request), all others would find the updated version in place and use it.

gregor_nette
Member | 12
+
0
-

Hi.
I try to use neon files, because DI is very elegant…
But if it is not easily possible, I will use some sort of external neon creator or something else.

Currently I like the simplicity of DI.
I could change dataproviders on the fly. e.g. icecat could be replaced with DCI or something completely different with the same interface.

Thanks
Gregor

petr.pavel
Member | 531
+
0
-

From your response I feel that you didn't understand my suggestion about calling addConfig() or addDynamicParameters() with an array of parameters – not with a string path to a Neon file.

This solution would create a DIC and give you the same DI power as the Neon files.

If you'd like to discuss this over Zoom let me know (petr.pavel@pepa.info).

gregor_nette
Member | 12
+
0
-

Hi petr,
I guess you are right… The hint with the array for addDynamicParameters() (https://doc.nette.org/…onfiguration#…) is most likely the solution that I can use.

TNX
Gregor