Vars   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A populate() 0 6 2
A env() 0 3 1
A unset() 0 3 2
A export() 0 3 1
A load() 0 3 1
1
<?php
2
/**
3
 * Vars manager
4
 * User: moyo
5
 * Date: 2018/8/22
6
 * Time: 4:08 PM
7
 */
8
9
namespace Carno\Env;
10
11
use Symfony\Component\Dotenv\Dotenv;
12
use Throwable;
13
14
class Vars
15
{
16
    /**
17
     * @var Dotenv
18
     */
19
    private static $ins = null;
20
21
    /**
22
     * @return Dotenv
23
     */
24
    private static function env() : Dotenv
25
    {
26
        return self::$ins ?? self::$ins = new Dotenv;
27
    }
28
29
    /**
30
     * @param string ...$files
31
     */
32
    public static function load(string ...$files) : void
33
    {
34
        self::env()->load(...$files);
35
    }
36
37
    /**
38
     * @param string $text
39
     */
40
    public static function populate(string $text) : void
41
    {
42
        try {
43
            self::env()->populate(self::env()->parse($text));
44
        } catch (Throwable $e) {
45
            trigger_error('env populate failed -> '.$e->getMessage(), E_USER_WARNING);
46
        }
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param string $val
52
     */
53
    public static function export(string $key, string $val) : void
54
    {
55
        self::populate(sprintf('%s=%s', trim($key), $val));
56
    }
57
58
    /**
59
     * @param string $key
60
     */
61
    public static function unset(string $key) : void
62
    {
63
        strstr($key, '=') || putenv(trim($key));
64
    }
65
}
66