0
点赞
收藏
分享

微信扫一扫

php读取env配置文件

心存浪漫 2022-02-24 阅读 127
<?php

class Env
{

    const ENV_PREFIX = '';

    /**
     * 加载配置文件
     * @access public
     * @param string $filePath 配置文件路径
     * @return void
     */

    public static function loadFile(string $filePath): void
    {

        if (!file_exists($filePath)) throw new \Exception('配置文件' . $filePath . '不存在');

        $env = parse_ini_file($filePath, true);

        foreach ($env as $key => $val) {

            $prefix = static::ENV_PREFIX . strtoupper($key);

            if (is_array($val)) {

                foreach ($val as $k => $v) {

                    $item = $prefix . '_' . strtoupper($k);

                    putenv("$item=$v");

                }

            } else {

                putenv("$prefix=$val");

            }

        }

    }

    /**
     * 获取环境变量值
     * @access public
     * @param string $name 环境变量名(支持二级 . 号分割)
     * @param string $default 默认值
     * @return mixed
     */

    public static function get(string $name, $default = null)

    {

        $result = getenv(static::ENV_PREFIX . strtoupper(str_replace('.', '_', $name)));

        if (false !== $result) {

            if ('false' === $result) {

                $result = false;

            } elseif ('true' === $result) {

                $result = true;

            }

            return $result;

        }

        return $default;

    }

}
举报

相关推荐

0 条评论