Env.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. public function __construct()
  26. {
  27. $this->data = $_ENV;
  28. }
  29. /**
  30. * 读取环境变量定义文件
  31. * @access public
  32. * @param string $file 环境变量定义文件
  33. * @return void
  34. */
  35. public function load(string $file): void
  36. {
  37. $env = parse_ini_file($file, true) ?: [];
  38. $this->set($env);
  39. }
  40. /**
  41. * 获取环境变量值
  42. * @access public
  43. * @param string $name 环境变量名
  44. * @param mixed $default 默认值
  45. * @return mixed
  46. */
  47. public function get(string $name = null, $default = null)
  48. {
  49. if (is_null($name)) {
  50. return $this->data;
  51. }
  52. $name = strtoupper(str_replace('.', '_', $name));
  53. if (isset($this->data[$name])) {
  54. return $this->data[$name];
  55. }
  56. return $this->getEnv($name, $default);
  57. }
  58. protected function getEnv(string $name, $default = null)
  59. {
  60. $result = getenv('PHP_' . $name);
  61. if (false === $result) {
  62. return $default;
  63. }
  64. if ('false' === $result) {
  65. $result = false;
  66. } elseif ('true' === $result) {
  67. $result = true;
  68. }
  69. if (!isset($this->data[$name])) {
  70. $this->data[$name] = $result;
  71. }
  72. return $result;
  73. }
  74. /**
  75. * 设置环境变量值
  76. * @access public
  77. * @param string|array $env 环境变量
  78. * @param mixed $value 值
  79. * @return void
  80. */
  81. public function set($env, $value = null): void
  82. {
  83. if (is_array($env)) {
  84. $env = array_change_key_case($env, CASE_UPPER);
  85. foreach ($env as $key => $val) {
  86. if (is_array($val)) {
  87. foreach ($val as $k => $v) {
  88. $this->data[$key . '_' . strtoupper($k)] = $v;
  89. }
  90. } else {
  91. $this->data[$key] = $val;
  92. }
  93. }
  94. } else {
  95. $name = strtoupper(str_replace('.', '_', $env));
  96. $this->data[$name] = $value;
  97. }
  98. }
  99. /**
  100. * 检测是否存在环境变量
  101. * @access public
  102. * @param string $name 参数名
  103. * @return bool
  104. */
  105. public function has(string $name): bool
  106. {
  107. return !is_null($this->get($name));
  108. }
  109. /**
  110. * 设置环境变量
  111. * @access public
  112. * @param string $name 参数名
  113. * @param mixed $value 值
  114. */
  115. public function __set(string $name, $value): void
  116. {
  117. $this->set($name, $value);
  118. }
  119. /**
  120. * 获取环境变量
  121. * @access public
  122. * @param string $name 参数名
  123. * @return mixed
  124. */
  125. public function __get(string $name)
  126. {
  127. return $this->get($name);
  128. }
  129. /**
  130. * 检测是否存在环境变量
  131. * @access public
  132. * @param string $name 参数名
  133. * @return bool
  134. */
  135. public function __isset(string $name): bool
  136. {
  137. return $this->has($name);
  138. }
  139. // ArrayAccess
  140. public function offsetSet($name, $value): void
  141. {
  142. $this->set($name, $value);
  143. }
  144. public function offsetExists($name): bool
  145. {
  146. return $this->__isset($name);
  147. }
  148. public function offsetUnset($name)
  149. {
  150. throw new Exception('not support: unset');
  151. }
  152. public function offsetGet($name)
  153. {
  154. return $this->get($name);
  155. }
  156. }