Skip to content

Add support for dumped .env files #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ This may be the case if you use hashed values of credentials you pass via `.env`
You can specify a class that implements `\Helhum\DotEnvConnector\DotEnvVars` interface,
if you need a different way to expose env vars.

*The default value* is "Helhum\DotEnvConnector\Adapter\SymfonyDotEnv",
*The default value* is `"Helhum\DotEnvConnector\Adapter\SymfonyDotEnv"`,
which uses symfony/dotenv default parsing of the one .env file.

This could be useful though e.g. if you prefer to use another dotenv parsing library to expose the variables defined in .env
or you want to switch to another parsing strategy of the Symfony dotenv parsing. In the latter case use
"Helhum\DotEnvConnector\Adapter\SymfonyLoadEnv" as value for this option.
`"Helhum\DotEnvConnector\Adapter\SymfonyLoadEnv"` as value for this option.

To additional support dumped .env settings, use the adapter `"\Helhum\DotEnvConnector\Adapter\SymfonyBootEnv"`.
This allows to load a PHP file instead, compiled from all .env* files in the environment.
*Important*: The command `dump-env` needed for compiling the PHP file, is not registered by default.
See the section [Configuring Environment Variables in Production](https://symfony.com/doc/current/configuration.html#configuring-environment-variables-in-production)
of the symfony documentation for details.

Have a look at the existing implementations for examples.

## Feedback
Expand Down
19 changes: 19 additions & 0 deletions src/Adapter/SymfonyBootEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Helhum\DotEnvConnector\Adapter;

use Helhum\DotEnvConnector\DotEnvVars;
use Symfony\Component\Dotenv\Dotenv;

class SymfonyBootEnv implements DotEnvVars
{
public function exposeToEnvironment(string $dotEnvFile): void
{
if (is_file($dotEnvFile) || is_file("$dotEnvFile.dist")) {
$dotEnv = new Dotenv();
$dotEnv->usePutenv();
$dotEnv->bootEnv($dotEnvFile);
}
}
}