statix-php / server

An object oriented wrapper around PHP's built-in server.
MIT License
117 stars 1 forks source link

How to reload env file on restart? #2

Closed xy2z closed 1 year ago

xy2z commented 1 year ago

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 1 year ago

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..

WyattCast44 commented 1 year ago

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

WyattCast44 commented 1 year ago

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.