Skip to content

Commit

Permalink
Allow setting Symfony's dotenv path
Browse files Browse the repository at this point in the history
This commit introduces the environment variable `ENV_DIR`.

When provided, davis will load env files (`.env`, `.env.local`, etc.)
from this folder instead of loading them from the default
location (which is the folder containing `composer.json`)

As discussed in #154.
  • Loading branch information
Ramblurr committed Apr 1, 2024
1 parent f67d842 commit 6e95b0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ APP_TIMEZONE="Australia/Lord_Howe"
> ```
> in your environment file if you wish to use the **actual default timezone of the server**, and not enforcing it.
i. Override the dotenv path
You can override the expected location of the env files (`.env`, `.env.local`, etc) by setting the `ENV_DIR` directory. The value should be to a folder containing the env files. This value must be specified in the actual environment and *not* in an `.env` file as it is read and evaluated before the env files are read.
```
ENV_DIR=/var/lib/davis
```
### Specific environment variables for IMAP and LDAP authentication methods
In case you use the `IMAP` auth type, you must specify the auth url (_the "mailbox" url_) in `IMAP_AUTH_URL`. See https://www.php.net/manual/en/function.imap-open.php for more details.
Expand Down
7 changes: 6 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env php
<?php

if (getenv('ENV_DIR') !== false && getenv('ENV_DIR') !== '' ) {
$_SERVER['APP_RUNTIME_OPTIONS']['dotenv_path'] = getenv('ENV_DIR').'/.env';
}

use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down Expand Up @@ -28,7 +32,8 @@ if ($input->hasParameterOption('--no-debug', true)) {
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
}

(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
$env_dir = getenv('ENV_DIR') != false ? getenv('ENV_DIR') : dirname(__DIR__);
(new Dotenv())->bootEnv($env_dir.'/.env');

if ($_SERVER['APP_DEBUG']) {
umask(0000);
Expand Down
9 changes: 8 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php

if (getenv('ENV_DIR') !== false && getenv('ENV_DIR') !== '' ) {
$_SERVER['APP_RUNTIME_OPTIONS']['dotenv_path'] = getenv('ENV_DIR').'/.env';
}

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/vendor/autoload.php';

(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');

$env_dir = getenv('ENV_DIR') != false ? getenv('ENV_DIR') : dirname(__DIR__);
(new Dotenv())->bootEnv($env_dir.'/.env');


if ($_SERVER['APP_DEBUG']) {
umask(0000);
Expand Down

0 comments on commit 6e95b0d

Please sign in to comment.