8000 [Runtime] Support extra dot-env files by natepage · Pull Request #60139 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Runtime] Support extra dot-env files #60139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Symfony/Component/Runtime/SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas
* - "test_envs" to define the names of the test envs - defaults to ["test"];
* - "use_putenv" to tell Dotenv to set env vars using putenv() (NOT RECOMMENDED.)
* - "dotenv_overload" to tell Dotenv to override existing vars
* - "dotenv_extra_paths" to define a list of additional dot-env files
*
* When the "debug" / "env" options are not defined, they will fallback to the
* "APP_DEBUG" / "APP_ENV" environment variables, and to the "--env|-e" / "--no-debug"
Expand Down Expand Up @@ -86,6 +87,7 @@ class SymfonyRuntime extends GenericRuntime
* env_var_name?: string,
* debug_var_name?: string,
* dotenv_overload?: ?bool,
* dotenv_extra_paths?: ?string[],
* } $options
*/
public function __construct(array $options = [])
Expand All @@ -107,12 +109,22 @@ public function __construct(array $options = [])
}

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

if (isset($this->input) && ($options['dotenv_overload'] ?? false)) {
$dotenv->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $overrideExistingVars);

if (\is_array($options['dotenv_extra_paths'] ?? null) && $options['dotenv_extra_paths']) {
$options['dotenv_extra_paths'] = array_map(fn (string $path) => $options['project_dir'].'/'.$path, $options['dotenv_extra_paths']);

$overrideExistingVars
? $dotenv->overload(...$options['dotenv_extra_paths'])
: $dotenv->load(...$options['dotenv_extra_paths']);
}

if (isset($this->input) && $overrideExistingVars) {
if ($this->input->getParameterOption(['--env', '-e'], $_SERVER[$envKey], true) !== $_SERVER[$envKey]) {
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));
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/.env.extra
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_VAR=foo_bar_extra
25 changes: 25 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/dotenv_extra_load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$_SERVER['SOME_VAR'] = 'ccc';
$_SERVER['APP_RUNTIME_OPTIONS'] = [
'dotenv_extra_paths' => [
'.env.extra',
],
'dotenv_overload' => false,
];

require __DIR__.'/autoload.php';

return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']);
12 changes: 12 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/dotenv_extra_load.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test Dotenv extra paths load
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_extra_load.php';

?>
--EXPECTF--
OK Request ccc
25 changes: 25 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/dotenv_extra_overload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
AB05 * file that was distributed with this source code.
*/

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$_SERVER['SOME_VAR'] = 'ccc';
$_SERVER['APP_RUNTIME_OPTIONS'] = [
'dotenv_extra_paths' => [
'.env.extra',
],
'dotenv_overload' => true,
];

require __DIR__.'/autoload.php';

return fn (Request $request, array $context) => new Response('OK Request '.$context['SOME_VAR']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test Dotenv extra paths overload
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/dotenv_extra_overload.php';

?>
--EXPECTF--
OK Request foo_bar_extra
Loading
0