-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathDumpEnvCommand.php
148 lines (116 loc) · 4.96 KB
/
DumpEnvCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex\Command;
use Composer\Command\BaseCommand;
use Composer\Config;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Flex\Options;
class DumpEnvCommand extends BaseCommand
{
private $config;
private $options;
public function __construct(Config $config, Options $options)
{
$this->config = $config;
$this->options = $options;
parent::__construct();
}
protected function configure()
{
$this->setName('symfony:dump-env')
->setAliases(['dump-env'])
->setDescription('Compiles .env files to .env.local.php.')
->setDefinition([
new InputArgument('env', InputArgument::OPTIONAL, 'The application environment to dump .env files for - e.g. "prod".'),
])
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
->addOption('override', null, InputOption::VALUE_NONE, 'Whether existing environment variables set by the system should be overridden')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$runtime = $this->options->get('runtime') ?? [];
$envKey = $runtime['env_var_name'] ?? 'APP_ENV';
if ($env = $input->getArgument('env') ?? $runtime['env'] ?? null) {
$_SERVER[$envKey] = $env;
}
$path = $this->options->get('root-dir').'/'.($runtime['dotenv_path'] ?? '.env');
if (!$env || !$input->getOption('empty')) {
$vars = $this->loadEnv($path, $env, $runtime, $input->getOption('override'));
$env = $vars[$envKey];
}
if ($input->getOption('empty')) {
$vars = [$envKey => $env];
}
$vars = var_export($vars, true);
$vars = <<<EOF
<?php
// This file was generated by running "composer dump-env $env"
return $vars;
EOF;
file_put_contents($path.'.local.php', $vars, \LOCK_EX);
$this->getIO()->writeError('Successfully dumped .env files in <info>.env.local.php</>');
return 0;
}
private function loadEnv(string $path, ?string $env, array $runtime, bool $override): array
{
if (!file_exists($autoloadFile = $this->config->get('vendor-dir').'/autoload.php')) {
throw new \RuntimeException(\sprintf('Please run "composer install" before running this command: "%s" not found.', $autoloadFile));
}
require $autoloadFile;
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
}
$envKey = $runtime['env_var_name'] ?? 'APP_ENV';
$globalsBackup = [$_SERVER, $_ENV];
unset($_SERVER[$envKey]);
$_ENV = [$envKey => $env];
$_SERVER['SYMFONY_DOTENV_VARS'] = implode(',', array_keys($_SERVER));
putenv('SYMFONY_DOTENV_VARS='.$_SERVER['SYMFONY_DOTENV_VARS']);
try {
if (method_exists(Dotenv::class, 'usePutenv')) {
$dotenv = new Dotenv();
} else {
$dotenv = new Dotenv(false);
}
if (!$env && file_exists($p = "$path.local")) {
$env = $_ENV[$envKey] = $dotenv->parse(file_get_contents($p), $p)[$envKey] ?? null;
}
if (!$env) {
throw new \RuntimeException(\sprintf('Please provide the name of the environment either by passing it as command line argument or by defining the "%s" variable in the ".env.local" file.', $envKey));
}
$testEnvs = $runtime['test_envs'] ?? ['test'];
if (method_exists($dotenv, 'loadEnv')) {
$dotenv->loadEnv($path, $envKey, 'dev', $testEnvs, $override);
} else {
// fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added)
$dotenv->load(file_exists($path) || !file_exists($p = "$path.dist") ? $path : $p);
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
$dotenv->load($p);
}
if (file_exists($p = "$path.$env")) {
$dotenv->load($p);
}
if (file_exists($p = "$path.$env.local")) {
$dotenv->load($p);
}
}
unset($_ENV['SYMFONY_DOTENV_VARS']);
$env = $_ENV;
} finally {
list($_SERVER, $_ENV) = $globalsBackup;
}
return $env;
}
}