Skip to content

Commit

Permalink
🚀 v5
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Meilick <[email protected]>
  • Loading branch information
bnomei committed Nov 27, 2024
1 parent bc065aa commit fe5d63d
Showing 1 changed file with 38 additions and 40 deletions.
78 changes: 38 additions & 40 deletions classes/DotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,51 @@ final class DotEnv

public function __construct(array $options = [], bool $canUseKirbyOptions = true)
{
$defaults = $canUseKirbyOptions ? [
'debug' => option('debug'),
'dir' => option('bnomei.dotenv.dir'),
'file' => option('bnomei.dotenv.file'),
'environment' => option('bnomei.dotenv.environment'),
'required' => option('bnomei.dotenv.required'),
'setup' => option('bnomei.dotenv.setup'),
] : [
'debug' => false,
'dir' => [],
'file' => '.env',
'environment' => null,
'required' => [],
'setup' => function ($dotenv) {
$defaults = [
'debug' => $canUseKirbyOptions ? option('debug') : false,
'dir' => $canUseKirbyOptions ? option('bnomei.dotenv.dir') : [],
'file' => $canUseKirbyOptions ? option('bnomei.dotenv.file') : '.env',
'environment' => $canUseKirbyOptions ? option('bnomei.dotenv.environment') : null,
'required' => $canUseKirbyOptions ? option('bnomei.dotenv.required') : [],
'setup' => $canUseKirbyOptions ? option('bnomei.dotenv.setup') : function ($dotenv) {
return $dotenv;
},
];
$this->options = array_merge($defaults, $options);

// allow callback for a few options
foreach ($this->options as $key => $value) {
if ($value instanceof Closure && in_array($key, ['dir', 'file', 'environment', 'required'])) {
$this->options[$key] = $value();
}
}
$this->allowCallbackForAFewOptions();
$this->tryMultipleReasonableDirs();
$this->addRequired($this->options['required']);
// allow additional last step setup
$this->dotenv = $this->options['setup']($this->dotenv);
}

public function option(string $key): mixed
{
return A::get($this->options, $key);
}

private function tryMultipleReasonableDirs(): void
{
// try multiple reasonable dirs
$dirs = $this->options['dir'];
if (! is_array($dirs)) {
$dirs = [$dirs];
}
$dirs = array_filter($dirs, function ($dir) {
return ! empty($dir);
});
$files = [
$this->options['file'].'.'.$this->options['environment'],
$this->options['file'],
];
foreach ($dirs as $dir) {
if (empty($dir)) {
continue;
}
// more specific file first as it will break on first success
$files = [
$this->options['file'].'.'.$this->options['environment'],
$this->options['file'],
];
foreach ($files as $file) {
if ($this->loadFromDir($dir, $file)) {
break;
break 2;
}
}
}

// add rules for required env vars
$this->addRequired($this->options['required']);

// allow additional last step setup
$this->dotenv = $this->options['setup']($this->dotenv);
}

public function option(string $key): mixed
{
return A::get($this->options, $key);
}

private function loadFromDir(string $dir, string $file): bool
Expand Down Expand Up @@ -132,4 +120,14 @@ public static function getenv(string $env, mixed $default = null): mixed

return A::get($_ENV, $env, $default);
}

public function allowCallbackForAFewOptions(): void
{
// allow callback for a few options
foreach ($this->options as $key => $value) {
if ($value instanceof Closure && in_array($key, ['dir', 'file', 'environment', 'required'])) {
$this->options[$key] = $value();
}
}
}
}

0 comments on commit fe5d63d

Please sign in to comment.