Skip to content

Commit

Permalink
Make sure the .env.local.php is correctly loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Mar 4, 2024
1 parent 38f9755 commit d14915f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions manager-bundle/src/HttpKernel/ContaoKernel.php
Expand Up @@ -255,7 +255,7 @@ public static function fromRequest(string $projectDir, Request $request): HttpKe
$env = 'dev';
}

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env;
self::loadEnv($projectDir, $env);
}

$kernel = static::create($projectDir, $env);
Expand Down Expand Up @@ -363,16 +363,14 @@ private static function create(string $projectDir, string|null $env = null): sel
private static function loadEnv(string $projectDir, string $defaultEnv = 'prod'): void
{
// Load cached env vars if the .env.local.php file exists.
// https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/config/bootstrap.php
if (\is_array($env = @include Path::join($projectDir, '.env.local.php'))) {
foreach ($env as $k => $v) {
$_ENV[$k] ??= isset($_SERVER[$k]) && !str_starts_with($k, 'HTTP_') ? $_SERVER[$k] : $v;
}
// https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.4/config/bootstrap.php
if (\is_array($env = @include Path::join($projectDir, '.env.local.php')) && (!isset($env['APP_ENV']) || $defaultEnv === $env['APP_ENV'])) {
(new Dotenv())->populate($env);
} elseif (file_exists($filePath = Path::join($projectDir, '.env'))) {
(new Dotenv())->usePutenv(false)->loadEnv($filePath, 'APP_ENV', $defaultEnv);
(new Dotenv())->loadEnv($filePath, 'APP_ENV', $defaultEnv);
}

$_SERVER += $_ENV;
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null ?: $defaultEnv;
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: $defaultEnv;
}
}

0 comments on commit d14915f

Please sign in to comment.