DotEnv::toEnv()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core;
3
4
class DotEnv
5
{
6
7
    /**
8
     * @var array
9
     */
10
    private $env;
11
12
    /**
13
     * @param string $env
14
     * @return $this
15
     * @throws \RuntimeException
16
     */
17 2
    public function load($env = '.env')
18
    {
19
20 2
        if (!file_exists($env)) {
21
            throw new \RuntimeException("Invalid ENV file or file is not readable");
22
        }
23
24 2
        $content = file_get_contents($env);
25 2
        $values = explode("\n", str_replace(array("\r\n", "\n\r", "\r"), "\n", $content));
26
27 2
        foreach ($values AS $value) {
28 2
            $this->parse($value);
29
        }
30
31 2
        return $this;
32
    }
33
34 2
    private function parse($values)
35
    {
36 2
        if (empty($values)) {
37 2
            return;
38
        }
39
        
40 2
        list($key, $value) = explode('=', $values);
41
42 2
        $this->env[$key] = $value;
43 2
    }
44
45
    /**
46
     * @return $this
47
     */
48 2
    public function toEnv()
49
    {
50 2
        foreach ($this->env AS $key => $value) {
51 2
            $_ENV[$key] = $value;
52 2
            putenv("$key=$value");
53
        }
54
55 2
        return $this;
56
    }
57
}
58