/src/Symfony/Component/Dotenv/Command/DotenvDumpCommand.php

https://github.com/stof/symfony · PHP · 122 lines · 79 code · 21 blank · 22 comment · 3 complexity · fdf843326b55385465a41227d0e4bcdb MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Dotenv\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
  18. use Symfony\Component\Dotenv\Dotenv;
  19. /**
  20. * A console command to compile .env files into a PHP-optimized file called .env.local.php.
  21. *
  22. * @internal
  23. */
  24. #[Autoconfigure(bind: ['$projectDir' => '%kernel.project_dir%', '$defaultEnv' => '%kernel.environment%'])]
  25. #[AsCommand(name: 'dotenv:dump', description: 'Compiles .env files to .env.local.php')]
  26. final class DotenvDumpCommand extends Command
  27. {
  28. private string $projectDir;
  29. private string|null $defaultEnv;
  30. public function __construct(string $projectDir, string $defaultEnv = null)
  31. {
  32. $this->projectDir = $projectDir;
  33. $this->defaultEnv = $defaultEnv;
  34. parent::__construct();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function configure()
  40. {
  41. $this
  42. ->setDefinition([
  43. new InputArgument('env', null === $this->defaultEnv ? InputArgument::REQUIRED : InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'),
  44. ])
  45. ->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
  46. ->setHelp(<<<'EOT'
  47. The <info>%command.name%</info> command compiles .env files into a PHP-optimized file called .env.local.php.
  48. <info>%command.full_name%</info>
  49. EOT
  50. )
  51. ;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function execute(InputInterface $input, OutputInterface $output): int
  57. {
  58. $config = [];
  59. if (is_file($projectDir = $this->projectDir)) {
  60. $config = ['dotenv_path' => basename($projectDir)];
  61. $projectDir = \dirname($projectDir);
  62. }
  63. $composerFile = $projectDir.'/composer.json';
  64. $config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
  65. $dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
  66. $env = $input->getArgument('env') ?? $this->defaultEnv;
  67. $envKey = $config['env_var_name'] ?? 'APP_ENV';
  68. if ($input->getOption('empty')) {
  69. $vars = [$envKey => $env];
  70. } else {
  71. $vars = $this->loadEnv($dotenvPath, $env, $config);
  72. $env = $vars[$envKey];
  73. }
  74. $vars = var_export($vars, true);
  75. $vars = <<<EOF
  76. <?php
  77. // This file was generated by running "php bin/console dotenv:dump $env"
  78. return $vars;
  79. EOF;
  80. file_put_contents($dotenvPath.'.local.php', $vars, \LOCK_EX);
  81. $output->writeln(sprintf('Successfully dumped .env files in <info>.env.local.php</> for the <info>%s</> environment.', $env));
  82. return 0;
  83. }
  84. private function loadEnv(string $dotenvPath, string $env, array $config): array
  85. {
  86. $dotenv = new Dotenv();
  87. $envKey = $config['env_var_name'] ?? 'APP_ENV';
  88. $testEnvs = $config['test_envs'] ?? ['test'];
  89. $globalsBackup = [$_SERVER, $_ENV];
  90. unset($_SERVER[$envKey]);
  91. $_ENV = [$envKey => $env];
  92. $_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER));
  93. try {
  94. $dotenv->loadEnv($dotenvPath, null, 'dev', $testEnvs);
  95. unset($_ENV['SYMFONY_DOTENV_VARS']);
  96. return $_ENV;
  97. } finally {
  98. [$_SERVER, $_ENV] = $globalsBackup;
  99. }
  100. }
  101. }