Config.Class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Mall\Framework\Core;
  3. class Config {
  4. static private $instance;
  5. /**
  6. * 所有app的配置集
  7. * array(
  8. * md5($app_conf_path) => array(
  9. *
  10. * ),
  11. * ... ,
  12. * );
  13. * @var array
  14. */
  15. static protected $appConfigs = array();
  16. /**
  17. *
  18. * 当前app的配置
  19. * @var array
  20. */
  21. protected $appConfig;
  22. /**
  23. *
  24. * 不允许直接 new
  25. */
  26. private function __construct() {}
  27. /**
  28. *
  29. * 载入配置,如果成功,返回instance
  30. * @param string $app_conf_path 配置文件的路径
  31. */
  32. static public function load($app_conf_path) {
  33. $app_conf_path = realpath($app_conf_path);
  34. $app_conf_key = md5($app_conf_path);
  35. if (!isset(self::$appConfigs[$app_conf_key])) {
  36. $config = include($app_conf_path);
  37. if (!is_array($config)) {
  38. throw new \Exception("load app_conf_path fail: {$app_conf_path}");
  39. }
  40. # 将conf的path存进来
  41. $config['conf_path'] = $app_conf_path;
  42. self::$appConfigs[$app_conf_key] = $config;
  43. }
  44. if (is_null(self::$instance)) {
  45. self::$instance = new self();
  46. }
  47. self::$instance->appConfig = self::$appConfigs[$app_conf_key];
  48. return self::$instance;
  49. }
  50. /**
  51. *
  52. * 单例模式
  53. */
  54. static public function getInstance() {
  55. if (is_null(self::$instance)) {
  56. self::$instance = new self();
  57. }
  58. if (is_null(self::$instance->appConfig)) {
  59. throw new \Exception('instance appconfig is null, pleace run Mall\Core\Config::load !');
  60. }
  61. return self::$instance;
  62. }
  63. /**
  64. *
  65. * 获取指定配置项的值
  66. * @param string $key
  67. * @return mixed
  68. */
  69. public function get($key) {
  70. if (empty($key)) {
  71. return false;
  72. }
  73. $keys = explode('.', $key);
  74. $value = $this->appConfig;
  75. foreach ($keys as $tmpKey) {
  76. if (!isset($value[$tmpKey])) {
  77. return false;
  78. }
  79. $value = $value[$tmpKey];
  80. }
  81. return $value;
  82. }
  83. /**
  84. *
  85. * 获取指定配置项下的得某一项
  86. * @param string $configKey 配置文件下的某一项
  87. * @param string $appointKey 指定项下的某一个节点
  88. * @return mixed
  89. */
  90. public function getAppoint($configKey, $appointKey) {
  91. if (!$configKey) {
  92. return false;
  93. }
  94. $allConfig = $this->appConfig;
  95. if(empty($allConfig[$configKey])){
  96. return [];
  97. }
  98. if(isset($allConfig[$configKey][$appointKey])){
  99. return $allConfig[$configKey][$appointKey];
  100. }else{
  101. return $allConfig[$configKey];
  102. }
  103. }
  104. /**
  105. *
  106. * 获取所有app的配置集
  107. * @return array
  108. */
  109. public function all() {
  110. return $this->appConfig;
  111. }
  112. }