Env.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <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 (isset($this->convert[$result])) {
  79. $result = $this->convert[$result];
  80. }
  81. if (!isset($this->data[$name])) {
  82. $this->data[$name] = $result;
  83. }
  84. return $result;
  85. }
  86. /**
  87. * 设置环境变量值
  88. * @access public
  89. * @param string|array $env 环境变量
  90. * @param mixed $value 值
  91. * @return void
  92. */
  93. public function set($env, $value = null): void
  94. {
  95. if (is_array($env)) {
  96. $env = array_change_key_case($env, CASE_UPPER);
  97. foreach ($env as $key => $val) {
  98. if (is_array($val)) {
  99. foreach ($val as $k => $v) {
  100. $this->data[$key . '_' . strtoupper($k)] = $v;
  101. }
  102. } else {
  103. $this->data[$key] = $val;
  104. }
  105. }
  106. } else {
  107. $name = strtoupper(str_replace('.', '_', $env));
  108. $this->data[$name] = $value;
  109. }
  110. }
  111. /**
  112. * 检测是否存在环境变量
  113. * @access public
  114. * @param string $name 参数名
  115. * @return bool
  116. */
  117. public function has(string $name): bool
  118. {
  119. return !is_null($this->get($name));
  120. }
  121. /**
  122. * 设置环境变量
  123. * @access public
  124. * @param string $name 参数名
  125. * @param mixed $value 值
  126. */
  127. public function __set(string $name, $value): void
  128. {
  129. $this->set($name, $value);
  130. }
  131. /**
  132. * 获取环境变量
  133. * @access public
  134. * @param string $name 参数名
  135. * @return mixed
  136. */
  137. public function __get(string $name)
  138. {
  139. return $this->get($name);
  140. }
  141. /**
  142. * 检测是否存在环境变量
  143. * @access public
  144. * @param string $name 参数名
  145. * @return bool
  146. */
  147. public function __isset(string $name): bool
  148. {
  149. return $this->has($name);
  150. }
  151. // ArrayAccess
  152. public function offsetSet(mixed $name, mixed $value): void
  153. {
  154. $this->set($name, $value);
  155. }
  156. public function offsetExists(mixed $name): bool
  157. {
  158. return $this->__isset($name);
  159. }
  160. public function offsetUnset(mixed $name): void
  161. {
  162. throw new Exception('not support: unset');
  163. }
  164. public function offsetGet(mixed $name): mixed
  165. {
  166. return $this->get($name);
  167. }
  168. }