Skip to content

Commit

Permalink
Merge branch '5.4' into 6.0
Browse files Browse the repository at this point in the history
* 5.4:
  [Messenger] Fix dealing with unexpected payload in Redis transport
  [Filesystem] Update some PHPDoc of the Path class
  [VarDumper] Fix dumping mysqli_driver instances
  Fix missing ReturnTypeWillChange attributes
  [Cache] Add missing log when saving namespace
  [HttpKernel] Reset services between requests performed by KernelBrowser
  [HttpKernel] Remove unused argument in ArgumentMetadataFactory
  [Stopwatch] Fix test expectation
  [SecurityBundle] fix autoconfiguring Monolog's ProcessorInterface
  KernelTestCase resets internal state on tearDown
  [Security/Http] Fix getting password-upgrader when user-loader is a closure
  [HttpKernel] Fix extracting controller name from closures
  [Intl] fix wrong offset timezone PHP 8.1
  Fix type binding
  Remove duplicated test
  [Dotenv] Fix reading config for symfony/runtime when running dump command
  [Serializer] Remove unnecessary break
  [Runtime] Fix dotenv_overload with commands
  Make document type nodes ignorable
  Initialize Symfony\Component\Security\Core\Exception\AccountStatusException:: property
  • Loading branch information
nicolas-grekas committed Feb 21, 2022
2 parents 45c47b6 + 83a2310 commit 1c2288f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions Command/DotenvDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
*
* @internal
*/
#[Autoconfigure(bind: ['$dotenvPath' => '%kernel.project_dir%/.env', '$defaultEnv' => '%kernel.environment%'])]
#[Autoconfigure(bind: ['$projectDir' => '%kernel.project_dir%', '$defaultEnv' => '%kernel.environment%'])]
#[AsCommand(name: 'dotenv:dump', description: 'Compiles .env files to .env.local.php')]
final class DotenvDumpCommand extends Command
{
private $dotenvPath;
private $defaultEnv;
private string $projectDir;
private string|null $defaultEnv;

public function __construct(string $dotenvPath, string $defaultEnv = null)
public function __construct(string $projectDir, string $defaultEnv = null)
{
$this->dotenvPath = $dotenvPath;
$this->projectDir = $projectDir;
$this->defaultEnv = $defaultEnv;

parent::__construct();
Expand Down Expand Up @@ -64,13 +64,23 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$config = [];
if (is_file($projectDir = $this->projectDir)) {
$config = ['dotenv_path' => basename($projectDir)];
$projectDir = \dirname($projectDir);
}

$composerFile = $projectDir.'/composer.json';
$config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
$dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
$env = $input->getArgument('env') ?? $this->defaultEnv;
$envKey = $config['env_var_name'] ?? 'APP_ENV';

if ($input->getOption('empty')) {
$vars = ['APP_ENV' => $env];
$vars = [$envKey => $env];
} else {
$vars = $this->loadEnv($env);
$env = $vars['APP_ENV'];
$vars = $this->loadEnv($dotenvPath, $env, $config);
$env = $vars[$envKey];
}

$vars = var_export($vars, true);
Expand All @@ -82,26 +92,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $vars;
EOF;
file_put_contents($this->dotenvPath.'.local.php', $vars, \LOCK_EX);
file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX);

$output->writeln(sprintf('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment.', $env));

return 0;
}

private function loadEnv(string $env): array
private function loadEnv(string $dotenvPath, string $env, array $config): array
{
$dotenv = new Dotenv();
$composerFile = \dirname($this->dotenvPath).'/composer.json';
$testEnvs = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime']['test_envs'] ?? ['test'];
$envKey = $config['env_var_name'] ?? 'APP_ENV';
$testEnvs = $config['test_envs'] ?? ['test'];

$globalsBackup = [$_SERVER, $_ENV];
unset($_SERVER['APP_ENV']);
$_ENV = ['APP_ENV' => $env];
unset($_SERVER[$envKey]);
$_ENV = [$envKey => $env];
$_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER));

try {
$dotenv->loadEnv($this->dotenvPath, null, 'dev', $testEnvs);
$dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs);
unset($_ENV['SYMFONY_DOTENV_VARS']);

return $_ENV;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Command/DotenvDumpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testExecuteTestEnvs()
private function createCommand(): CommandTester
{
$application = new Application();
$application->add(new DotenvDumpCommand(__DIR__.'/.env'));
$application->add(new DotenvDumpCommand(__DIR__));

return new CommandTester($application->find('dotenv:dump'));
}
Expand Down

0 comments on commit 1c2288f

Please sign in to comment.