8000 [Dotenv] DebugCommand must respect dotenv_path runtime configuration by alamirault · Pull Request #47901 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
[Dotenv] DebugCommand must respect dotenv_path runtime configuration
  • Loading branch information
alamirault committed Oct 18, 2022
commit a56b24d056ababc8890e44031eb10e6de03448c1
52 changes: 36 additions & 16 deletions src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$envFiles = $this->getEnvFiles();
$availableFiles = array_filter($envFiles, function (string $file) {
return is_file($this->getFilePath($file));
$dotenvDirectory = $this->getDotenvDirectory();
$envFiles = $this->getEnvFiles($dotenvDirectory);
$availableFiles = array_filter($envFiles, function (string $file) use ($dotenvDirectory) {
return is_file($this->getFilePath($dotenvDirectory, $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.');
}

if (is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
if (is_file($this->getFilePath($dotenvDirectory, '.env')) && is_file($this->getFilePath($dotenvDirectory, '.env.dist'))) {
$io->warning('The file .env.dist gets skipped due to the existence of .env.');
}

$io->section('Scanned Files (in descending priority)');
$io->listing(array_map(static function (string $envFile) use ($availableFiles) {
$io->listing(array_map(static function (string $envFile) use ($availableFiles, $dotenvDirectory) {
$file = (null === $dotenvDirectory ? '' : $dotenvDirectory.\DIRECTORY_SEPARATOR).$envFile;

return \in_array($envFile, $availableFiles, true)
? sprintf('<fg=green>✓</> %s', $envFile)
: sprintf('<fg=red>⨯</> %s', $envFile);
? sprintf('<fg=green>✓</> %s', $file)
: sprintf('<fg=red>⨯</> %s', $file);
}, $envFiles));

$io->section('Variables');
$io->table(
array_merge(['Variable', 'Value'], $availableFiles),
$this->getVariables($availableFiles)
$this->getVariables($dotenvDirectory, $availableFiles)
);

$io->comment('Note real values might be different between web and CLI.');

return 0;
}

private function getVariables(array $envFiles): array
private function getVariables(?string $dotenvDirectory, array $envFiles): array
{
$vars = explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? '');
sort($vars);
Expand All @@ -91,7 +94,7 @@ private function getVariables(array $envFiles): array
$realValue = $_SERVER[$var];
$varDetails = [$var, $realValue];
foreach ($envFiles as $envFile) {
$values = $fileValues[$envFile] ?? $fileValues[$envFile] = $this->loadValues($envFile);
$values = $fileValues[$envFile] ?? $fileValues[$envFile] = $this->loadValues($dotenvDirectory, $envFile);

$varString = $values[$var] ?? '<fg=yellow>n/a</>';
$shortenedVar = $this->getHelper('formatter')->truncate($varString, 30);
Expand All @@ -104,7 +107,7 @@ private function getVariables(array $envFiles): array
return $output;
}

private function getEnvFiles(): array
private function getEnvFiles(?string $dotenvDirectory): array
{
$files = [
'.env.local.php',
Expand All @@ -116,7 +119,7 @@ private function getEnvFiles(): array
$files[] = '.env.local';
}

if (!is_file($this->getFilePath('.env')) && is_file($this->getFilePath('.env.dist'))) {
if (!is_file($this->getFilePath($dotenvDirectory, '.env')) && is_file($this->getFilePath($dotenvDirectory, '.env.dist'))) {
$files[] = '.env.dist';
} else {
$files[] = '.env';
Expand All @@ -125,19 +128,36 @@ private function getEnvFiles(): array
return $files;
}

private function getFilePath(string $file): string
private function getFilePath(?string $dotenvDirectory, string $file): string
{
return $this->projectDirectory.\DIRECTORY_SEPARATOR.$file;
return $this->projectDirectory.(null === $dotenvDirectory ? '' : \DIRECTORY_SEPARATOR.$dotenvDirectory).\DIRECTORY_SEPARATOR.$file;
}

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

if (str_ends_with($filePath, '.php')) {
return include $filePath;
}

return (new Dotenv())->parse(file_get_contents($filePath));
}

private function getDotenvDirectory(): ?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);
$dotenvPath = $composerContent['extra']['runtime']['dotenv_path'] ?? null;

if (null !== $dotenvPath) {
return \dirname($dotenvPath);
}
}

return null;
}
}
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/.env.local.php', $output);
$this->assertStringContainsString('⨯ sub/path/.env.prod.local', $output);
$this->assertStringContainsString('⨯ sub/path/.env.prod', $output);
$this->assertStringContainsString('✓ sub/path/.env.local', $output);
$this->assertStringContainsString('⨯ sub/path/.env'.\PHP_EOL, $output);

// Skipped Files
$this->assertStringNotContainsString('.env.dist', $output);
$this->assertStringNotContainsString('.env.dev', $output);
$this->asse 67ED rtStringNotContainsString('.env.test', $output);

// Variables
$this->assertStringContainsString('Variable Value .env.local.php .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/.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',
];
0