Env.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use ArrayAccess;
  14. /**
  15. * Env管理类
  16. * @package think
  17. */
  18. class Env implements ArrayAccess
  19. {
  20. /**
  21. * 环境变量数据
  22. * @var array
  23. */
  24. protected $data = [];
  25. /**
  26. * 数据转换映射
  27. * @var array
  28. */
  29. protected $convert = [
  30. 'true' => true,
  31. 'false' => false,
  32. 'off' => false,
  33. 'on' => true,
  34. ];
  35. public function __construct()
  36. {
  37. $this->data = $_ENV;
  38. }
  39. /**
  40. * 读取环境变量定义文件
  41. * @access public
  42. * @param string $file 环境变量定义文件
  43. * @return void
  44. */
  45. public function load(string $file): void
  46. {
  47. $env = parse_ini_file($file, true, INI_SCANNER_RAW) ?: [];
  48. $this->set($env);
  49. }
  50. /**
  51. * 获取环境变量值
  52. * @access public
  53. * @param string $name 环境变量名
  54. * @param mixed $default 默认值
  55. * @return mixed
  56. */
  57. public function get(string $name = null, $default = null)
  58. {
  59. if (is_null($name)) {
  60. return $this->data;
  61. }
  62. $name = strtoupper(str_replace('.', '_', $name));
  63. if (isset($this->data[$name])) {
  64. $result = $this->data[$name];
  65. if (is_string($result) && isset($this->convert[$result])) {
  66. return $this->convert[$result];
  67. }
  68. return $result;
  69. }
  70. return $this->getEnv($name, $default);
  71. }
  72. protected function getEnv(string $name, $default = null)
  73. {
  74. $result = getenv('PHP_' . $name);
  75. if (false === $result) {
  76. return $default;
  77. }
  78. if ('false' === $result) {
  79. $result = false;
  80. } elseif ('true' === $result) {
  81. $result = true;
  82. }
  83. if (!isset($this->data[$name])) {
  84. $this->data[$name] = $result;
  85. }
  86. return $result;
  87. }
  88. /**
  89. * 设置环境变量值
  90. * @access public
  91. * @param string|array $env 环境变量
  92. * @param mixed $value 值
  93. * @return void
  94. */
  95. public function set($env, $value = null): void
  96. {
  97. if (is_array($env)) {
  98. $env = array_change_key_case($env, CASE_UPPER);
  99. foreach ($env as $key => $val) {
  100. if (is_array($val)) {
  101. foreach ($val as $k => $v) {
  102. $this->data[$key . '_' . strtoupper($k)] = $v;
  103. }
  104. } else {
  105. $this->data[$key] = $val;
  106. }
  107. }
  108. } else {
  109. $name = strtoupper(str_replace('.', '_', $env));
  110. $this->data[$name] = $value;
  111. }
  112. }
  113. /**
  114. * 检测是否存在环境变量
  115. * @access public
  116. * @param string $name 参数名
  117. * @return bool
  118. */
  119. public function has(string $name): bool
  120. {
  121. return !is_null($this->get($name));
  122. }
  123. /**
  124. * 设置环境变量
  125. * @access public
  126. * @param string $name 参数名
  127. * @param mixed $value 值
  128. */
  129. public function __set(string $name, $value): void
  130. {
  131. $this->set($name, $value);
  132. }
  133. /**
  134. * 获取环境变量
  135. * @access public
  136. * @param string $name 参数名
  137. * @return mixed
  138. */
  139. public function __get(string $name)
  140. {
  141. return $this->get($name);
  142. }
  143. /**
  144. * 检测是否存在环境变量
  145. * @access public
  146. * @param string $name 参数名
  147. * @return bool
  148. */
  149. public function __isset(string $name): bool
  150. {
  151. return $this->has($name);
  152. }
  153. // ArrayAccess
  154. #[\ReturnTypeWillChange]
  155. public function offsetSet($name, $value): void
  156. {
  157. $this->set($name, $value);
  158. }
  159. #[\ReturnTypeWillChange]
  160. public function offsetExists($name): bool
  161. {
  162. return $this->__isset($name);
  163. }
  164. #[\ReturnTypeWillChange]
  165. public function offsetUnset($name): void
  166. {
  167. throw new Exception('not support: unset');
  168. }
  169. #[\ReturnTypeWillChange]
  170. public function offsetGet($name)
  171. {
  172. return $this->get($name);
  173. }
  174. }