8000 [Dotenv] Add $overrideExistingVars to bootEnv() and loadEnv() and dotenv_overload to SymfonyRuntime by fancyweb · Pull Request #43755 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Dotenv] Add $overrideExistingVars to bootEnv() and loadEnv() and dotenv_overload to SymfonyRuntime #43755

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
Oct 26, 2021
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Dotenv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `dotenv:dump` command to compile the contents of the .env files into a PHP-optimized file called `.env.local.php`
* Add `debug:dotenv` command to list all dotenv files with variables and values
* Add `$overrideExistingVars` on `Dotenv::bootEnv()` and `Dotenv::loadEnv()`

5.1.0
-----
Expand Down
22 changes: 10 additions & 12 deletions src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ public function load(string $path, string ...$extraPaths): void
* @throws FormatException when a file has a syntax error
* @throws PathException when a file does not exist or is not readable
*/
public function loadEnv(string $path, string $envKey = null, string $defaultEnv = 'dev', array $testEnvs = ['test']): void
public function loadEnv(string $path, string $envKey = null, string $defaultEnv = 'dev', array $testEnvs = ['test'], bool $overrideExistingVars = false): void
{
$k = $envKey ?? $this->envKey;

if (is_file($path) || !is_file($p = "$path.dist")) {
$this->load($path);
$this->doLoad($overrideExistingVars, [$path]);
} else {
$this->load($p);
$this->doLoad($overrideExistingVars, [$p]);
}

if (null === $env = $_SERVER[$k] ?? $_ENV[$k] ?? null) {
$this->populate([$k => $env = $defaultEnv]);
$this->populate([$k => $env = $defaultEnv], $overrideExistingVars);
}

if (!\in_array($env, $testEnvs, true) && is_file($p = "$path.local")) {
$this->load($p);
$this->doLoad($overrideExistingVars, [$p]);
$env = $_SERVER[$k] ?? $_ENV[$k] ?? $env;
}

Expand All @@ -130,11 +130,11 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
}

if (is_file($p = "$path.$env")) {
$this->load($p);
$this->doLoad($overrideExistingVars, [$p]);
}

if (is_file($p = "$path.$env.local")) {
$this->load($p);
$this->doLoad($overrideExistingVars, [$p]);
}
}

Expand All @@ -145,16 +145,16 @@ public function loadEnv(string $path, string $envKey = null, string $defaultEnv
*
* See method loadEnv() for rules related to .env files.
*/
public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test']): void
public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test'], bool $overrideExistingVars = false): void
{
$p = $path.'.local.php';
$env = is_file($p) ? include $p : null;
$k = $this->envKey;

if (\is_array($env) && (!isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
$this->populate($env);
$this->populate($env, $overrideExistingVars);
} else {
$this->loadEnv($path, $k, $defaultEnv, $testEnvs);
$this->loadEnv($path, $k, $defaultEnv, $testEnvs, $overrideExistingVars);
}

$_SERVER += $_ENV;
Expand Down Expand Up @@ -227,8 +227,6 @@ public function populate(array $values, bool $overrideExistingVars = false): voi
* @param string $data The data to be parsed
* @param string $path The original file name where data where stored (used for more meaningful error messages)
*
* @return array
*
* @throws FormatException when a file has a syntax error
*/
public function parse(string $data, string $path = '.env'): array
Expand Down
85 changes: 78 additions & 7 deletions src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,61 +236,117 @@ public function testLoadEnv()
putenv('SYMFONY_DOTENV_VARS');
putenv('FOO');
putenv('TEST_APP_ENV');

$_ENV['EXISTING_KEY'] = $_SERVER['EXISTING_KEY'] = 'EXISTING_VALUE';
putenv('EXISTING_KEY=EXISTING_VALUE');
};

@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');

$path = tempnam($tmpdir, 'sf-');

// .env
file_put_contents($path, 'FOO=BAR');
file_put_contents($path, "FOO=BAR\nEXISTING_KEY=NEW_VALUE");

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('BAR', getenv('FOO'));
$this->assertSame('dev', getenv('TEST_APP_ENV'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('BAR', getenv('FOO'));
$this->assertSame('dev', getenv('TEST_APP_ENV'));
$this->assertSame('NEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('NEW_VALUE', $_ENV['EXISTING_KEY']);

// .env.local
file_put_contents("$path.local", 'FOO=localBAR');
file_put_contents("$path.local", "FOO=localBAR\nEXISTING_KEY=localNEW_VALUE");

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'local';
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('localBAR', getenv('FOO'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'local';
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('localBAR', getenv('FOO'));
$this->assertSame('localNEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('localNEW_VALUE', $_ENV['EXISTING_KEY']);

// special case for test
$resetContext();
$_SERVER['TEST_APP_ENV'] = 'test';
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('BAR', getenv('FOO'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'test';
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('BAR', getenv('FOO'));
$this->assertSame('NEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('NEW_VALUE', $_ENV['EXISTING_KEY']);

// .env.dev
file_put_contents("$path.dev", 'FOO=devBAR');
file_put_contents("$path.dev", "FOO=devBAR\nEXISTING_KEY=devNEW_VALUE");

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('devBAR', getenv('FOO'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('devBAR', getenv('FOO'));
$this->assertSame('devNEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('devNEW_VALUE', $_ENV['EXISTING_KEY']);

// .env.dev.local
file_put_contents("$path.dev.local", 'FOO=devlocalBAR');
file_put_contents("$path.dev.local", "FOO=devlocalBAR\nEXISTING_KEY=devlocalNEW_VALUE");

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('devlocalBAR', getenv('FOO'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('devlocalBAR', getenv('FOO'));
$this->assertSame('devlocalNEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('devlocalNEW_VALUE', $_ENV['EXISTING_KEY']);
unlink("$path.local");
unlink("$path.dev");
unlink("$path.dev.local");

// .env.dist
file_put_contents("$path.dist", 'FOO=distBAR');
file_put_contents("$path.dist", "FOO=distBAR\nEXISTING_KEY=distNEW_VALUE");

$resetContext();
unlink($path);
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('distBAR', getenv('FOO'));
$this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

$resetContext();
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
$this->assertSame('distBAR', getenv('FOO'));
$this->assertSame('distNEW_VALUE', getenv('EXISTING_KEY'));
$this->assertSame('distNEW_VALUE', $_ENV['EXISTING_KEY']);
unlink("$path.dist");

$resetContext();
unset($_ENV['EXISTING_KEY'], $_SERVER['EXISTING_KEY']);
putenv('EXISTING_KEY');
rmdir($tmpdir);
}

Expand Down Expand Up @@ -490,22 +546,37 @@ public function testBootEnv()
unset($_SERVER['TEST_APP_ENV'], $_ENV['TEST_APP_ENV']);
unset($_SERVER['TEST_APP_DEBUG'], $_ENV['TEST_APP_DEBUG']);
unset($_SERVER['FOO'], $_ENV['FOO']);

$_ENV['EXISTING_KEY'] = $_SERVER['EXISTING_KEY'] = 'EXISTING_VALUE';
};

@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');
$path = tempnam($tmpdir, 'sf-');

file_put_contents($path, 'FOO=BAR');
file_put_contents($path, "FOO=BAR\nEXISTING_KEY=NEW_VALUE");
$resetContext();
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path);
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('EXISTING_VALUE', $_SERVER['EXISTING_KEY']);

$resetContext();
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path, 'dev', ['test'], true);
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('NEW_VALUE', $_SERVER['EXISTING_KEY']);
unlink($path);

file_put_contents($path.'.local.php', '<?php return ["TEST_APP_ENV" => "dev", "FOO" => "BAR"];');
file_put_contents($path.'.local.php', '<?php return ["TEST_APP_ENV" => "dev", "FOO" => "BAR", "EXISTING_KEY" => "localphpNEW_VALUE"];');
$resetContext();
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path);
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
$this->assertSame('EXISTING_VALUE', $_SERVER['EXISTING_KEY']);

$resetContext();
(new Dotenv('TEST_APP_ENV', 'TEST_APP_DEBUG'))->bootEnv($path, 'dev', ['test'], true);
$this->assertSame('BAR', $_SERVER['FOO']);
$this->assertSame('1', $_SERVER['TEST_APP_DEBUG']);
$this->assertSame('localphpNEW_VALUE', $_SERVER['EXISTING_KEY']);
unlink($path.'.local.php');

$resetContext();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* The component is not experimental anymore
* Add options "env_var_name" and "debug_var_name" to `GenericRuntime` and `SymfonyRuntime`
* Add option "dotenv_overload" to `SymfonyRuntime`

5.3.0
-----
Expand Down
4 changes: 3 additions & 1 deletion 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
* error_handler?: string|false,
* env_var_name?: string,
* debug_var_name?: string,
* dotenv_overload?: ?bool,
* } $options
*/
public function __construct(array $options = [])
Expand All @@ -102,7 +104,7 @@ public function __construct(array $options = [])
(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']));
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']), $options['dotenv_overload'] ?? false);
$options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey];
$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__,
] + ($_SERVER['APP_RUNTIME_OPTIONS'] ?? []);

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']);
};
14 changes: 14 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,14 @@
--TEST--
Test Dotenv overload
--SKIPIF--
<?php require dirname(__DIR__, 6).'/vendor/autoload.php'; if (4 > (new \ReflectionMethod(\Symfony\Component\Dotenv\Dotenv::class, 'bootEnv'))->getNumberOfParameters()) die('Skip because Dotenv version is too low');
--INI--
display_errors=1
--FILE--
<?php

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

?>
--EXPECTF--
OK Request foo_bar
0