Skip to content

[Dotenv] Add SYMFONY_DOTENV_PATH, consumed by debug:dotenv for custom .env path #52638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Dotenv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add `SYMFONY_DOTENV_PATH` variable with the path to the `.env` file loaded by `Dotenv::loadEnv()` or `Dotenv::bootEnv()`

6.2
---

Expand Down
50 changes: 27 additions & 23 deletions src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$envFiles = $this->getEnvFiles();
$availableFiles = array_filter($envFiles, fn (string $file) => is_file($this->getFilePath($file)));
$filePath = $_SERVER['SYMFONY_DOTENV_PATH'] ?? $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env';

if (\in_array('.env.local.php', $availableFiles, true)) {
$io->warning('Due to existing dump file (.env.local.php) all other dotenv files are skipped.');
$envFiles = $this->getEnvFiles($filePath);
$availableFiles = array_filter($envFiles, fn (string $file) => is_file($file));

if (\in_array(sprintf('%s.local.php', $filePath), $availableFiles, true)) {
$io->warning(sprintf('Due to existing dump file (%s.local.php) all other dotenv files are skipped.', $this->getRelativeName($filePath)));
}

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.');
if (is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
$io->warning(sprintf('The file %s.dist gets skipped due to the existence of %1$s.', $this->getRelativeName($filePath)));
}

$io->section('Scanned Files (in descending priority)');
$io->listing(array_map(static fn (string $envFile) => \in_array($envFile, $availableFiles, true)
? sprintf('<fg=green>✓</> %s', $envFile)
: sprintf('<fg=red>⨯</> %s', $envFile), $envFiles));
$io->listing(array_map(fn (string $envFile) => \in_array($envFile, $availableFiles, true)
? sprintf('<fg=green>✓</> %s', $this->getRelativeName($envFile))
: sprintf('<fg=red>⨯</> %s', $this->getRelativeName($envFile)), $envFiles));

$nameFilter = $input->getArgument('filter');
$variables = $this->getVariables($availableFiles, $nameFilter);
Expand All @@ -93,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if ($variables || null === $nameFilter) {
$io->table(
array_merge(['Variable', 'Value'], $availableFiles),
array_merge(['Variable', 'Value'], array_map($this->getRelativeName(...), $availableFiles)),
$this->getVariables($availableFiles, $nameFilter)
);

Expand Down Expand Up @@ -147,36 +149,38 @@ private function getAvailableVars(): array
return $vars;
}

private function getEnvFiles(): array
private function getEnvFiles(string $filePath): array
{
$files = [
'.env.local.php',
sprintf('.env.%s.local', $this->kernelEnvironment),
sprintf('.env.%s', $this->kernelEnvironment),
sprintf('%s.local.php', $filePath),
sprintf('%s.%s.local', $filePath, $this->kernelEnvironment),
sprintf('%s.%s', $filePath, $this->kernelEnvironment),
];

if ('test' !== $this->kernelEnvironment) {
$files[] = '.env.local';
$files[] = sprintf('%s.local', $filePath);
}

if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
$files[] = '.env.dist';
if (!is_file($filePath) && is_file(sprintf('%s.dist', $filePath))) {
$files[] = sprintf('%s.dist', $filePath);
} else {
$files[] = '.env';
$files[] = $filePath;
}

return $files;
}

private function getFilePath(string $file): string
private function getRelativeName(string $filePath): string
{
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
if (str_starts_with($filePath, $this->projectDirectory)) {
return substr($filePath, \strlen($this->projectDirectory) + 1);
}

return basename($filePath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only display the basename if the dotenv file is outside of the project directory.

}

private function loadValues(string $file): array
private function loadValues(string $filePath): array
{
$filePath = $this->getFilePath($file);

if (str_ends_with($filePath, '.php')) {
return include $filePath;
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private function loadEnv(string $dotenvPath, string $env, array $config): array
try {
$dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs);
unset($_ENV['SYMFONY_DOTENV_VARS']);
unset($_ENV['SYMFONY_DOTENV_PATH']);

return $_ENV;
} finally {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Dotenv/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public function load(string $path, string ...$extraPaths): void
*/
public function loadEnv(string $path, string $envKey = null, string $defaultEnv = 'dev', array $testEnvs = ['test'], bool $overrideExistingVars = false): void
{
$this->populatePath($path);

$k = $envKey ?? $this->envKey;

if (is_file($path) || !is_file($p = "$path.dist")) {
Expand Down Expand Up @@ -144,6 +146,7 @@ public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnv
$k = $this->envKey;

if (\is_array($env) && ($overrideExistingVars || !isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
$this->populatePath($path);
$this->populate($env, $overrideExistingVars);
} else {
$this->loadEnv($path, $k, $defaultEnv, $testEnvs, $overrideExistingVars);
Expand Down Expand Up @@ -556,4 +559,13 @@ private function doLoad(bool $overrideExistingVars, array $paths): void
$this->populate($this->parse(file_get_contents($path), $path), $overrideExistingVars);
}
}

private function populatePath(string $path): void
{
$_ENV['SYMFONY_DOTENV_PATH'] = $_SERVER['SYMFONY_DOTENV_PATH'] = $path;

if ($this->usePutenv) {
putenv('SYMFONY_DOTENV_PATH='.$path);
}
}
}
42 changes: 40 additions & 2 deletions src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ public function testScenario2InProdEnv()
$this->assertStringContainsString('TEST 1234 1234 1234 0000', $output);
}

public function testScenario2WithCustomPath()
{
$output = $this->executeCommand(__DIR__.'/Fixtures', 'prod', [], __DIR__.'/Fixtures/Scenario2/.env');

// Scanned Files
$this->assertStringContainsString('✓ Scenario2/.env.local.php', $output);
$this->assertStringContainsString('⨯ Scenario2/.env.prod.local', $output);
$this->assertStringContainsString('✓ Scenario2/.env.prod', $output);
$this->assertStringContainsString('⨯ Scenario2/.env.local', $output);
$this->assertStringContainsString('✓ Scenario2/.env.dist', $output);

// Skipped Files
$this->assertStringNotContainsString('.env'.\PHP_EOL, $output);
$this->assertStringNotContainsString('.env.dev', $output);
$this->assertStringNotContainsString('.env.test', $output);

// Variables
$this->assertStringContainsString('Variable Value Scenario2/.env.local.php Scenario2/.env.prod Scenario2/.env.dist', $output);
$this->assertStringContainsString('FOO BaR BaR BaR n/a', $output);
$this->assertStringContainsString('TEST 1234 1234 1234 0000', $output);
}

public function testWarningOnEnvAndEnvDistFile()
{
$output = $this->executeCommand(__DIR__.'/Fixtures/Scenario3', 'dev');
Expand All @@ -132,6 +154,14 @@ public function testWarningOnEnvAndEnvDistFile()
$this->assertStringContainsString('[WARNING] The file .env.dist gets skipped', $output);
}

public function testWarningOnEnvAndEnvDistFileWithCustomPath()
{
$output = $this->executeCommand(__DIR__.'/Fixtures', 'dev', dotenvPath: __DIR__.'/Fixtures/Scenario3/.env');

// Warning
$this->assertStringContainsString('[WARNING] The file Scenario3/.env.dist gets skipped', $output);
}

public function testWarningOnPhpEnvFile()
{
$output = $this->executeCommand(__DIR__.'/Fixtures/Scenario2', 'prod');
Expand All @@ -140,6 +170,14 @@ public function testWarningOnPhpEnvFile()
$this->assertStringContainsString('[WARNING] Due to existing dump file (.env.local.php)', $output);
}

public function testWarningOnPhpEnvFileWithCustomPath()
{
$output = $this->executeCommand(__DIR__.'/Fixtures', 'prod', dotenvPath: __DIR__.'/Fixtures/Scenario2/.env');

// Warning
$this->assertStringContainsString('[WARNING] Due to existing dump file (Scenario2/.env.local.php)', $output);
}

public function testScenario1InDevEnvWithNameFilter()
{
$output = $this->executeCommand(__DIR__.'/Fixtures/Scenario1', 'dev', ['filter' => 'FoO']);
Expand Down Expand Up @@ -226,10 +264,10 @@ public function testCompletion()
$this->assertSame(['FOO', 'TEST'], $tester->complete(['']));
}

private function executeCommand(string $projectDirectory, string $env, array $input = []): string
private function executeCommand(string $projectDirectory, string $env, array $input = [], string $dotenvPath = null): string
{
$_SERVER['TEST_ENV_KEY'] = $env;
(new Dotenv('TEST_ENV_KEY'))->bootEnv($projectDirectory.'/.env');
(new Dotenv('TEST_ENV_KEY'))->bootEnv($dotenvPath ?? $projectDirectory.'/.env');

$command = new DebugCommand($env, $projectDirectory);
$command->setHelperSet(new HelperSet([new FormatterHelper()]));
Expand Down