Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

jfsenechal
Copy link

here is my proposition

@@ -176,12 +178,25 @@ private function loadMinkParameters(ContainerBuilder $container): void
private function loadBootstrap(?string $bootstrap): void
{
if ($bootstrap === null) {
$this->loadEnv();

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?

Copy link
Author

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

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

Copy link
Author

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;

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?

Copy link
Author

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 ?

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();

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');

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;

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.

@mtamazlicaru
Copy link

Seems ok to me, now we need to see what the maintainer (@pamil) has to say. Thanks @jfsenechal

@walva
Copy link
Contributor

walva commented Nov 27, 2020

Hello there ! Do you need help for this? I had the issue today and I fixed it thanks to #126 (comment)

@Yozhef
Copy link
Contributor

Yozhef commented Dec 13, 2021

@jfsenechal can you please rebase in master)

Copy link
Contributor

@Yozhef Yozhef left a 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);
Copy link
Member

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?

Suggested change
(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'])) {
Copy link
Member

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.

@jfsenechal
Copy link
Author

@jfsenechal can you please rebase in master)

Hi, I am sorry I don't know how to do that

@Yozhef
Copy link
Contributor

Yozhef commented Dec 13, 2021

@jfsenechal can you please rebase in master)

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

@jfsenechal
Copy link
Author

@jfsenechal can you please rebase in master)

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

@Yozhef
Copy link
Contributor

Yozhef commented Dec 14, 2021

@jfsenechal you need to pull in your fork actual master then in your pr git pull master -> then you see conflict, resolve his and push )

How pull in your fork actual master:

  1. Go to work
  2. press button

Screenshot 2021-12-14 at 12 45 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants