Config.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Webman;
  15. use FilesystemIterator;
  16. use RecursiveDirectoryIterator;
  17. use RecursiveIteratorIterator;
  18. use function array_replace_recursive;
  19. use function array_reverse;
  20. use function count;
  21. use function explode;
  22. use function in_array;
  23. use function is_array;
  24. use function is_dir;
  25. use function is_file;
  26. use function key;
  27. use function str_replace;
  28. class Config
  29. {
  30. /**
  31. * @var array
  32. */
  33. protected static $config = [];
  34. /**
  35. * @var string
  36. */
  37. protected static $configPath = '';
  38. /**
  39. * @var bool
  40. */
  41. protected static $loaded = false;
  42. /**
  43. * Load.
  44. * @param string $configPath
  45. * @param array $excludeFile
  46. * @param string|null $key
  47. * @return void
  48. */
  49. public static function load(string $configPath, array $excludeFile = [], string $key = null)
  50. {
  51. static::$configPath = $configPath;
  52. if (!$configPath) {
  53. return;
  54. }
  55. static::$loaded = false;
  56. $config = static::loadFromDir($configPath, $excludeFile);
  57. if (!$config) {
  58. static::$loaded = true;
  59. return;
  60. }
  61. if ($key !== null) {
  62. foreach (array_reverse(explode('.', $key)) as $k) {
  63. $config = [$k => $config];
  64. }
  65. }
  66. static::$config = array_replace_recursive(static::$config, $config);
  67. static::formatConfig();
  68. static::$loaded = true;
  69. }
  70. /**
  71. * This deprecated method will certainly be removed in the future.
  72. * @param string $configPath
  73. * @param array $excludeFile
  74. * @return void
  75. * @deprecated
  76. */
  77. public static function reload(string $configPath, array $excludeFile = [])
  78. {
  79. static::load($configPath, $excludeFile);
  80. }
  81. /**
  82. * Clear.
  83. * @return void
  84. */
  85. public static function clear()
  86. {
  87. static::$config = [];
  88. }
  89. /**
  90. * FormatConfig.
  91. * @return void
  92. */
  93. protected static function formatConfig()
  94. {
  95. $config = static::$config;
  96. // Merge log config
  97. foreach ($config['plugin'] ?? [] as $firm => $projects) {
  98. if (isset($projects['app'])) {
  99. foreach ($projects['log'] ?? [] as $key => $item) {
  100. $config['log']["plugin.$firm.$key"] = $item;
  101. }
  102. }
  103. foreach ($projects as $name => $project) {
  104. if (!is_array($project)) {
  105. continue;
  106. }
  107. foreach ($project['log'] ?? [] as $key => $item) {
  108. $config['log']["plugin.$firm.$name.$key"] = $item;
  109. }
  110. }
  111. }
  112. // Merge database config
  113. foreach ($config['plugin'] ?? [] as $firm => $projects) {
  114. if (isset($projects['app'])) {
  115. foreach ($projects['database']['connections'] ?? [] as $key => $connection) {
  116. $config['database']['connections']["plugin.$firm.$key"] = $connection;
  117. }
  118. }
  119. foreach ($projects as $name => $project) {
  120. if (!is_array($project)) {
  121. continue;
  122. }
  123. foreach ($project['database']['connections'] ?? [] as $key => $connection) {
  124. $config['database']['connections']["plugin.$firm.$name.$key"] = $connection;
  125. }
  126. }
  127. }
  128. if (!empty($config['database']['connections'])) {
  129. $config['database']['default'] = $config['database']['default'] ?? key($config['database']['connections']);
  130. }
  131. // Merge thinkorm config
  132. foreach ($config['plugin'] ?? [] as $firm => $projects) {
  133. if (isset($projects['app'])) {
  134. foreach ($projects['thinkorm']['connections'] ?? [] as $key => $connection) {
  135. $config['thinkorm']['connections']["plugin.$firm.$key"] = $connection;
  136. }
  137. }
  138. foreach ($projects as $name => $project) {
  139. if (!is_array($project)) {
  140. continue;
  141. }
  142. foreach ($project['thinkorm']['connections'] ?? [] as $key => $connection) {
  143. $config['thinkorm']['connections']["plugin.$firm.$name.$key"] = $connection;
  144. }
  145. }
  146. }
  147. if (!empty($config['thinkorm']['connections'])) {
  148. $config['thinkorm']['default'] = $config['thinkorm']['default'] ?? key($config['thinkorm']['connections']);
  149. }
  150. // Merge redis config
  151. foreach ($config['plugin'] ?? [] as $firm => $projects) {
  152. if (isset($projects['app'])) {
  153. foreach ($projects['redis'] ?? [] as $key => $connection) {
  154. $config['redis']["plugin.$firm.$key"] = $connection;
  155. }
  156. }
  157. foreach ($projects as $name => $project) {
  158. if (!is_array($project)) {
  159. continue;
  160. }
  161. foreach ($project['redis'] ?? [] as $key => $connection) {
  162. $config['redis']["plugin.$firm.$name.$key"] = $connection;
  163. }
  164. }
  165. }
  166. static::$config = $config;
  167. }
  168. /**
  169. * LoadFromDir.
  170. * @param string $configPath
  171. * @param array $excludeFile
  172. * @return array
  173. */
  174. public static function loadFromDir(string $configPath, array $excludeFile = []): array
  175. {
  176. $allConfig = [];
  177. $dirIterator = new RecursiveDirectoryIterator($configPath, FilesystemIterator::FOLLOW_SYMLINKS);
  178. $iterator = new RecursiveIteratorIterator($dirIterator);
  179. foreach ($iterator as $file) {
  180. /** var SplFileInfo $file */
  181. if (is_dir($file) || $file->getExtension() != 'php' || in_array($file->getBaseName('.php'), $excludeFile)) {
  182. continue;
  183. }
  184. $appConfigFile = $file->getPath() . '/app.php';
  185. if (!is_file($appConfigFile)) {
  186. continue;
  187. }
  188. $relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', substr($file, 0, -4));
  189. $explode = array_reverse(explode(DIRECTORY_SEPARATOR, $relativePath));
  190. if (count($explode) >= 2) {
  191. $appConfig = include $appConfigFile;
  192. if (empty($appConfig['enable'])) {
  193. continue;
  194. }
  195. }
  196. $config = include $file;
  197. foreach ($explode as $section) {
  198. $tmp = [];
  199. $tmp[$section] = $config;
  200. $config = $tmp;
  201. }
  202. $allConfig = array_replace_recursive($allConfig, $config);
  203. }
  204. return $allConfig;
  205. }
  206. /**
  207. * Get.
  208. * @param string|null $key
  209. * @param mixed $default
  210. * @return array|mixed|void|null
  211. */
  212. public static function get(string $key = null, $default = null)
  213. {
  214. if ($key === null) {
  215. return static::$config;
  216. }
  217. $keyArray = explode('.', $key);
  218. $value = static::$config;
  219. $found = true;
  220. foreach ($keyArray as $index) {
  221. if (!isset($value[$index])) {
  222. if (static::$loaded) {
  223. return $default;
  224. }
  225. $found = false;
  226. break;
  227. }
  228. $value = $value[$index];
  229. }
  230. if ($found) {
  231. return $value;
  232. }
  233. return static::read($key, $default);
  234. }
  235. /**
  236. * Read.
  237. * @param string $key
  238. * @param mixed $default
  239. * @return array|mixed|null
  240. */
  241. protected static function read(string $key, $default = null)
  242. {
  243. $path = static::$configPath;
  244. if ($path === '') {
  245. return $default;
  246. }
  247. $keys = $keyArray = explode('.', $key);
  248. foreach ($keyArray as $index => $section) {
  249. unset($keys[$index]);
  250. if (is_file($file = "$path/$section.php")) {
  251. $config = include $file;
  252. return static::find($keys, $config, $default);
  253. }
  254. if (!is_dir($path = "$path/$section")) {
  255. return $default;
  256. }
  257. }
  258. return $default;
  259. }
  260. /**
  261. * Find.
  262. * @param array $keyArray
  263. * @param mixed $stack
  264. * @param mixed $default
  265. * @return array|mixed
  266. */
  267. protected static function find(array $keyArray, $stack, $default)
  268. {
  269. if (!is_array($stack)) {
  270. return $default;
  271. }
  272. $value = $stack;
  273. foreach ($keyArray as $index) {
  274. if (!isset($value[$index])) {
  275. return $default;
  276. }
  277. $value = $value[$index];
  278. }
  279. return $value;
  280. }
  281. }