Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Dotenv] DebugCommand must respect dotenv_path runtime configuration #47901

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

Closed
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
45 changes: 31 additions & 14 deletions src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand All @@ -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)
);

Expand Down Expand Up @@ -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;
Expand All @@ -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';
Copy link
Member

@GromNaN GromNaN Nov 18, 2022

Choose a reason for hiding this comment

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

I see the dotenv_path can be configured for Symfony Runtime in APP_RUNTIME_OPTIONS env var. We can look at it too, but I don't know what is the priority.

* In addition to the options managed by GenericRuntime, it accepts the following options:
* - "env" to define the name of the environment the app runs in;
* - "disable_dotenv" to disable looking for .env files;
* - "dotenv_path" to define the path of dot-env files - defaults to ".env";

$runtime = $_SERVER['APP_RUNTIME'] ?? $_ENV['APP_RUNTIME'] ?? %runtime_class%;
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);

if (is_file($composerFile)) {
$composerContent = json_decode(file_get_contents($composerFile), true);

return $composerContent['extra']['runtime']['dotenv_path'] ?? '.env';
}

return '.env';
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST=0000
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOO=BaR
TEST=1234
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extra": {
"runtime": {
"dotenv_path": "sub/path/custom.env"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST=1111
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return [
'FOO' => 'BaR',
'TEST' => '1234',
];