Skip to content

Commit 0a28a56

Browse files
committed
[Dotenv] Use SYMFONY_DOTENV_PATH variable when dumping dotenv
When overriding the `dotenv_path` directly in the `index.php` or `console` file like this: ```php // public/index.php use App\Kernel; require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; $_SERVER['APP_RUNTIME_OPTIONS'] = [ 'dotenv_path' => '.env.path' ]; return function (array $context): Kernel { return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); }; ``` The `dotenv:dump` command was ignoring this value and used the default `.env` path, which resulted in two things: * The file was still generated (with wrong values), but ... * It was never used since the application itself used the `dotenv_path` from the runtime options and could not find the corresponding `*.local.php` file in the updated path To fix this, the command itself now checks if the `SYMFONY_DOTENV_PATH` env variable is set, and if it is, its value is used instead of checking the `composer.json` file or falling back to the `.env` file.
1 parent 1c7c255 commit 0a28a56

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6666
$projectDir = \dirname($projectDir);
6767
}
6868

69-
$composerFile = $projectDir.'/composer.json';
70-
$config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
71-
$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
69+
if (null === $dotenvPath = $_SERVER['SYMFONY_DOTENV_PATH'] ?? null) {
70+
$composerFile = $projectDir.'/composer.json';
71+
$config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
72+
$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
73+
}
74+
7275
$env = $input->getArgument('env') ?? $this->defaultEnv;
7376
$envKey = $config['env_var_name'] ?? 'APP_ENV';
7477

@@ -90,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9093
EOF;
9194
file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX);
9295

93-
$output->writeln(\sprintf('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment.', $env));
96+
$output->writeln(\sprintf('Successfully dumped .env files in <info>%s.local.php</> for the <info>%s</> environment.', basename($dotenvPath), $env));
9497

9598
return 0;
9699
}

src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class DotenvDumpCommandTest extends TestCase
2020
{
2121
protected function setUp(): void
2222
{
23+
unset($_SERVER['SYMFONY_DOTENV_PATH']);
24+
2325
file_put_contents(__DIR__.'/.env', <<<EOF
2426
APP_ENV=dev
2527
APP_SECRET=abc123
@@ -36,8 +38,13 @@ protected function tearDown(): void
3638
{
3739
@unlink(__DIR__.'/.env');
3840
@unlink(__DIR__.'/.env.local');
41+
@unlink(__DIR__.'/.env.path');
42+
@unlink(__DIR__.'/.env.path.local');
3943
@unlink(__DIR__.'/.env.local.php');
44+
@unlink(__DIR__.'/.env.path.local.php');
4045
@unlink(__DIR__.'/composer.json');
46+
47+
unset($_SERVER['SYMFONY_DOTENV_PATH']);
4148
}
4249

4350
public function testExecute()
@@ -92,6 +99,35 @@ public function testExecuteTestEnvs()
9299
], $vars);
93100
}
94101

102+
public function testExecuteSymfonyDotEnvPath()
103+
{
104+
file_put_contents(__DIR__.'/.env.path', <<<EOF
105+
APP_ENV=test
106+
APP_SECRET=newpath123
107+
EOF
108+
);
109+
file_put_contents(__DIR__.'/.env.path.local', <<<EOF
110+
LOCAL_PATH=yes
111+
EOF
112+
);
113+
114+
$_SERVER['SYMFONY_DOTENV_PATH'] = __DIR__.'/.env.path';
115+
116+
$command = $this->createCommand();
117+
$command->execute([
118+
'env' => 'dev',
119+
]);
120+
121+
$this->assertFileExists(__DIR__.'/.env.path.local.php');
122+
123+
$vars = require __DIR__.'/.env.path.local.php';
124+
$this->assertSame([
125+
'APP_ENV' => 'dev',
126+
'APP_SECRET' => 'newpath123',
127+
'LOCAL_PATH' => 'yes',
128+
], $vars);
129+
}
130+
95131
private function createCommand(): CommandTester
96132
{
97133
$application = new Application();

0 commit comments

Comments
 (0)