Passed
Push — 6.0 ( 0e07aa...575f2b )
by yun
02:55
created

Env   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 148
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __isset() 0 3 1
A offsetExists() 0 3 1
A offsetSet() 0 3 1
A load() 0 4 2
A __get() 0 3 1
A __construct() 0 3 1
A offsetGet() 0 3 1
A get() 0 13 3
A getEnv() 0 19 5
A set() 0 18 5
A offsetUnset() 0 3 1
A __set() 0 3 1
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use ArrayAccess;
16
17
class Env implements ArrayAccess
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Env
Loading history...
18
{
19
    /**
20
     * 环境变量数据
21
     * @var array
22
     */
23
    protected $data = [];
24
25 3
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
26
    {
27 3
        $this->data = $_ENV;
28 3
    }
29
30
    /**
31
     * 读取环境变量定义文件
32
     * @access public
33
     * @param string $file 环境变量定义文件
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
34
     * @return void
35
     */
36 1
    public function load(string $file): void
37
    {
38 1
        $env = parse_ini_file($file, true) ?: [];
39 1
        $this->set($env);
40 1
    }
41
42
    /**
43
     * 获取环境变量值
44
     * @access public
45
     * @param string $name    环境变量名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
46
     * @param mixed  $default 默认值
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
47
     * @return mixed
48
     */
49 3
    public function get(string $name = null, $default = null)
50
    {
51 3
        if (is_null($name)) {
52 1
            return $this->data;
53
        }
54
55 3
        $name = strtoupper(str_replace('.', '_', $name));
56
57 3
        if (isset($this->data[$name])) {
58 2
            return $this->data[$name];
59
        }
60
61 1
        return $this->getEnv($name, $default);
62
    }
63
64 1
    protected function getEnv(string $name, $default = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getEnv()
Loading history...
65
    {
66 1
        $result = getenv('PHP_' . $name);
67
68 1
        if (false === $result) {
69 1
            return $default;
70
        }
71
72 1
        if ('false' === $result) {
73 1
            $result = false;
74 1
        } elseif ('true' === $result) {
75 1
            $result = true;
76
        }
77
78 1
        if (!isset($this->data[$name])) {
79 1
            $this->data[$name] = $result;
80
        }
81
82 1
        return $result;
83
    }
84
85
    /**
86
     * 设置环境变量值
87
     * @access public
88
     * @param string|array $env   环境变量
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
89
     * @param mixed        $value 值
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
90
     * @return void
91
     */
92 2
    public function set($env, $value = null): void
93
    {
94 2
        if (is_array($env)) {
95 2
            $env = array_change_key_case($env, CASE_UPPER);
96
97 2
            foreach ($env as $key => $val) {
98 2
                if (is_array($val)) {
99 1
                    foreach ($val as $k => $v) {
100 1
                        $this->data[$key . '_' . strtoupper($k)] = $v;
101
                    }
102
                } else {
103 2
                    $this->data[$key] = $val;
104
                }
105
            }
106
        } else {
107 1
            $name = strtoupper(str_replace('.', '_', $env));
108
109 1
            $this->data[$name] = $value;
110
        }
111 2
    }
112
113
    /**
114
     * 设置环境变量
115
     * @access public
116
     * @param string $name  参数名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
117
     * @param mixed  $value 值
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119 1
    public function __set(string $name, $value): void
120
    {
121 1
        $this->set($name, $value);
122 1
    }
123
124
    /**
125
     * 获取环境变量
126
     * @access public
127
     * @param string $name 参数名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
128
     * @return mixed
129
     */
130 1
    public function __get(string $name)
131
    {
132 1
        return $this->get($name);
133
    }
134
135
    /**
136
     * 检测是否存在环境变量
137
     * @access public
138
     * @param string $name 参数名
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
139
     * @return bool
140
     */
141
    public function __isset(string $name): bool
142
    {
143
        return !is_null($this->get($name));
144
    }
145
146
    // ArrayAccess
147 1
    public function offsetSet($name, $value): void
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
148
    {
149 1
        $this->set($name, $value);
150 1
    }
151
152
    public function offsetExists($name): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetExists()
Loading history...
153
    {
154
        return $this->__isset($name);
155
    }
156
157 1
    public function offsetUnset($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetUnset()
Loading history...
158
    {
159 1
        throw new Exception('not support: unset');
160
    }
161
162 1
    public function offsetGet($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetGet()
Loading history...
163
    {
164 1
        return $this->get($name);
165
    }
166
}
167