Skip to content

Commit

Permalink
改进env文件解析
Browse files Browse the repository at this point in the history
  • Loading branch information
ken678 committed Sep 21, 2022
1 parent 5701663 commit a699772
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions library/think/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class Env
* @var array
*/
protected $data = [];
/**
* 数据转换映射
* @var array
*/
protected $convert = [
'true' => true,
'false' => false,
'off' => false,
'on' => true,
];

public function __construct()
{
Expand All @@ -32,7 +42,7 @@ public function __construct()
*/
public function load($file)
{
$env = parse_ini_file($file, true);
$env = parse_ini_file($file, true, INI_SCANNER_RAW) ?: [];
$this->set($env);
}

Expand All @@ -52,7 +62,11 @@ public function get($name = null, $default = null, $php_prefix = true)
$name = strtoupper(str_replace('.', '_', $name));

if (isset($this->data[$name])) {
return $this->data[$name];
$result = $this->data[$name];
if (is_string($result) && isset($this->convert[$result])) {
return $this->convert[$result];
}
return $result;
}

return $this->getEnv($name, $default, $php_prefix);
Expand Down

0 comments on commit a699772

Please sign in to comment.