Skip to content

Commit 132b306

Browse files
committed
Rebasing upstream
1 parent 62f0708 commit 132b306

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CHANGELOG
33

44
5.4
55
---
6-
* Add `dotenv:dump` command to compile the contents of the .env* files into a PHP-optimized file called `.env.local.php`. Copied from symfony/flex.
6+
* Add `dotenv:dump` command to compile the contents of the .env files into a PHP-optimized file called `.env.local.php`. Copied from symfony/flex.
77
* Add autowiring alias for `HttpCache\StoreInterface`
88
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
99
* Deprecate the public `profiler` service to private

src/Symfony/Bundle/FrameworkBundle/Command/DotenvDumpCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\Dotenv\Dotenv;
2121

2222
/**
23-
* A console command to compile the contents of the .env* files into a PHP-optimized file called .env.local.php.
23+
* A console command to compile the contents of the .env files into a PHP-optimized file called .env.local.php.
2424
*
2525
* @internal
2626
*/
@@ -41,7 +41,7 @@ protected function configure()
4141
])
4242
->addOption('empty', null, InputOption::VALUE_NONE, 'Ignore the content of .env files')
4343
->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.
44+
The <info>%command.name%</info> command compiles the contents of the .env files into a PHP-optimized file called .env.local.php.
4545
4646
<info>%command.full_name%</info>
4747
EOT
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)