|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Dotenv\Dotenv; |
| 21 | + |
| 22 | +/** |
| 23 | + * A console command to compiles the contents of the .env* files into a PHP-optimized file called .env.local.php. |
| 24 | + * |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +class DumpEnvCommand extends Command |
| 28 | +{ |
| 29 | + protected static $defaultName = 'env:dump'; |
| 30 | + protected static $defaultDescription = 'Compiles .env files to .env.local.php'; |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritdoc} |
| 34 | + */ |
| 35 | + protected function configure() |
| 36 | + { |
| 37 | + $this |
| 38 | + ->setDescription('Compiles .env files to .env.local.php.') |
| 39 | + ->setDefinition([ |
| 40 | + new InputArgument('env', InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'), |
| 41 | + ]) |
| 42 | + ->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files') |
| 43 | + ->setHelp(<<<'EOT' |
| 44 | +The <info>%command.name%</info> command compiles the contents of the .env* files into a PHP-optimized file called .env.local.php. |
| 45 | +
|
| 46 | + <info>%command.full_name%</info> |
| 47 | +EOT |
| 48 | + ) |
| 49 | + ; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 56 | + { |
| 57 | + /** @var Application $application */ |
| 58 | + $application = $this->getApplication(); |
| 59 | + $kernel = $application->getKernel(); |
| 60 | + |
| 61 | + if ($env = $input->getArgument('env')) { |
| 62 | + $_SERVER['APP_ENV'] = $env; |
| 63 | + } |
| 64 | + |
| 65 | + $path = $kernel->getProjectDir().'/.env'; |
| 66 | + |
| 67 | + if (!$env || !$input->getOption('empty')) { |
| 68 | + $vars = $this->loadEnv($path, $env); |
| 69 | + $env = $vars['APP_ENV']; |
| 70 | + } |
| 71 | + |
| 72 | + if ($input->getOption('empty')) { |
| 73 | + $vars = ['APP_ENV' => $env]; |
| 74 | + } |
| 75 | + |
| 76 | + $vars = var_export($vars, true); |
| 77 | + $vars = <<<EOF |
| 78 | +<?php |
| 79 | +
|
| 80 | +// This file was generated by running "php bin/console env:dump $env" |
| 81 | +
|
| 82 | +return $vars; |
| 83 | +
|
| 84 | +EOF; |
| 85 | + file_put_contents($path.'.local.php', $vars, \LOCK_EX); |
| 86 | + |
| 87 | + $output->writeln('Successfully dumped .env files in <info>.env.local.php</>'); |
| 88 | + |
| 89 | + return Command::SUCCESS; |
| 90 | + } |
| 91 | + |
| 92 | + private function loadEnv(string $path, ?string $env): array |
| 93 | + { |
| 94 | + $globalsBackup = [$_SERVER, $_ENV]; |
| 95 | + unset($_SERVER['APP_ENV']); |
| 96 | + $_ENV = ['APP_ENV' => $env]; |
| 97 | + $_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER)); |
| 98 | + putenv('SYMFONY_DOTENV_VARS='.$_SERVER['SYMFONY_DOTENV_VARS']); |
| 99 | + |
| 100 | + try { |
| 101 | + $dotenv = new Dotenv(); |
| 102 | + |
| 103 | + if (!$env && file_exists($p = "$path.local")) { |
| 104 | + $env = $_ENV['APP_ENV'] = $dotenv->parse(file_get_contents($p), $p)['APP_ENV'] ?? null; |
| 105 | + } |
| 106 | + |
| 107 | + if (!$env) { |
| 108 | + throw new \RuntimeException('Please provide the name of the environment either by passing it as command line argument or by defining the "APP_ENV" variable in the ".env.local" file.'); |
| 109 | + } |
| 110 | + |
| 111 | + $dotenv->loadEnv($path); |
| 112 | + $env = $_ENV; |
| 113 | + } finally { |
| 114 | + [$_SERVER, $_ENV] = $globalsBackup; |
| 115 | + } |
| 116 | + |
| 117 | + return $env; |
| 118 | + } |
| 119 | +} |
0 commit comments