8000 [Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv… · symfony/symfony@035ac95 · GitHub
[go: up one dir, main page]

Skip to content

Commit 035ac95

Browse files
committed
[Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv to override existing vars
1 parent 414c78b commit 035ac95

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas
4040
* - "prod_envs" to define the names of the production envs - defaults to ["prod"];
4141
* - "test_envs" to define the names of the test envs - defaults to ["test"];
4242
* - "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.)
43+
* - "dotenv_overload" to tell Dotenv to override existing vars
4344
*
4445
* When the "debug" / "env" options are not defined, they will fallback to the
4546
* "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug"
@@ -84,6 +85,7 @@ class SymfonyRuntime extends GenericRuntime
8485
* use_putenv?: ?bool,
8586
* runtimes?: ?array,
8687
* error_handler?: string|false,
88+
* dotenv_overload?: ?bool,
8789
* } $options
8890
*/
8991
public function __construct(array $options = [])
@@ -96,10 +98,15 @@ public function __construct(array $options = [])
9698
}
9799

98100
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
99-
(new Dotenv())
101+
$fullDotenvPath = $options['project_dir'].'/'.($options['dotenv_path'] ?? '.env');
102+
($dotenv = new Dotenv())
100103
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
101104
->usePutenv($options['use_putenv'] ?? false)
102-
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']));
105+
->bootEnv($fullDotenvPath, 'dev', (array) ($options['test_envs'] ?? ['test']));
106+
if ($options['dotenv_overload'] ?? false) {
107+
$dotenv->overload($fullDotenvPath);
108+
}
109+
103110
$options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG'];
104111
$options['disable_dotenv'] = true;
105112
} else {

src/Symfony/Component/Runtime/Tests/phpt/autoload.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use Symfony\Component\Runtime\SymfonyRuntime;
44

5-
$_SERVER['APP_RUNTIME_OPTIONS'] = [
5+
$_SERVER['APP_RUNTIME_OPTIONS'] = $_SERVER['APP_RUNTIME_OPTIONS'] ?? [];
6+
$_SERVER['APP_RUNTIME_OPTIONS'] += [
67
'project_dir' => __DIR__,
78
];
89

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Symfony\Component\HttpFoundation\Request;
4+
use Symfony\Component\HttpFoundation\Response;
5+
6+
$_SERVER['SOME_VAR'] = 'ccc';
7+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
8+
'dotenv_overload' => true,
9+
];
10+
11+
require __DIR__.'/autoload.php';
12+
13+
return function (Request $request, array $context) {
14+
return new Response('OK Request '.$context['SOME_VAR']);
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Dotenv overload
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_overload.php';
9+
10+
?>
11+
--EXPECTF--
12+
OK Request foo_bar

0 commit comments

Comments
 (0)
0