Passed
Pull Request — 8.0 (#2946)
by
unknown
02:28
created

Env::get()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

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