statix-php/server

How to reload env file on restart?

xy2z opened this issue · 3 comments

xy2z commented

Hey

When I restart the server, I would like to also reload the .env file. It doesn't seem to work sadly.

$server = Server::new()
    // ->php('path')
    ->host('localhost')
    ->port('85')
    ->root(__DIR__ . '/public')
    ->withEnvFile('.env');

// ...

$server->runInBackground();

then I do a simple while loop and restart the server if a file was changed, but the ".env" file is not reloaded, even though the file was changed before the $server->restart().

I have tried different things, but can't get it to work.

xy2z commented

Ok, I did some debugging, and withEnvFile() get called on every restart(), but it seems this is the problem:

(Dotenv::createImmutable(
            dirname($path),
            basename($path)
        ))->safeLoad();

it doesn't overwrite env when called on restart(), if it the content in the $path has changed...

From the https://github.com/vlucas/phpdotenv README:

If you want Dotenv to overwrite existing environment variables, use createMutable instead of createImmutable:

$dotenv = Dotenv\Dotenv::createMutable(__DIR__);
$dotenv->load();

I tried changing it to createMutable() instead of createImmutable() and it does work a bit, the ENV value is now correctly changed in $envVarsToPass - but it still doesn't change in the public/index.php php file, hmm..

Hmm okay I'll take a look tonight after work, thanks for doing some debugging!

Closing this for now, after troubleshooting this seems to be an issue with the Symfony Process component and their use of getenv and setenv functions which are not thread safe and causing the issue. Sorry I couldn't fix that for you, let me know if you find any solutions.