No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Env.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. namespace think;
  12. class Env
  13. {
  14. /**
  15. * 环境变量数据
  16. * @var array
  17. */
  18. protected $data = [];
  19. /**
  20. * 数据转换映射
  21. * @var array
  22. */
  23. protected $convert = [
  24. 'true' => true,
  25. 'false' => false,
  26. 'off' => false,
  27. 'on' => true,
  28. ];
  29. public function __construct()
  30. {
  31. $this->data = $_ENV;
  32. }
  33. /**
  34. * 读取环境变量定义文件
  35. * @access public
  36. * @param string $file 环境变量定义文件
  37. * @return void
  38. */
  39. public function load($file)
  40. {
  41. $env = parse_ini_file($file, true, INI_SCANNER_RAW) ?: [];
  42. $this->set($env);
  43. }
  44. /**
  45. * 获取环境变量值
  46. * @access public
  47. * @param string $name 环境变量名
  48. * @param mixed $default 默认值
  49. * @return mixed
  50. */
  51. public function get($name = null, $default = null, $php_prefix = true)
  52. {
  53. if (is_null($name)) {
  54. return $this->data;
  55. }
  56. $name = strtoupper(str_replace('.', '_', $name));
  57. if (isset($this->data[$name])) {
  58. $result = $this->data[$name];
  59. if (is_string($result) && isset($this->convert[$result])) {
  60. return $this->convert[$result];
  61. }
  62. return $result;
  63. }
  64. return $this->getEnv($name, $default, $php_prefix);
  65. }
  66. protected function getEnv($name, $default = null, $php_prefix = true)
  67. {
  68. if ($php_prefix) {
  69. $name = 'PHP_' . $name;
  70. }
  71. $result = getenv($name);
  72. if (false === $result) {
  73. return $default;
  74. }
  75. if ('false' === $result) {
  76. $result = false;
  77. } elseif ('true' === $result) {
  78. $result = true;
  79. }
  80. if (!isset($this->data[$name])) {
  81. $this->data[$name] = $result;
  82. }
  83. return $result;
  84. }
  85. /**
  86. * 设置环境变量值
  87. * @access public
  88. * @param string|array $env 环境变量
  89. * @param mixed $value 值
  90. * @return void
  91. */
  92. public function set($env, $value = null)
  93. {
  94. if (is_array($env)) {
  95. $env = array_change_key_case($env, CASE_UPPER);
  96. foreach ($env as $key => $val) {
  97. if (is_array($val)) {
  98. foreach ($val as $k => $v) {
  99. $this->data[$key . '_' . strtoupper($k)] = $v;
  100. }
  101. } else {
  102. $this->data[$key] = $val;
  103. }
  104. }
  105. } else {
  106. $name = strtoupper(str_replace('.', '_', $env));
  107. $this->data[$name] = $value;
  108. }
  109. }
  110. }