Config.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace daswork;
  3. class Config
  4. {
  5. // 配置参数
  6. private static $config = [];
  7. public static function load($file=''){
  8. if(is_file($file)) {
  9. self::$config = include_once($file);
  10. } else {
  11. self::$config = include_once(DAS_PATH . DS . 'config.php');
  12. }
  13. }
  14. /**
  15. * 获取配置参数 为空则获取所有配置
  16. * @param string $name 配置参数名(支持二级配置 .号分割)
  17. * @return mixed
  18. */
  19. public static function get($name='')
  20. {
  21. if ($name) {
  22. return self::$config[$name];
  23. } else {
  24. return self::$config;
  25. }
  26. }
  27. /**
  28. * 检测配置是否存在
  29. * @param string $name 配置参数名(支持二级配置 .号分割)
  30. * @return bool
  31. */
  32. public static function has($name)
  33. {
  34. if (!strpos($name, '.')) {
  35. return isset(self::$config[strtolower($name)]);
  36. } else {
  37. // 二维数组设置和获取支持
  38. $name = explode('.', $name, 2);
  39. return isset(self::$config[strtolower($name[0])][$name[1]]);
  40. }
  41. }
  42. /**
  43. * 设置配置参数 name为数组则为批量设置
  44. * @param string|array $name 配置参数名(支持二级配置 .号分割)
  45. * @param mixed $value 配置值
  46. * @param string $range 作用域
  47. * @return mixed
  48. */
  49. public static function set($name, $value = null)
  50. {
  51. if (!isset(self::$config)) {
  52. self::$config = [];
  53. }
  54. if (is_string($name)) {
  55. if (!strpos($name, '.')) {
  56. self::$config[strtolower($name)] = $value;
  57. } else {
  58. // 二维数组设置和获取支持
  59. $name = explode('.', $name, 2);
  60. self::$config[strtolower($name[0])][$name[1]] = $value;
  61. }
  62. return;
  63. } elseif (is_array($name)) {
  64. // 批量设置
  65. if (!empty($value)) {
  66. self::$config[$value] = isset(self::$config[$value]) ?
  67. array_merge(self::$config[$value], $name) :
  68. self::$config[$value] = $name;
  69. return self::$config[$value];
  70. } else {
  71. return self::$config = array_merge(self::$config, array_change_key_case($name));
  72. }
  73. } else {
  74. // 为空直接返回 已有配置
  75. return self::$config;
  76. }
  77. }
  78. /**
  79. * 重置配置参数
  80. */
  81. public static function reset()
  82. {
  83. self::$config = [];
  84. }
  85. }