Skip to content

Commit 33f42da

Browse files
committed
增加配置的深度合并,避免覆盖
1 parent 2274695 commit 33f42da

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

helper.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ function site_config($key)
4949
return Config::get($key);
5050
}
5151

52+
function array_deep_merge(array $a, array $b)
53+
{
54+
foreach ($a as $key => $val) {
55+
if (isset($b[$key])) {
56+
if (gettype($a[$key]) != gettype($b[$key])) {
57+
continue;
58+
}
59+
if (is_array($a[$key])) {
60+
$a[$key] = array_deep_merge($a[$key], $b[$key]);
61+
} else {
62+
$a[$key] = $b[$key];
63+
}
64+
}
65+
}
66+
67+
return $a;
68+
}
69+
5270
\think\Console::addDefaultCommands([
5371
'tadmin:init' => \tadmin\command\Init::class,
5472
'tadmin:migrate:run' => \tadmin\command\Migrate::class,

src/behavior/Boot.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ protected function loadConfig()
6666
$configName = pathinfo($file, PATHINFO_FILENAME);
6767
$config = $this->app->config->pull($configName);
6868

69-
$this->app->config->load($file, $configName);
69+
$config = array_deep_merge(
70+
require_once $file,
71+
$config
72+
);
73+
74+
$this->app->config->set($config, $configName);
7075

7176
// 重新加载应用中的同名配置,以覆盖此配置
72-
if (is_file($this->app->getConfigPath().basename($file))) {
73-
$this->app->config->load($this->app->getConfigPath().basename($file), $configName);
74-
}
77+
// if (is_file($this->app->getConfigPath().basename($file))) {
78+
// $this->app->config->load($this->app->getConfigPath().basename($file), $configName);
79+
// }
7580
}
7681
}
7782
}

0 commit comments

Comments
 (0)