Skip to content

Commit 1422b49

Browse files
authored
[Chore] Fix .env overriding (#204)
1 parent 6760193 commit 1422b49

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

bin/console

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env php
22
<?php
33

4-
if (getenv('ENV_DIR') !== false && getenv('ENV_DIR') !== '' ) {
5-
$_SERVER['APP_RUNTIME_OPTIONS']['dotenv_path'] = getenv('ENV_DIR').'/.env';
4+
$overridenEnvDir = getenv('ENV_DIR') ?: null;
5+
6+
if ($overridenEnvDir) {
7+
// Tell the Runtime not to touch dotenv so we can load our own file.
8+
// This is needed if the ENV_DIR is outside of the project directory
9+
$_SERVER['APP_RUNTIME_OPTIONS']['disable_dotenv'] = true;
610
}
711

812
use App\Kernel;
@@ -19,11 +23,12 @@ if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
1923

2024
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
2125

22-
return function (array $context) {
23-
24-
$env_dir = getenv('ENV_DIR') != false ? getenv('ENV_DIR') : dirname(__DIR__);
25-
(new Dotenv())->bootEnv($env_dir.'/.env');
26+
if ($overridenEnvDir) {
27+
// Load our own now, after the runtime has booted
28+
(new Dotenv())->bootEnv($overridenEnvDir.'/.env');
29+
}
2630

31+
return function (array $context) {
2732
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
2833

2934
return new Application($kernel);

public/index.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
<?php
22

3-
if (false !== getenv('ENV_DIR') && '' !== getenv('ENV_DIR')) {
4-
$_SERVER['APP_RUNTIME_OPTIONS']['dotenv_path'] = getenv('ENV_DIR').'/.env';
3+
$overridenEnvDir = getenv('ENV_DIR') ?: null;
4+
5+
if ($overridenEnvDir) {
6+
// Tell the Runtime not to touch dotenv so we can load our own file.
7+
// This is needed if the ENV_DIR is outside of the project directory
8+
$_SERVER['APP_RUNTIME_OPTIONS']['disable_dotenv'] = true;
59
}
610

711
use App\Kernel;
812
use Symfony\Component\Dotenv\Dotenv;
913

1014
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
1115

12-
return function (array $context) {
13-
$env_dir = false != getenv('ENV_DIR') ? getenv('ENV_DIR') : dirname(__DIR__);
14-
(new Dotenv())->bootEnv($env_dir.'/.env');
16+
if ($overridenEnvDir) {
17+
// Load our own now, after the runtime has booted
18+
(new Dotenv())->bootEnv($overridenEnvDir.'/.env');
19+
}
1520

21+
return function (array $context) {
1622
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
1723
};

0 commit comments

Comments
 (0)