博主信息
博文 24
粉丝 0
评论 2
访问量 33606
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
congfig类的阅读
路雲萧的博客
原创
859人浏览过

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 https://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

class Config
{
   // 配置参数
   private static $config = [];
   // 参数作用域
   private static $range = '_sys_';

   // 设定配置参数的作用域
   public static function range($range)
   {
       self::$range = $range;
       if (!isset(self::$config[$range])) {
           self::$config[$range] = [];
       }
   }

   /**
    * 解析配置文件或内容
    * @param string    $config 配置文件路径或内容
    * @param string    $type 配置解析类型
    * @param string    $name 配置名(如设置即表示二级配置)
    * @param string    $range  作用域
    * @return mixed
    */
   public static function parse($config, $type = '', $name = '', $range = '')
   {
       $range = $range ?: self::$range;
       if (empty($type)) {
           $type = pathinfo($config, PATHINFO_EXTENSION);
       }
       $class = false !== strpos($type, '\\') ? $type : '\\think\\config\\driver\\' . ucwords($type);
       return self::set((new $class())->parse($config), $name, $range);
   }

   /**
    * 加载配置文件(PHP格式)
    * @param string    $file 配置文件名
    * @param string    $name 配置名(如设置即表示二级配置)
    * @param string    $range  作用域
    * @return mixed
    */
   public static function load($file, $name = '', $range = '')
   {
       $range = $range ?: self::$range;
       if (!isset(self::$config[$range])) {
           self::$config[$range] = [];
       }
       if (is_file($file)) {
           $name = strtolower($name);
           $type = pathinfo($file, PATHINFO_EXTENSION);
           if ('php' == $type) {
               return self::set(include $file, $name, $range);
           } elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
               return self::set(yaml_parse_file($file), $name, $range);
           } else {
               return self::parse($file, $type, $name, $range);
           }
       } else {
           return self::$config[$range];
       }
   }

   /**
    * 检测配置是否存在
    * @param string    $name 配置参数名(支持二级配置 .号分割)
    * @param string    $range  作用域
    * @return bool
    */
   public static function has($name, $range = '')
   {
       $range = $range ?: self::$range;

       if (!strpos($name, '.')) {
           return isset(self::$config[$range][strtolower($name)]);
       } else {
           // 二维数组设置和获取支持
           $name = explode('.', $name, 2);
           return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
       }
   }

   /**
    * 获取配置参数 为空则获取所有配置
    * @param string    $name 配置参数名(支持二级配置 .号分割)
    * @param string    $range  作用域
    * @return mixed
    */
   public static function get($name = null, $range = '')
   {
       $range = $range ?: self::$range;
       // 无参数时获取所有
       if (empty($name) && isset(self::$config[$range])) {
           return self::$config[$range];
       }

       if (!strpos($name, '.')) {
           $name = strtolower($name);
           return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
       } else {
           // 二维数组设置和获取支持
           $name    = explode('.', $name, 2);
           $name[0] = strtolower($name[0]);

           if (!isset(self::$config[$range][$name[0]])) {
               // 动态载入额外配置
               $module = Request::instance()->module();
               $file   = CONF_PATH . ($module ? $module . DS : '') . 'extra' . DS . $name[0] . CONF_EXT;

               is_file($file) && self::load($file, $name[0]);
           }

           return isset(self::$config[$range][$name[0]][$name[1]]) ? self::$config[$range][$name[0]][$name[1]] : null;
       }
   }

   /**
    * 设置配置参数 name为数组则为批量设置
    * @param string|array  $name 配置参数名(支持二级配置 .号分割)
    * @param mixed         $value 配置值
    * @param string        $range  作用域
    * @return mixed
    */
   public static function set($name, $value = null, $range = '')
   {
       $range = $range ?: self::$range;
       if (!isset(self::$config[$range])) {
           self::$config[$range] = [];
       }
       if (is_string($name)) {
           if (!strpos($name, '.')) {
               self::$config[$range][strtolower($name)] = $value;
           } else {
               // 二维数组设置和获取支持
               $name                                                 = explode('.', $name, 2);
               self::$config[$range][strtolower($name[0])][$name[1]] = $value;
           }
           return;
       } elseif (is_array($name)) {
           // 批量设置
           if (!empty($value)) {
               self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
               array_merge(self::$config[$range][$value], $name) : $name;
               return self::$config[$range][$value];
           } else {
               return self::$config[$range] = array_merge(self::$config[$range], array_change_key_case($name));
           }
       } else {
           // 为空直接返回 已有配置
           return self::$config[$range];
       }
   }

   /**
    * 重置配置参数
    */
   public static function reset($range = '')
   {
       $range = $range ?: self::$range;
       if (true === $range) {
           self::$config = [];
       } else {
           self::$config[$range] = [];
       }
   }
}

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学