8000 [Runtime] Support extra dot-env files · symfony/symfony@22d505a · GitHub
[go: up one dir, main page]

Skip to content

Commit 22d505a

Browse files
natepagefabpot
authored andcommitted
[Runtime] Support extra dot-env files
1 parent 59d8c63 commit 22d505a

File tree

6 files changed

+91
-4
lines changed

6 files changed

+91
-4
lines changed

src/Symfony/Component/Runtime/SymfonyRuntime.php

+16-4
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOME_VAR=foo_bar_extra
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']);
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
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']);
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