Skip to content
Closed
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
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ need to be enclosed in single quotes.
This may be the case if you use hashed values of credentials you pass via `.env`, for example.

#### `adapter`
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",
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.
You can specify a class that implements the `\Helhum\DotEnvConnector\DotEnvVars` interface if you need a different way
to expose environment variables. This package ships with three adapters for the `symfony/dotenv` component.

* `Helhum\DotEnvConnector\Adapter\SymfonyDotEnv` (Default): Uses `$dotenv->load()`.
* This loads variables from your `.env` file but will not overwrite any existing environment variables.
* The loading is skipped if an `APP_ENV` environment variable is already set.
* `Helhum\DotEnvConnector\Adapter\SymfonyOverload`: Uses `$dotenv->overload()`.
* This is similar to the default adapter, but it will overwrite any existing environment variables with the values from your `.env` file.
* The loading is also skipped if an [APP_ENV]() environment variable is already set.
* `Helhum\DotEnvConnector\Adapter\SymfonyLoadEnv`: Uses `$dotenv->loadEnv()`.
* This adapter uses Symfony's more powerful loading mechanism, which can load multiple files (`.env`, `.env.local`, `.env.$APP_ENV.local`, etc.).
* It does not check for the existence of `APP_ENV` before running.

You can also provide your own implementation if you want to use a different dotenv library.
Have a look at the existing implementations for examples.

## Feedback
Expand Down
19 changes: 19 additions & 0 deletions src/Adapter/SymfonyOverload.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 SymfonyOverload implements DotEnvVars
{
public function exposeToEnvironment(string $dotEnvFile): void
{
if (!getenv('APP_ENV') && file_exists($dotEnvFile)) {
$dotEnv = new Dotenv();
$dotEnv->usePutenv();
$dotEnv->overload($dotEnvFile);
}
}
}