| Total Complexity | 23 |
| Total Lines | 148 |
| Duplicated Lines | 0 % |
| Coverage | 92.31% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | class Env implements ArrayAccess |
||
|
1 ignored issue
–
show
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * 环境变量数据 |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $data = []; |
||
| 24 | |||
| 25 | 3 | public function __construct() |
|
| 26 | { |
||
| 27 | 3 | $this->data = $_ENV; |
|
| 28 | 3 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * 读取环境变量定义文件 |
||
| 32 | * @access public |
||
| 33 | * @param string $file 环境变量定义文件 |
||
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 46 | * @param mixed $default 默认值 |
||
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 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
|
|||
| 89 | * @param mixed $value 值 |
||
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 117 | * @param mixed $value 值 |
||
|
1 ignored issue
–
show
|
|||
| 118 | */ |
||
| 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
|
|||
| 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
|
|||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function __isset(string $name): bool |
||
| 144 | } |
||
| 145 | |||
| 146 | // ArrayAccess |
||
| 147 | 1 | public function offsetSet($name, $value): void |
|
| 148 | { |
||
| 149 | 1 | $this->set($name, $value); |
|
| 150 | 1 | } |
|
| 151 | |||
| 152 | public function offsetExists($name): bool |
||
| 155 | } |
||
| 156 | |||
| 157 | 1 | public function offsetUnset($name) |
|
| 158 | { |
||
| 159 | 1 | throw new Exception('not support: unset'); |
|
| 160 | } |
||
| 161 | |||
| 162 | 1 | public function offsetGet($name) |
|
| 165 | } |
||
| 166 | } |
||
| 167 |