Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

DotenvBootloader::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\DotEnv\Bootloader;
6
7
use Dotenv\Dotenv;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Boot\DirectoriesInterface;
10
use Spiral\Boot\EnvironmentInterface;
11
12
final class DotenvBootloader extends Bootloader
13
{
14
    private bool $init = false;
15
16 2
    public function init(
17
        DirectoriesInterface $dirs,
18
        EnvironmentInterface $env,
19
    ): void {
20 2
        $this->loadEnvVariables($dirs, $env);
21
    }
22
23 2
    public function loadEnvVariables(DirectoriesInterface $dirs, EnvironmentInterface $env): void
24
    {
25 2
        if ($this->init) {
26
            return;
27
        }
28
29 2
        $this->init = true;
30
31 2
        $dotenvPath = $env->get('DOTENV_PATH', $dirs->get('root') . '.env');
32
33 2
        if (!\file_exists($dotenvPath)) {
34 1
            return;
35
        }
36
37 1
        $path = \dirname($dotenvPath);
38 1
        $file = \basename($dotenvPath);
39
40 1
        foreach (Dotenv::createImmutable($path, $file)->load() as $key => $value) {
41 1
            $env->set($key, $value);
42
        }
43
    }
44
}
45