1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace daswork;
- class Config
- {
- // 配置参数
- private static $config = [];
- public static function load($file=''){
- if(is_file($file)) {
- self::$config = include_once($file);
- } else {
- self::$config = include_once(DAS_PATH . DS . 'config.php');
- }
- }
- /**
- * 获取配置参数 为空则获取所有配置
- * @param string $name 配置参数名(支持二级配置 .号分割)
- * @return mixed
- */
- public static function get($name='')
- {
- if ($name) {
- return self::$config[$name];
- } else {
- return self::$config;
- }
- }
- /**
- * 检测配置是否存在
- * @param string $name 配置参数名(支持二级配置 .号分割)
- * @return bool
- */
- public static function has($name)
- {
- if (!strpos($name, '.')) {
- return isset(self::$config[strtolower($name)]);
- } else {
- // 二维数组设置和获取支持
- $name = explode('.', $name, 2);
- return isset(self::$config[strtolower($name[0])][$name[1]]);
- }
- }
- /**
- * 设置配置参数 name为数组则为批量设置
- * @param string|array $name 配置参数名(支持二级配置 .号分割)
- * @param mixed $value 配置值
- * @param string $range 作用域
- * @return mixed
- */
- public static function set($name, $value = null)
- {
- if (!isset(self::$config)) {
- self::$config = [];
- }
- if (is_string($name)) {
- if (!strpos($name, '.')) {
- self::$config[strtolower($name)] = $value;
- } else {
- // 二维数组设置和获取支持
- $name = explode('.', $name, 2);
- self::$config[strtolower($name[0])][$name[1]] = $value;
- }
- return;
- } elseif (is_array($name)) {
- // 批量设置
- if (!empty($value)) {
- self::$config[$value] = isset(self::$config[$value]) ?
- array_merge(self::$config[$value], $name) :
- self::$config[$value] = $name;
- return self::$config[$value];
- } else {
- return self::$config = array_merge(self::$config, array_change_key_case($name));
- }
- } else {
- // 为空直接返回 已有配置
- return self::$config;
- }
- }
- /**
- * 重置配置参数
- */
- public static function reset()
- {
- self::$config = [];
- }
- }
|