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
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"vimeo/psalm": "3.10.1",
"sylius-labs/coding-standard": "^3.0",
"symfony/browser-kit": "^4.4|^5.0",
"symfony/dotenv": "^4.4|^5.0",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0"
Expand Down
15 changes: 15 additions & 0 deletions src/ServiceContainer/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Dotenv\Exception\PathException;

final class SymfonyExtension implements Extension
{
Expand Down Expand Up @@ -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


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.

try {
(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.

}
}

private function fallbackToTestEnvironment(): void
{
// If there's no defined server / environment variable with an environment, default to test
Expand Down