8000 [Dotenv] load .env.local.php and define APP_DEBUG · symfony/symfony@19cd11c · GitHub
[go: up one dir, main page]

Skip to content

Commit 19cd11c

Browse files
[Dotenv] load .env.local.php and define APP_DEBUG
1 parent 37a8863 commit 19cd11c

File tree

9 files changed

+109
-41
lines changed

9 files changed

+109
-41
lines changed

UPGRADE-5.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 5.0 to 5.1
22
=======================
33

4+
Dotenv
5+
------
6+
7+
* Deprecated passing `$usePutenv` argument to Dotenv's constructor, use `Dotenv::usePutenv()` instead.
8+
49
EventDispatcher
510
---------------
611

UPGRADE-6.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 5.x to 6.0
22
=======================
33

4+
Dotenv
5+
------
6+
7+
* Removed argument `$usePutenv` from Dotenv's constructor, use `Dotenv::usePutenv()` instead.
8+
49
EventDispatcher
510
---------------
611

src/Symfony/Bridge/PhpUnit/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"symfony/error-handler": "For tracking deprecated interfaces usages at runtime with DebugClassLoader"
2525
},
2626
"conflict": {
27-
"phpunit/phpunit": "<5.4.3"
27+
"phpunit/phpunit": "<5.4.3",
28+
"symfony/dotenv": "<5.1"
2829
},
2930
"autoload": {
3031
"files": [ "bootstrap.php" ],

src/Symfony/Bundle/FrameworkBundle/Tests/Secrets/DotenvVaultTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testEncryptAndDecrypt()
3838
$vault->seal('foo', $plain);
3939

4040
unset($_SERVER['foo'], $_ENV['foo']);
41-
(new Dotenv(false))->load($this->envFile);
41+
(new Dotenv())->load($this->envFile);
4242

4343
$decrypted = $vault->reveal('foo');
4444
$this->assertSame($plain, $decrypted);
@@ -50,7 +50,7 @@ public function testEncryptAndDecrypt()
5050
$this->assertFalse($vault->remove('foo'));
5151

5252
unset($_SERVER['foo'], $_ENV['foo']);
53-
(new Dotenv(false))->load($this->envFile);
53+
(new Dotenv())->load($this->envFile);
5454

5555
$this->assertArrayNotHasKey('foo', $vault->list());
5656
}

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"symfony/console": "^4.4|^5.0",
3838
"symfony/css-selector": "^4.4|^5.0",
3939
"symfony/dom-crawler": "^4.4|^5.0",
40-
"symfony/dotenv": "^4.4|^5.0",
40+
"symfony/dotenv": "^5.1",
4141
"symfony/polyfill-intl-icu": "~1.0",
4242
"symfony/form": "^4.4|^5.0",
4343
"symfony/expression-language": "^4.4|^5.0",
@@ -71,7 +71,7 @@
7171
"symfony/asset": "<4.4",
7272
"symfony/browser-kit": "<4.4",
7373
"symfony/console": "<4.4",
74-
"symfony/dotenv": "<4.4",
74+
"symfony/dotenv": "<5.1",
7575
"symfony/dom-crawler": "<4.4",
7676
"symfony/http-client": "<4.4",
7777
"symfony/form": "<4.4",

src/Symfony/Component/Console/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"conflict": {
4343
"symfony/dependency-injection": "<4.4",
44+
"symfony/dotenv": "<5.1",
4445
"symfony/event-dispatcher": "<4.4",
4546
"symfony/lock": "<4.4",
4647
"symfony/process": "<4.4"

src/Symfony/Component/Dotenv/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* added `Dotenv::setProdEnvs()` and `Dotenv::usePutenv()`
8+
* made `Dotenv::loadEnv()` check for `env.local.php`
9+
* made Dotenv's constructor to accept `$envKey` and `$debugKey` arguments, to define
10+
the name of the env vars that configure the env name and debug settings
11+
* deprecated passing `$usePutenv` argument to Dotenv's constructor
12+
413
5.0.0
514
-----
615

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,47 @@ final class Dotenv
3535
private $data;
3636
private $end;
3737
private $values;
38-
private $usePutenv;
38+
private $envKey;
39+
private $debugKey;
40+
private $prodEnvs = ['prod'];
41+
private $usePutenv = false;
3942

4043
/**
41-
* @var bool If `putenv()` should be used to define environment variables or not.
42-
* Beware that `putenv()` is not thread safe, that's why this setting defaults to false
44+
* @param string|null $envKey
4345
*/
44-
public function __construct(bool $usePutenv = false)
46+
public function __construct($envKey = null, string $debugKey = null)
47+
{
48+
if (null !== $envKey && \in_array($envKey = (string) $envKey, ['1', ''], true)) {
49+
@trigger_error(sprintf('Passing a boolean to the constructor of "%s" is deprecated since Symfony 5.1, use "Dotenv::usePutenv()".', __CLASS__), E_USER_DEPRECATED);
50+
$this->usePutenv = (bool) $envKey;
51+
$envKey = null;
52+
}
53+
54+
$this->envKey = $envKey;
55+
$this->debugKey = $debugKey;
56+
}
57+
58+
/**
59+
* @return $this
60+
*/
61+
public function setProdEnvs(array $prodEnvs): self
62+
{
63+
$this->prodEnvs = $prodEnvs;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* @param bool $usePutenv If `putenv()` should be used to define environment variables or not.
70+
* Beware that `putenv()` is not thread safe, that's why this setting defaults to false
71+
*
72+
* @return $this
73+
*/
74+
public function usePutenv($usePutenv = true): self
4575
{
4676
$this->usePutenv = $usePutenv;
77+
78+
return $this;
4779
}
4880

4981
/**
@@ -67,32 +99,43 @@ public function load(string $path, string ...$extraPaths): void
6799
* .env.dist is loaded when it exists and .env is not found.
68100
*
69101
< 6D4E span class=pl-c> * @param string $path A file to load
70-
* @param string $varName The name of the env vars that defines the app env
102+
* @param string $envKey The name of the env vars that defines the app env
71103
* @param string $defaultEnv The app env to use when none is defined
72104
* @param array $testEnvs A list of app envs for which .env.local should be ignored
73105
*
74106
* @throws FormatException when a file has a syntax error
75107
* @throws PathException when a file does not exist or is not readable
76108
*/
77-
public function loadEnv(string $path, string $varName = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = ['test']): void
109+
public function loadEnv(string $path, string $envKey = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = ['test']): void
78110
{
111+
$envKey = null === $this->envKey || 1 < \func_num_args() ? $envKey : $this->envKey;
112+
113+
$p = $path.'.local.php';
114+
$env = (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($p)) || file_exists($p) ? include $p : null 9E7A ;
115+
116+
if (\is_array($env) && ($_SERVER[$envKey] ?? $_ENV[$envKey] ?? $env[$envKey] ?? null) === ($env[$envKey] ?? null)) {
117+
$this->populate($env);
118+
$env = $env[$envKey] ?? $defaultEnv;
119+
goto configure_debug;
120+
}
121+
79122
if (file_exists($path) || !file_exists($p = "$path.dist")) {
80123
$this->load($path);
81124
} else {
82125
$this->load($p);
83126
}
84127

85-
if (null === $env = $_SERVER[$varName] ?? $_ENV[$varName] ?? null) {
86-
$this->populate([$varName => $env = $defaultEnv]);
128+
if (null === $env = $_SERVER[$envKey] ?? $_ENV[$envKey] ?? null) {
129+
$this->populate([$envKey => $env = $defaultEnv]);
87130
}
88131

89132
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
90133
$this->load($p);
91-
$env = $_SERVER[$varName] ?? $_ENV[$varName] ?? $env;
134+
$env = $_SERVER[$envKey] ?? $_ENV[$envKey] ?? $env;
92135
}
93136

94137
if ('local' === $env) {
95-
return;
138+
goto configure_debug;
96139
}
97140

98141
if (file_exists($p = "$path.$env")) {
@@ -102,6 +145,14 @@ public function loadEnv(string $path, string $varName = 'APP_ENV', string $defau
102145
if (file_exists($p = "$path.$env.local")) {
103146
$this->load($p);
104147
}
148+
149+
configure_debug:
150+
if (null !== $debugKey = $this->debugKey) {
151+
$debug = $_SERVER[$debugKey] ?? $_ENV[$debugKey] ?? !\in_array($env, $this->prodEnvs);
152+
$_ENV[$debugKey] = (int) $debug || filter_var($debug, FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
153+
}
154+
155+
$_SERVER += $_ENV;
105156
}
106157

107158
/**

0 commit comments

Comments
 (0)
0