Skip to content

Navigation Menu

Sign in
Appearance settings

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 fda552e

Browse filesBrowse files
natepagefabpot
authored andcommitted
[Runtime] Support extra dot-env files
1 parent b495af9 commit fda552e
Copy full SHA for fda552e

File tree

Expand file treeCollapse file tree

6 files changed

+91
-4
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+91
-4
lines changed

‎SymfonyRuntime.php

Copy file name to clipboardExpand all lines: SymfonyRuntime.php
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas
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.)
4343
* - "dotenv_overload" to tell Dotenv to override existing vars
44+
* - "dotenv_extra_paths" to define a list of additional dot-env files
4445
*
4546
* When the "debug" / "env" options are not defined, they will fallback to the
4647
* "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug"
@@ -86,6 +87,7 @@ class SymfonyRuntime extends GenericRuntime
8687
* env_var_name?: string,
8788
* debug_var_name?: string,
8889
* dotenv_overload?: ?bool,
90+
* dotenv_extra_paths?: ?string[],
8991
* } $options
9092
*/
9193
public function __construct(array $options = [])
@@ -107,12 +109,22 @@ public function __construct(array $options = [])
107109
}
108110

109111
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
110-
(new Dotenv($envKey, $debugKey))
112+
$overrideExistingVars = $options['dotenv_overload'] ?? false;
113+
$dotenv = (new Dotenv($envKey, $debugKey))
111114
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
112-
->usePutenv($options['use_putenv'] ?? false)
113-
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false);
115+
->usePutenv($options['use_putenv'] ?? false);
114116

115-
if (isset($this->input) && ($options['dotenv_overload'] ?? false)) {
117+
$dotenv->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $overrideExistingVars);
118+
119+
if (\is_array($options['dotenv_extra_paths'] ?? null) && $options['dotenv_extra_paths']) {
120+
$options['dotenv_extra_paths'] = array_map(fn (string $path) => $options['project_dir'].'/'.$path, $options['dotenv_extra_paths']);
121+
122+
$overrideExistingVars
123+
? $dotenv->overload(...$options['dotenv_extra_paths'])
124+
: $dotenv->load(...$options['dotenv_extra_paths']);
125+
}
126+
127+
if (isset($this->input) && $overrideExistingVars) {
116128
if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) {
117129
throw new \LogicException(\sprintf('Cannot use "--env" or "-e" when the "%s" file defines "%s" and the "dotenv_overload" runtime option is true.', $options['dotenv_path'] ?? '.env', $envKey));
118130
}

‎Tests/phpt/.env.extra

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOME_VAR=foo_bar_extra

‎Tests/phpt/dotenv_extra_load.php

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
$_SERVER['SOME_VAR'] = 'ccc';
16+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
17+
'dotenv_extra_paths' => [
18+
'.env.extra',
19+
],
20+
'dotenv_overload' => false,
21+
];
22+
23+
require __DIR__.'/autoload.php';
24+
25+
return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']);

‎Tests/phpt/dotenv_extra_load.phpt

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Dotenv extra paths load
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_extra_load.php';
9+
10+
?>
11+
--EXPECTF--
12+
OK Request ccc

‎Tests/phpt/dotenv_extra_overload.php

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
$_SERVER['SOME_VAR'] = 'ccc';
16+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
17+
'dotenv_extra_paths' => [
18+
'.env.extra',
19+
],
20+
'dotenv_overload' => true,
21+
];
22+
23+
require __DIR__.'/autoload.php';
24+
25+
return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']);

‎Tests/phpt/dotenv_extra_overload.phpt

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Dotenv extra paths overload
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_extra_overload.php';
9+
10+
?>
11+
--EXPECTF--
12+
OK Request foo_bar_extra

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.