8000 [Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv to override existing vars by fancyweb · Pull Request #41681 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Runtime] Add dotenv_overload option to SymfonyRuntime to tell Dotenv to override existing vars #41681

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions src/Symfony/Component/Runtime/SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class_exists(MissingDotenv::class, false) || class_exists(Dotenv::class) || clas
* - "prod_envs" to define the names of the production envs - defaults to ["prod"];
* - "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
*
* 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 @@ -84,6 +85,7 @@ class SymfonyRuntime extends GenericRuntime
* use_putenv?: ?bool,
* runtimes?: ?array,
* error_handler?: string|false,
* dotenv_overload?: ?bool,
* } $options
*/
public function __construct(array $options = [])
Expand All @@ -96,10 +98,15 @@ public function __construct(array $options = [])
}

if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
(new Dotenv())
$fullDotenvPath = $options['project_dir'].'/'.($options['dotenv_path'] ?? '.env');
($dotenv = new Dotenv())
->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']));
->bootEnv($fullDotenvPath, 'dev', (array) ($options['test_envs'] ?? ['test']));
if ($options['dotenv_overload'] ?? false) {
$dotenv->overload($fullDotenvPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, I would welcome a new argument $overload argument on bootEnv()

Copy link
Contributor Author
@fancyweb fancyweb Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean calling $this->overload() at the end of bootEnv()?

Copy link
Member
@nicolas-grekas nicolas-grekas Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean calling populate() with $overrideExistingVars and doLoad() with the same arg instead of load()

}

$options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG'];
$options['disable_dotenv'] = true;
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Runtime/Tests/phpt/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use Symfony\Component\Runtime\SymfonyRuntime;

$_SERVER['APP_RUNTIME_OPTIONS'] = [
$_SERVER['APP_RUNTIME_OPTIONS'] = $_SERVER['APP_RUNTIME_OPTIONS'] ?? [];
$_SERVER['APP_RUNTIME_OPTIONS'] += [
'project_dir' => __DIR__,
];

Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/dotenv_overload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

$_SERVER['SOME_VAR'] = 'ccc';
$_SERVER['APP_RUNTIME_OPTIONS'] = [
'dotenv_overload' => true,
];

require __DIR__.'/autoload.php';

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

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

?>
--EXPECTF--
OK Request foo_bar
0