| Total Complexity | 24 |
| Total Lines | 159 |
| Duplicated Lines | 0 % |
| Coverage | 88.89% |
| Changes | 0 | ||
| 1 | <?php |
||
| 20 | class Env implements ArrayAccess |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * 环境变量数据 |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $data = []; |
||
| 27 | |||
| 28 | 3 | public function __construct() |
|
| 29 | { |
||
| 30 | 3 | $this->data = $_ENV; |
|
| 31 | 3 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * 读取环境变量定义文件 |
||
| 35 | * @access public |
||
| 36 | * @param string $file 环境变量定义文件 |
||
|
1 ignored issue
–
show
|
|||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | 1 | public function load(string $file): void |
|
| 43 | 1 | } |
|
| 44 | |||
| 45 | /** |
||
| 46 | * 获取环境变量值 |
||
| 47 | * @access public |
||
| 48 | * @param string $name 环境变量名 |
||
|
1 ignored issue
–
show
|
|||
| 49 | * @param mixed $default 默认值 |
||
|
1 ignored issue
–
show
|
|||
| 50 | * @return mixed |
||
| 51 | */ |
||
| 52 | 3 | public function get(string $name = null, $default = null) |
|
| 65 | } |
||
| 66 | |||
| 67 | 1 | protected function getEnv(string $name, $default = null) |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 设置环境变量值 |
||
| 90 | * @access public |
||
| 91 | * @param string|array $env 环境变量 |
||
|
1 ignored issue
–
show
|
|||
| 92 | * @param mixed $value 值 |
||
|
1 ignored issue
–
show
|
|||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 2 | public function set($env, $value = null): void |
|
| 113 | } |
||
| 114 | 2 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * 检测是否存在环境变量 |
||
| 118 | * @access public |
||
| 119 | * @param string $name 参数名 |
||
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 131 | * @param mixed $value 值 |
||
|
1 ignored issue
–
show
|
|||
| 132 | */ |
||
| 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
|
|||
| 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
|
|||
| 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 |
|
| 164 | 1 | } |
|
| 165 | |||
| 166 | public function offsetExists($name): bool |
||
| 167 | { |
||
| 168 | return $this->__isset($name); |
||
| 169 | } |
||
| 170 | |||
| 171 | 1 | public function offsetUnset($name) |
|
| 172 | { |
||
| 173 | 1 | throw new Exception('not support: unset'); |
|
| 174 | } |
||
| 175 | |||
| 176 | 1 | public function offsetGet($name) |
|
| 179 | } |
||
| 180 | } |
||
| 181 |