Completed
Push — 6.0 ( 314b92...dbdaa9 )
by liu
05:32
created

Env::set()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
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
/**
18
 * Env管理类
19
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
20
 */
21
class Env implements ArrayAccess
22
{
23
    /**
24
     * 环境变量数据
25
     * @var array
26
     */
27
    protected $data = [];
28
29 12
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
30
    {
31 12
        $this->data = $_ENV;
32 12
    }
33
34
    /**
35
     * 读取环境变量定义文件
36
     * @access public
37
     * @param string $file 环境变量定义文件
38
     * @return void
39
     */
40 1
    public function load(string $file): void
41
    {
42 1
        $env = parse_ini_file($file, true) ?: [];
43 1
        $this->set($env);
44 1
    }
45
46
    /**
47
     * 获取环境变量值
48
     * @access public
49
     * @param string $name    环境变量名
50
     * @param mixed  $default 默认值
51
     * @return mixed
52
     */
53 4
    public function get(string $name = null, $default = null)
54
    {
55 4
        if (is_null($name)) {
56 1
            return $this->data;
57
        }
58
59 4
        $name = strtoupper(str_replace('.', '_', $name));
60
61 4
        if (isset($this->data[$name])) {
62 3
            return $this->data[$name];
63
        }
64
65 1
        return $this->getEnv($name, $default);
66
    }
67
68 1
    protected function getEnv(string $name, $default = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getEnv()
Loading history...
69
    {
70 1
        $result = getenv('PHP_' . $name);
71
72 1
        if (false === $result) {
73 1
            return $default;
74
        }
75
76 1
        if ('false' === $result) {
77 1
            $result = false;
78 1
        } elseif ('true' === $result) {
79 1
            $result = true;
80
        }
81
82 1
        if (!isset($this->data[$name])) {
83 1
            $this->data[$name] = $result;
84
        }
85
86 1
        return $result;
87
    }
88
89
    /**
90
     * 设置环境变量值
91
     * @access public
92
     * @param string|array $env   环境变量
93
     * @param mixed        $value 值
94
     * @return void
95
     */
96 3
    public function set($env, $value = null): void
97
    {
98 3
        if (is_array($env)) {
99 3
            $env = array_change_key_case($env, CASE_UPPER);
100
101 3
            foreach ($env as $key => $val) {
102 3
                if (is_array($val)) {
103 1
                    foreach ($val as $k => $v) {
104 1
                        $this->data[$key . '_' . strtoupper($k)] = $v;
105
                    }
106
                } else {
107 3
                    $this->data[$key] = $val;
108
                }
109
            }
110
        } else {
111 1
            $name = strtoupper(str_replace('.', '_', $env));
112
113 1
            $this->data[$name] = $value;
114
        }
115 3
    }
116
117
    /**
118
     * 检测是否存在环境变量
119
     * @access public
120
     * @param string $name 参数名
121
     * @return bool
122
     */
123 1
    public function has(string $name): bool
124
    {
125 1
        return !is_null($this->get($name));
126
    }
127
128
    /**
129
     * 设置环境变量
130
     * @access public
131
     * @param string $name  参数名
132
     * @param mixed  $value 值
133
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
134 1
    public function __set(string $name, $value): void
135
    {
136 1
        $this->set($name, $value);
137 1
    }
138
139
    /**
140
     * 获取环境变量
141
     * @access public
142
     * @param string $name 参数名
143
     * @return mixed
144
     */
145 1
    public function __get(string $name)
146
    {
147 1
        return $this->get($name);
148
    }
149
150
    /**
151
     * 检测是否存在环境变量
152
     * @access public
153
     * @param string $name 参数名
154
     * @return bool
155
     */
156 1
    public function __isset(string $name): bool
157
    {
158 1
        return $this->has($name);
159
    }
160
161
    // ArrayAccess
162 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...
163
    {
164 1
        $this->set($name, $value);
165 1
    }
166
167 1
    public function offsetExists($name): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetExists()
Loading history...
168
    {
169 1
        return $this->__isset($name);
170
    }
171
172 1
    public function offsetUnset($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetUnset()
Loading history...
173
    {
174 1
        throw new Exception('not support: unset');
175
    }
176
177 1
    public function offsetGet($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function offsetGet()
Loading history...
178
    {
179 1
        return $this->get($name);
180
    }
181
}
182