Skip to content

Commit 9487088

Browse files
committed
[Dotenv] Use SYMFONY_DOTENV_PATH variable when dumping local 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 9487088

2 files changed

Lines changed: 40 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ protected function tearDown(): void
3636
{
3737
@unlink(__DIR__.'/.env');
3838
@unlink(__DIR__.'/.env.local');
39+
@unlink(__DIR__.'/.env.path');
40+
@unlink(__DIR__.'/.env.path.local');
3941
@unlink(__DIR__.'/.env.local.php');
42+
@unlink(__DIR__.'/.env.path.local.php');
4043
@unlink(__DIR__.'/composer.json');
44+
unset($_SERVER['SYMFONY_DOTENV_PATH']);
4145
}
4246

4347
public function testExecute()
@@ -92,6 +96,35 @@ public function testExecuteTestEnvs()
9296
], $vars);
9397
}
9498

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

0 commit comments

Comments
 (0)