diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 8ceb1fd484845..bad818fdc29e8 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -49,17 +49,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $envFiles = $this->getEnvFiles(); + $dotenvFile = $this->getDotenvFile(); + $envFiles = $this->getEnvFiles($dotenvFile); $availableFiles = array_filter($envFiles, function (string $file) { return is_file($this->getFilePath($file)); }); - if (\in_array('.env.local.php', $availableFiles, true)) { - $io->warning('Due to existing dump file (.env.local.php) all other dotenv files are skipped.'); + $localDotenvFile = sprintf('%s.local.php', $dotenvFile); + if (\in_array($localDotenvFile, $availableFiles, true)) { + $io->warning(sprintf('Due to existing dump file (%s) all other dotenv files are skipped.', $localDotenvFile)); } - if (is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) { - $io->warning('The file .env.dist gets skipped due to the existence of .env.'); + $distDotenvFile = sprintf('%s.dist', $dotenvFile); + if (is_file($this->getFilePath($dotenvFile)) && is_file($this->getFilePath($distDotenvFile))) { + $io->warning(sprintf('The file %s gets skipped due to the existence of %s.', $distDotenvFile, $dotenvFile)); } $io->section('Scanned Files (in descending priority)'); @@ -71,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->section('Variables'); $io->table( - array_merge(['Variable', 'Value'], $availableFiles), + array_merge(['Variable', 'Value'], array_map('basename', $availableFiles)), $this->getVariables($availableFiles) ); @@ -104,22 +107,22 @@ private function getVariables(array $envFiles): array return $output; } - private function getEnvFiles(): array + private function getEnvFiles(string $dotenvFile): array { $files = [ - '.env.local.php', - sprintf('.env.%s.local', $this->kernelEnvironment), - sprintf('.env.%s', $this->kernelEnvironment), + sprintf('%s.local.php', $dotenvFile), + sprintf('%s.%s.local', $dotenvFile, $this->kernelEnvironment), + sprintf('%s.%s', $dotenvFile, $this->kernelEnvironment), ]; if ('test' !== $this->kernelEnvironment) { - $files[] = '.env.local'; + $files[] = sprintf('%s.local', $dotenvFile); } - if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) { - $files[] = '.env.dist'; + if (!is_file($this->getFilePath($dotenvFile)) && is_file($this->getFilePath($distDotenvFile = sprintf('%s.dist', $dotenvFile)))) { + $files[] = $distDotenvFile; } else { - $files[] = '.env'; + $files[] = $dotenvFile; } return $files; @@ -140,4 +143,18 @@ private function loadValues(string $file): array return (new Dotenv())->parse(file_get_contents($filePath)); } + + private function getDotenvFile(): string + { + $projectDir = is_file($projectDir = $this->projectDirectory) ? basename($projectDir) : $projectDir; + + $composerFile = $projectDir.'/composer.json'; + if (is_file($composerFile)) { + $composerContent = json_decode(file_get_contents($composerFile), true); + + return $composerContent['extra']['runtime']['dotenv_path'] ?? '.env'; + } + + return '.env'; + } } diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index b3b089e4559c9..79bc5f6a5e88b 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -122,6 +122,28 @@ public function testScenario2InProdEnv() $this->assertStringContainsString('TEST 1234 1234 1234 0000', $output); } + public function testScenario4InProdEnv() + { + $output = $this->executeCommand(__DIR__.'/Fixtures/Scenario4', 'prod'); + + // Scanned Files + $this->assertStringContainsString('✓ sub/path/custom.env.local.php', $output); + $this->assertStringContainsString('⨯ sub/path/custom.env.prod.local', $output); + $this->assertStringContainsString('⨯ sub/path/custom.env.prod', $output); + $this->assertStringContainsString('✓ sub/path/custom.env.local', $output); + $this->assertStringContainsString('⨯ sub/path/custom.env'.\PHP_EOL, $output); + + // Skipped Files + $this->assertStringNotContainsString('custom.env.dist', $output); + $this->assertStringNotContainsString('custom.env.dev', $output); + $this->assertStringNotContainsString('custom.env.test', $output); + + // Variables + $this->assertStringContainsString('Variable Value custom.env.local.php custom.env.local', $output); + $this->assertStringContainsString('FOO BaR BaR n/a', $output); + $this->assertStringContainsString('TEST 1234 1234 1111', $output); + } + public function testWarningOnEnvAndEnvDistFile() { $output = $this->executeCommand(__DIR__.'/Fixtures/Scenario3', 'dev'); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.dist b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.dist new file mode 100644 index 0000000000000..96c193cbcb47d --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.dist @@ -0,0 +1 @@ +TEST=0000 diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.prod b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.prod new file mode 100644 index 0000000000000..b3116d1f1d898 --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.prod @@ -0,0 +1,2 @@ +FOO=BaR +TEST=1234 diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json new file mode 100644 index 0000000000000..3c7ca9f0c8127 --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json @@ -0,0 +1,7 @@ +{ + "extra": { + "runtime": { + "dotenv_path": "sub/path/custom.env" + } + } +} \ No newline at end of file diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local new file mode 100644 index 0000000000000..5770c3418bdfe --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local @@ -0,0 +1 @@ +TEST=1111 diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local.php b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local.php new file mode 100644 index 0000000000000..07744b5876e66 --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/custom.env.local.php @@ -0,0 +1,5 @@ + 'BaR', + 'TEST' => '1234', +];