-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
alamirault
wants to merge
3
commits into
symfony:5.4
from
alamirault:hotfix/47880-debug-dotenv-must-respect-dotenv-path
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
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
commit a56b24d056ababc8890e44031eb10e6de03448c1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||||||
|
@@ -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); | ||||||||||||||
|
@@ -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', | ||||||||||||||
|
@@ -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'; | ||||||||||||||
|
@@ -125,19 +128,36 @@ private function getEnvFiles(): array | |||||||||||||
return $files; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private function getFilePath(string $file): string | ||||||||||||||
private function getFilePath(?string $dotenvDirectory, string $file): string | ||||||||||||||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
{ | ||||||||||||||
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 | ||||||||||||||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
{ | ||||||||||||||
$projectDir = is_file($projectDir = $this->projectDirectory) ? basename($projectDir) : $projectDir; | ||||||||||||||
|
||||||||||||||
$composerFile = $projectDir.'/composer.json'; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the symfony/src/Symfony/Component/Runtime/SymfonyRuntime.php Lines 36 to 39 in ac88f9a
|
||||||||||||||
if (is_file($composerFile)) { | ||||||||||||||
$composerContent = json_decode(file_get_contents($composerFile), true); | ||||||||||||||
$dotenvPath = $composerContent['extra']['runtime']['dotenv_path'] ?? null; | ||||||||||||||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
if (null !== $dotenvPath) { | ||||||||||||||
return \dirname($dotenvPath); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return null; | ||||||||||||||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
} | ||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.dist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TEST=0000 |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/.env.prod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FOO=BaR | ||
TEST=1234 |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extra": { | ||
"runtime": { | ||
"dotenv_path": "sub/path/.env" | ||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/.env.local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TEST=1111 |
5 changes: 5 additions & 0 deletions
5
src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/sub/path/.env.local.php
6526
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
return [ | ||
'FOO' => 'BaR', | ||
'TEST' => '1234', | ||
]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.