Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Command/DumpEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$path = $this->options->get('root-dir').'/'.($runtime['dotenv_path'] ?? '.env');
$GLOBALS['SYMFONY_DOTENV_VARS'] = [];

if (!$env || !$input->getOption('empty')) {
$vars = $this->loadEnv($path, $env, $runtime);
Expand All @@ -66,6 +67,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$vars = var_export($vars, true);

foreach ($GLOBALS['SYMFONY_DOTENV_VARS'] as $k => $v) {
$k = var_export($k, true);
$vars = str_replace($v, "'.(\$_ENV[{$k}] ?? ".(str_starts_with($k, "'HTTP_") ? '' : "\$_SERVER[{$k}] ?? ")."'').'", $vars);
}
unset($GLOBALS['SYMFONY_DOTENV_VARS']);
$vars = strtr($vars, [
"''.(" => '(',
").''.(" => ').(',
").''" => ')',
]);

$vars = <<<EOF
<?php

Expand Down Expand Up @@ -145,3 +158,14 @@ private function loadEnv(string $path, ?string $env, array $runtime): array
return $env;
}
}

namespace Symfony\Component\Dotenv;

function getenv(?string $name = null, bool $local_only = false): string|array|false
{
if (null === $name) {
return \getenv($name, $local_only);
}

return $GLOBALS['SYMFONY_DOTENV_VARS'][$name] ??= md5(random_bytes(10));
}
57 changes: 45 additions & 12 deletions tests/Command/DumpEnvCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function testFileIsCreated()
@unlink($envLocal);

$envContent = <<<EOF
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
file_put_contents($env, $envContent);

$command = $this->createCommandDumpEnv();
Expand Down Expand Up @@ -60,9 +60,9 @@ public function testEmptyOptionMustIgnoreContent()
@unlink($envLocal);

$envContent = <<<EOF
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
APP_ENV=dev
APP_SECRET=abcdefgh123456789
EOF;
file_put_contents($env, $envContent);

$command = $this->createCommandDumpEnv();
Expand Down Expand Up @@ -94,9 +94,9 @@ public function testEnvCanBeReferenced()
@unlink($envLocal);

$envContent = <<<'EOF'
BAR=$FOO
FOO=123
EOF;
BAR=$FOO
FOO=123
EOF;
file_put_contents($env, $envContent);

$_SERVER['FOO'] = 'Foo';
Expand Down Expand Up @@ -179,9 +179,9 @@ public function testLoadLocalEnvWhenTestEnvIsNotEqual()

file_put_contents($env, 'APP_ENV=dev');
file_put_contents($envLocal, <<<EOF
APP_ENV=test
APP_SECRET=abcdefgh123456789
EOF
APP_ENV=test
APP_SECRET=abcdefgh123456789
EOF
);

$command = $this->createCommandDumpEnv(['runtime' => ['test_envs' => []]]);
Expand All @@ -202,6 +202,39 @@ public function testLoadLocalEnvWhenTestEnvIsNotEqual()
unlink($envLocalPhp);
}

public function testEnvVarReferenceInDumpedFile()
{
@mkdir(FLEX_TEST_DIR);
$env = FLEX_TEST_DIR.'/.env';
$envLocal = FLEX_TEST_DIR.'/.env.local.php';

@unlink($env);
@unlink($envLocal);

$envContent = <<<'EOF'
APP_ENV=prod
APP_SHARE_DIR=$APP_PROJECT_DIR/var/share
EOF;
file_put_contents($env, $envContent);

$command = $this->createCommandDumpEnv();
$command->execute(['env' => 'prod']);

$dumpedContent = file_get_contents($envLocal);
$this->assertStringContainsString("\$_ENV['APP_PROJECT_DIR']", $dumpedContent);
$this->assertStringContainsString("\$_SERVER['APP_PROJECT_DIR']", $dumpedContent);

$_ENV['APP_PROJECT_DIR'] = '/path/to/project';
$vars = require $envLocal;
$this->assertSame([
'APP_ENV' => 'prod',
'APP_SHARE_DIR' => '/path/to/project/var/share',
], $vars);

unlink($env);
unlink($envLocal);
}

private function createCommandDumpEnv(array $options = [])
{
$command = new DumpEnvCommand(
Expand Down
Loading