Completed
Push — 6.0 ( 06cc73...122edf )
by liu
02:41
created

Env::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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