Skip to content

Commit 3e471cb

Browse files
Always load vars defined in .env files, providing .env.local.php for prod
1 parent 2126cc2 commit 3e471cb

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

symfony/console/3.3/bin/console

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ if (!class_exists(Application::class)) {
1515
}
1616

1717
$input = new ArgvInput();
18-
if (null !== $_ENV['APP_ENV'] = $input->getParameterOption(['--env', '-e'], null, true)) {
19-
putenv('APP_ENV='.$_ENV['APP_ENV']);
20-
// force loading .env files when --env is defined
21-
$_SERVER['APP_ENV'] = null;
18+
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
19+
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
2220
}
2321

2422
if ($input->hasParameterOption('--no-debug', true)) {

symfony/flex/1.0/.env

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
# This file defines all environment variables that the application needs.
2-
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE.
3-
# Use ".env.local" for local overrides during development.
4-
# Use real environment variables when deploying to production.
1+
# In all environments, the following files are loaded if they exist,
2+
# the later taking precedence over the former:
3+
#
4+
# * .env contains default values for the environment variables needed by the app
5+
# * .env.local uncommitted file with local overrides
6+
# * .env.$APP_ENV committed environment-specific defaults
7+
# * .env.$APP_ENV.local uncommitted environment-specific overrides
8+
#
9+
# Real environment variables win over .env files.
10+
#
11+
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
12+
#
13+
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
514
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration

symfony/framework-bundle/3.3/config/bootstrap.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
require dirname(__DIR__).'/vendor/autoload.php';
66

7-
if (!array_key_exists('APP_ENV', $_SERVER)) {
8-
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null;
9-
}
10-
11-
if ('prod' !== $_SERVER['APP_ENV']) {
12-
if (!class_exists(Dotenv::class)) {
13-
throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
14-
}
15-
7+
// Load cached env vars if the .env.local.php file exists
8+
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
9+
if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
10+
$_SERVER += $env;
11+
$_ENV += $env;
12+
} elseif (!class_exists(Dotenv::class)) {
13+
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
14+
} else {
1615
$path = dirname(__DIR__).'/.env';
1716
$dotenv = new Dotenv();
1817

18+
// load all the .env files
1919
if (method_exists($dotenv, 'loadEnv')) {
2020
$dotenv->loadEnv($path);
2121
} else {

symfony/framework-bundle/3.3/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"gitignore": [
2121
"/.env.local",
22+
"/.env.local.php",
2223
"/.env.*.local",
2324
"/%PUBLIC_DIR%/bundles/",
2425
"/%VAR_DIR%/",

symfony/framework-bundle/4.2/config/bootstrap.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
require dirname(__DIR__).'/vendor/autoload.php';
66

7-
if (!array_key_exists('APP_ENV', $_SERVER)) {
8-
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] ?? null;
9-
}
10-
11-
if ('prod' !== $_SERVER['APP_ENV']) {
12-
if (!class_exists(Dotenv::class)) {
13-
throw new RuntimeException('The "APP_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
14-
}
15-
7+
// Load cached env vars if the .env.local.php file exists
8+
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
9+
if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
10+
$_SERVER += $env;
11+
$_ENV += $env;
12+
} elseif (!class_exists(Dotenv::class)) {
13+
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
14+
} else {
15+
// load all the .env files
1616
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
1717
}
1818

symfony/framework-bundle/4.2/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"gitignore": [
2121
"/.env.local",
22+
"/.env.local.php",
2223
"/.env.*.local",
2324
"/%PUBLIC_DIR%/bundles/",
2425
"/%VAR_DIR%/",

0 commit comments

Comments
 (0)