Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08319b2

Browse filesBrowse files
committed
[Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv to override existing vars
1 parent 1fac9ee commit 08319b2
Copy full SHA for 08319b2

File tree

4 files changed

+37
-3
lines changed
Filter options

4 files changed

+37
-3
lines changed

‎src/Symfony/Component/Runtime/SymfonyRuntime.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Runtime/SymfonyRuntime.php
+8-2Lines changed: 8 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,14 @@ 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+
($dotenv = new Dotenv())
100102
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
101103
->usePutenv($options['use_putenv'] ?? false)
102-
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']));
104+
->bootEnv($fullDotenvPath = $options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']));
105+
if ($options['dotenv_overload'] ?? false) {
106+
$dotenv->overload($fullDotenvPath);
107+
}
108+
103109
$options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG'];
104110
$options['disable_dotenv'] = true;
105111
} else {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Runtime/Tests/phpt/autoload.php
+2-1Lines 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

+15Lines 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+
};
+12Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.