a5de0c06304be0546acee995a68b4ff77add6c9f.svn-base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php namespace Maze\Config;
  2. class Load
  3. {
  4. /**
  5. * global
  6. *
  7. * @var array
  8. */
  9. static public $global;
  10. /**
  11. * object
  12. *
  13. * @var object
  14. */
  15. static public $object;
  16. /**
  17. * get
  18. * @param string $file
  19. * @param string $path
  20. * @param string $project
  21. *
  22. * @return mixed
  23. */
  24. static public function get($file = 'host', $project = '', $path = 'config')
  25. {
  26. if($project)
  27. {
  28. $index = $project . '_' . $path . '_' . $file;
  29. }
  30. else
  31. {
  32. $index = $file;
  33. }
  34. if(empty(self::$global[$index]))
  35. {
  36. /* 暂时取消对data的读取
  37. $project = $project ? $project : MAZE_PROJECT_NAME;
  38. $config = array(MAZE_PATH, MAZE_PROJECT_PATH, MAZE_PATH . 'data/' . $project . '/');
  39. */
  40. # 增加对project的读取
  41. if($project)
  42. {
  43. $project = Project::load($project);
  44. }
  45. $root = isset($project['path']) ? $project['path'] : MAZE_PROJECT_PATH;
  46. $config = array(MAZE_PATH, $root);
  47. self::$global[$index] = array();
  48. self::file($file, $path);
  49. foreach($config as $k => $v)
  50. {
  51. self::assign($v . $path, $index);
  52. }
  53. if($file != $index)
  54. {
  55. self::$global[$file] = self::$global[$index];
  56. }
  57. }
  58. return self::$global[$index];
  59. }
  60. /**
  61. * object
  62. * @param string $file
  63. *
  64. * @var array
  65. */
  66. static private function object($file, $project = '', $path = 'config')
  67. {
  68. if($project)
  69. {
  70. $index = $project . '_' . $path . '_' . $file;
  71. }
  72. else
  73. {
  74. $index = $file;
  75. }
  76. if(isset(self::$object[$index]))
  77. {
  78. return self::$object[$index];
  79. }
  80. self::$object[$index] = (object) self::$global[$index];
  81. if($file != $index)
  82. {
  83. self::$object[$file] = self::$object[$index];
  84. }
  85. return self::$object[$index];
  86. }
  87. /**
  88. * file
  89. * @param string $file
  90. * @param string $path
  91. *
  92. * @var array
  93. */
  94. static private function file($file, &$path)
  95. {
  96. $path .= '/';
  97. if($file == 'host' || $file == 'database' || $file == 'debug')
  98. {
  99. $path .= $_SERVER['MAZEPHP_SERVER'] . '/';
  100. }
  101. $path .= $file . '.php';
  102. }
  103. /**
  104. * assign
  105. * @param string $file
  106. *
  107. * @var array
  108. */
  109. static private function assign($file, $index)
  110. {
  111. if(is_file($file))
  112. {
  113. $config = include($file);
  114. if(is_array($config))
  115. {
  116. self::$global[$index] = array_merge(self::$global[$index], $config);
  117. }
  118. }
  119. }
  120. }