|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Flex\Command; |
| 13 | + |
| 14 | +use Composer\Command\BaseCommand; |
| 15 | +use Composer\Config; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Dotenv\Dotenv; |
| 20 | +use Symfony\Flex\Options; |
| 21 | + |
| 22 | +class DumpEnvCommand extends BaseCommand |
| 23 | +{ |
| 24 | + private $config; |
| 25 | + private $options; |
| 26 | + |
| 27 | + public function __construct(Config $config, Options $options) |
| 28 | + { |
| 29 | + $this->config = $config; |
| 30 | + $this->options = $options; |
| 31 | + |
| 32 | + parent::__construct(); |
| 33 | + } |
| 34 | + |
| 35 | + protected function configure() |
| 36 | + { |
| 37 | + $this->setName('symfony:dump-env') |
| 38 | + ->setAliases(['dump-env']) |
| 39 | + ->setDescription('Compiles .env files to .env.local.php.') |
| 40 | + ->setDefinition([ |
| 41 | + new InputArgument('env', InputArgument::REQUIRED, 'The application environment to dump .env files for - e.g. "prod".'), |
| 42 | + ]) |
| 43 | + ; |
| 44 | + } |
| 45 | + |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 47 | + { |
| 48 | + require $this->config->get('vendor-dir').'/autoload.php'; |
| 49 | + |
| 50 | + if (!class_exists(Dotenv::class)) { |
| 51 | + throw new \RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); |
| 52 | + } |
| 53 | + |
| 54 | + $_SERVER['APP_ENV'] = $env = $input->getArgument('env'); |
| 55 | + $path = $this->options->get('root-dir').'/.env'; |
| 56 | + $dotenv = new Dotenv(); |
| 57 | + |
| 58 | + if (method_exists($dotenv, 'loadEnv')) { |
| 59 | + $dotenv->loadEnv($path); |
| 60 | + } else { |
| 61 | + // fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added) |
| 62 | + $dotenv->load(file_exists($path) || !file_exists($p = "$path.dist") ? $path : $p); |
| 63 | + |
| 64 | + if ('test' !== $env && file_exists($p = "$path.local")) { |
| 65 | + $dotenv->load($p); |
| 66 | + } |
| 67 | + |
| 68 | + if (file_exists($p = "$path.$env")) { |
| 69 | + $dotenv->load($p); |
| 70 | + } |
| 71 | + |
| 72 | + if (file_exists($p = "$path.$env.local")) { |
| 73 | + $dotenv->load($p); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $vars = array_flip(explode(',', ($_SERVER['SYMFONY_DOTENV_VARS'] ?? '').',APP_ENV')); |
| 78 | + $vars = array_intersect_key($_SERVER, $vars); |
| 79 | + $vars = var_export($vars, true); |
| 80 | + $vars = <<<EOF |
| 81 | +<?php |
| 82 | +
|
| 83 | +// This file was generated by running "composer dump-env $env" |
| 84 | +
|
| 85 | +return $vars; |
| 86 | +
|
| 87 | +EOF; |
| 88 | + file_put_contents($path.'.local.php', $vars, LOCK_EX); |
| 89 | + |
| 90 | + $this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>'); |
| 91 | + } |
| 92 | +} |
0 commit comments