8000 [Dotenv] Add Dotenv::bootEnv() to check for .env.local.php before cal… · symfony/symfony@d66bcd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d66bcd9

Browse files
[Dotenv] Add Dotenv::bootEnv() to check for .env.local.php before calling Dotenv::loadEnv()
1 parent 37a8863 commit d66bcd9

File tree

9 files changed

+118
-40
lines changed

9 files changed

+118
-40
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+
< 10000 /div>
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::bootEnv()` to check for `.env.local.php` before calling `Dotenv::loadEnv()`
8+
* added `Dotenv::setProdEnvs()` and `Dotenv::usePutenv()`
9+
* made Dotenv's constructor 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: 70 additions & 9 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,28 +99,30 @@ public function load(string $path, string ...$extraPaths): void
6799
* .env.dist is loaded when it exists and .env is not found.
68100
*
69101
* @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+
79113
if (file_exists($path) || !file_exists($p = "$path.dist")) {
80114
$this->load($path);
81115
} else {
82116
$this->load($p);
83117
}
84118

85-
if (null === $env = $_SERVER[$varName] ?? $_ENV[$varName] ?? null) {
86-
$this->populate([$varName => $env = $defaultEnv]);
119+
if (null === $env = $_SERVER[$envKey] ?? $_ENV[$envKey] ?? null) {
120+
$this->populate([$envKey => $env = $defaultEnv]);
87121
}
88122

89123
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
90124
$this->load($p);
91-
$env = $_SERVER[$varName] ?? $_ENV[$varName] ?? $env;
125+
$env = $_SERVER[$envKey] ?? $_ENV[$envKey] ?? $env;
92126
}
93127

94128
if ('local' === $env) {
@@ -104,6 +138,33 @@ public function loadEnv(string $path, string $varName = 'APP_ENV', string $defau
104138
}
105139
}
106140

141+
/**
142+
* Loads env vars from .env.local.php if the file exists or from the other .env files otherwise.
143+
*
144+
* This method also configures the APP_DEBUG env var according to the current APP_ENV.
145+
*
146+
* See method loadEnv() for rules related to .env files.
147+
*/
148+
public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test']): void
149+
{
150+
$p = $path.'.local.php';
151+
$env = (\function_exists('opcache_is_script_cached') && @opcache_is_script_cached($p)) || file_exists($p) ? include $p : null;
152+
$envKey = $this->envKey ?? 'APP_ENV';
153+
154+
if (\is_array($env) && ($_SERVER[$envKey] ?? $_ENV[$envKey] ?? $env[$envKey] ?? null) === ($env[$envKey] ?? null)) {
155+
$this->populate($env);
156+
9EE3 } else {
157+
$this->loadEnv($path, $envKey, $defaultEnv, $testEnvs);
158+
}
159+
160+
if (null !== $debugKey = $this->debugKey) {
161+
$debug = $_SERVER[$debugKey] ?? $_ENV[$debugKey] ?? !\in_array($_SERVER[$envKey] ?? $_ENV[$envKey], $this->prodEnvs, true);
162+
$_ENV[$debugKey] = (int) $debug || (!\is_bool($debug) && filter_var($debug, FILTER_VALIDATE_BOOLEAN)) ? '1' : '0';
163+
}
164+
165+
$_SERVER += $_ENV;
166+
}
167+
107168
/**
108169
* Loads one or several .env files and enables override existing vars.
109170
*

0 commit comments

Comments
 (0)
0