-
Notifications
You must be signed in to change notification settings - Fork 61
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
fix loading environment variable with symfony 5.1 #127
base: master
Are you sure you want to change the base?
Conversation
@@ -176,12 +178,25 @@ private function loadMinkParameters(ContainerBuilder $container): void | |||
private function loadBootstrap(?string $bootstrap): void | |||
{ | |||
if ($bootstrap === null) { | |||
$this->loadEnv(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of loading it in loadBootstrap method, to load it separately just before calling loadBoostrap, to make a check if bootstrap is not specified to try to load environment variables.
Just to have a separated context. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I think you are right, since the bootstrap.php file has disappeared
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you mean has disappeared, i see that in configure method, there is still bootstrap configuration, and also there is configuration of environment, and in loadEnv method, where APP_ENV is loaded.
What i think is to let loadBoostrap method like it was, and if bootstrap configuration is null, then to have a fallback to load from .env file, if no, to do nothing like before.
Example:
$this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap']));
if (empty($config['bootstrap'])) {
// try to load by calling loadEnv() method.
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you mean has disappeared
The file was config/bootstrap.php
yes of course nothing should be changed to keep a retro compatibility
I agree with you
(new Dotenv())->bootEnv(basename(dirname(__DIR__)).'/../.env', $env); | ||
} | ||
catch (PathException $exception) { | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point here to return null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was to do something like this if another method of loading variables came up :
if ($bootstrap === null) {
if( $this->loadEnv() === nul))
.....
return;
}
Should we throw an exception if the variables could not be loaded ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here just to do return, instead of return null.
@@ -176,12 +178,25 @@ private function loadMinkParameters(ContainerBuilder $container): void | |||
private function loadBootstrap(?string $bootstrap): void | |||
{ | |||
if ($bootstrap === null) { | |||
$this->loadEnv(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you mean has disappeared, i see that in configure method, there is still bootstrap configuration, and also there is configuration of environment, and in loadEnv method, where APP_ENV is loaded.
What i think is to let loadBoostrap method like it was, and if bootstrap configuration is null, then to have a fallback to load from .env file, if no, to do nothing like before.
Example:
$this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap']));
if (empty($config['bootstrap'])) {
// try to load by calling loadEnv() method.
}
return; | ||
} | ||
|
||
require_once $bootstrap; | ||
} | ||
|
||
private function loadEnv() | ||
{ | ||
$env = getenv('APP_ENV'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here i think is needed to pick the env like this
$config['environment'] ?? $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'test'; // Is how is done in 117 line number.
(new Dotenv())->bootEnv(basename(dirname(__DIR__)).'/../.env', $env); | ||
} | ||
catch (PathException $exception) { | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here just to do return, instead of return null.
Seems ok to me, now we need to see what the maintainer (@pamil) has to say. Thanks @jfsenechal |
Hello there ! Do you need help for this? I had the issue today and I fixed it thanks to #126 (comment) |
@jfsenechal can you please rebase in master) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good solution but we need to start the CI process - so after merging support Symfony 6 let's do a merge and make sure everything works
$env = $config['environment'] ?? $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'test'; | ||
|
||
try { | ||
(new Dotenv())->bootEnv(basename(dirname(__DIR__)) . '/../.env', $env); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the other IO-related functions in this file use CWD as the base - I wouldn't switch it to __DIR__
just in this PR as it introduces a non-standard behaviour. Could we go with the following?
(new Dotenv())->bootEnv(basename(dirname(__DIR__)) . '/../.env', $env); | |
(new Dotenv())->bootEnv('.env', $env); |
@@ -75,6 +77,10 @@ public function load(ContainerBuilder $container, array $config): void | |||
|
|||
$this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap'])); | |||
|
|||
if (empty($config['bootstrap'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If using an autodiscovered bootstrap file, it would load the env variables two times, we should check for that.
Additionally, we should mention that in docs - might be not that obvious that it only happens if there's no explicitly passed bootstrap file.
Hi, I am sorry I don't know how to do that |
Okey, can you please merge original master in your pr - because we add Git Hub Action when run test |
can you the commands to be executed i never did that |
@jfsenechal you need to pull in your fork actual master then in your pr How pull in your fork actual master:
|
here is my proposition