diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 237d7b7cfd228..55585e8a722e3 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -50,7 +50,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $filePath = $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env'; + $composerFile = $this->projectDirectory . '/composer.json'; + $config = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? []; + $filePath = $this->projectDirectory.'/'.($config['dotenv_path'] ?? '.env'); $envFiles = $this->getEnvFiles($filePath); $availableFiles = array_filter($envFiles, 'is_file'); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index 001baec0c2539..64ad618dfffec 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -167,10 +167,27 @@ public function testWarningOnPhpEnvFile() $this->assertStringContainsString('[WARNING] Due to existing dump file (.env.local.php)', $output); } - private function executeCommand(string $projectDirectory, string $env): string + public function testLoadEnvFilesFromSubdirectory() + { + $projectDirectory = __DIR__.'/Fixtures/Scenario4'; + file_put_contents($projectDirectory.'/composer.json', '{"extra":{"runtime":{"dotenv_path": "config/.env"}}}'); + + $output = $this->executeCommand($projectDirectory, 'dev', 'config/.env'); + + @unlink($projectDirectory.'/composer.json'); + + // Scanned Files + $this->assertStringContainsString('⨯ config/.env.local.php', $output); + $this->assertStringContainsString('⨯ config/.env.dev.local', $output); + $this->assertStringContainsString('⨯ config/.env.dev', $output); + $this->assertStringContainsString('✓ config/.env.local', $output); + $this->assertStringContainsString('✓ config/.env'.\PHP_EOL, $output); + } + + private function executeCommand(string $projectDirectory, string $env, string $envPath = '.env'): string { $_SERVER['TEST_ENV_KEY'] = $env; - (new Dotenv('TEST_ENV_KEY'))->bootEnv($projectDirectory.'/.env'); + (new Dotenv('TEST_ENV_KEY'))->bootEnv($projectDirectory.'/'.$envPath); $command = new DebugCommand($env, $projectDirectory); $command->setHelperSet(new HelperSet([new FormatterHelper()])); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env new file mode 100644 index 0000000000000..dbc9b1ecab0ea --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env @@ -0,0 +1,2 @@ +TEST123=true +FOO=bar diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local new file mode 100644 index 0000000000000..c6a735ee12086 --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local @@ -0,0 +1 @@ +FOO=baz