Skip to content

Commit 376abbe

Browse files
[Dotenv] Fix reading config for symfony/runtime when running dump command
1 parent 3784dbc commit 376abbe

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
*
2525
* @internal
2626
*/
27-
#[Autoconfigure(bind: ['$dotenvPath' => '%kernel.project_dir%/.env', '$defaultEnv' => '%kernel.environment%'])]
27+
#[Autoconfigure(bind: ['$projectDir' => '%kernel.project_dir%', '$defaultEnv' => '%kernel.environment%'])]
2828
final class DotenvDumpCommand extends Command
2929
{
3030
protected static $defaultName = 'dotenv:dump';
3131
protected static $defaultDescription = 'Compiles .env files to .env.local.php';
3232

33-
private $dotenvPath;
33+
private $projectDir;
3434
private $defaultEnv;
3535

36-
public function __construct(string $dotenvPath, string $defaultEnv = null)
36+
public function __construct(string $projectDir, string $defaultEnv = null)
3737
{
38-
$this->dotenvPath = $dotenvPath;
38+
$this->projectDir = $projectDir;
3939
$this->defaultEnv = $defaultEnv;
4040

4141
parent::__construct();
@@ -65,13 +65,23 @@ protected function configure()
6565
*/
6666
protected function execute(InputInterface $input, OutputInterface $output): int
6767
{
68+
$config = [];
69+
if (is_file($projectDir = $this->projectDir)) {
70+
$config = ['dotenv_path' => basename($projectDir)];
71+
$projectDir = \dirname($projectDir);
72+
}
73+
74+
$composerFile = $projectDir.'/composer.json';
75+
$config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
76+
$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
6877
$env = $input->getArgument('env') ?? $this->defaultEnv;
78+
$envKey = $config['env_var_name'] ?? 'APP_ENV';
6979

7080
if ($input->getOption('empty')) {
71-
$vars = ['APP_ENV' => $env];
81+
$vars = [$envKey => $env];
7282
} else {
73-
$vars = $this->loadEnv($env);
74-
$env = $vars['APP_ENV'];
83+
$vars = $this->loadEnv($dotenvPath, $env, $config);
84+
$env = $vars[$envKey];
7585
}
7686

7787
$vars = var_export($vars, true);
@@ -83,26 +93,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8393
return $vars;
8494
8595
EOF;
86-
file_put_contents($this->dotenvPath.'.local.php', $vars, \LOCK_EX);
96+
file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX);
8797

8898
$output->writeln(sprintf('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment.', $env));
8999

90100
return 0;
91101
}
92102

93-
private function loadEnv(string $env): array
103+
private function loadEnv(string $dotenvPath, string $env, array $config): array
94104
{
95105
$dotenv = new Dotenv();
96-
$composerFile = \dirname($this->dotenvPath).'/composer.json';
97-
$testEnvs = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime']['test_envs'] ?? ['test'];
106+
$envKey = $config['env_var_name'] ?? 'APP_ENV';
107+
$testEnvs = $config['test_envs'] ?? ['test'];
98108

99109
$globalsBackup = [$_SERVER, $_ENV];
100-
unset($_SERVER['APP_ENV']);
101-
$_ENV = ['APP_ENV' => $env];
110+
unset($_SERVER[$envKey]);
111+
$_ENV = [$envKey => $env];
102112
$_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER));
103113

104114
try {
105-
$dotenv->loadEnv($this->dotenvPath, null, 'dev', $testEnvs);
115+
$dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs);
106116
unset($_ENV['SYMFONY_DOTENV_VARS']);
107117

108118
return $_ENV;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testExecuteTestEnvs()
9595
private function createCommand(): CommandTester
9696
{
9797
$application = new Application();
98-
$application->add(new DotenvDumpCommand(__DIR__.'/.env'));
98+
$application->add(new DotenvDumpCommand(__DIR__));
9999

100100
return new CommandTester($application->find('dotenv:dump'));
101101
}

0 commit comments

Comments
 (0)