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 all 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
17 changes: 17 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 @@ -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.

$this->loadEnv($config);
}

$this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel']));
$this->loadDriverKernel($container);

Expand Down Expand Up @@ -182,6 +188,17 @@ private function loadBootstrap(?string $bootstrap): void
require_once $bootstrap;
}

private function loadEnv(array $config)
{
$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);

} catch (PathException $exception) {
return;
}
}

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