Config.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * 配置
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2019/3/3
  7. * @time 16:41
  8. */
  9. namespace Luolongfei\Libs;
  10. class Config
  11. {
  12. /**
  13. * @var Config
  14. */
  15. protected static $instance;
  16. /**
  17. * @var array 配置
  18. */
  19. protected $allConfig;
  20. public function __construct()
  21. {
  22. $this->allConfig = require ROOT_PATH . '/config.php';
  23. }
  24. /**
  25. * 获取配置
  26. *
  27. * @param string $key
  28. * @param string $default 默认值
  29. *
  30. * @return array|mixed|null
  31. */
  32. public function get($key = '', $default = null)
  33. {
  34. $allConfig = $this->allConfig;
  35. if (strlen($key)) {
  36. if (strpos($key, '.')) {
  37. $keys = explode('.', $key);
  38. $val = $allConfig;
  39. foreach ($keys as $k) {
  40. if (!isset($val[$k])) {
  41. return $default; // 任一下标不存在就返回默认值
  42. }
  43. $val = $val[$k];
  44. }
  45. return $val;
  46. } else {
  47. if (isset($allConfig[$key])) {
  48. return $allConfig[$key];
  49. }
  50. return $default;
  51. }
  52. }
  53. return $allConfig;
  54. }
  55. public static function instance()
  56. {
  57. if (!self::$instance instanceof self) {
  58. self::$instance = new self();
  59. }
  60. return self::$instance;
  61. }
  62. }