From d571f6180594e53b6841f919f2813c5cc77cfbb1 Mon Sep 17 00:00:00 2001 From: rivanuff Date: Mon, 31 Aug 2026 14:53:58 +0200 Subject: [PATCH] fix: apply child theme config recursively after providers register --- src/Bootstrap/LoadConfiguration.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Bootstrap/LoadConfiguration.php b/src/Bootstrap/LoadConfiguration.php index 6f19d68..c7c74a9 100644 --- a/src/Bootstrap/LoadConfiguration.php +++ b/src/Bootstrap/LoadConfiguration.php @@ -26,7 +26,12 @@ public function bootstrap(ApplicationContract $app) $childApp = clone $app; $childApp->useConfigPath(get_stylesheet_directory() . '/config'); - $this->loadChildConfigurationFiles($childApp, $app->get('config')); + // Deferred until every provider has registered, so child config also overrides package defaults. + // Example: a package's register() adds five defaults under `shop.labels`; applying a child theme's + // single-key override earlier would let that shallow merge drop the other four. + $app->booting(function () use ($app, $childApp): void { + $this->loadChildConfigurationFiles($childApp, $app->get('config')); + }); } public function loadChildConfigurationFiles(Application $childApp, Repository $repository): void @@ -38,11 +43,31 @@ public function loadChildConfigurationFiles(Application $childApp, Repository $r if (0 === count($config)) { $repository->unset($key); } else { - $repository->set($key, array_merge( + $repository->set($key, $this->mergeRecursively( $repository->get($key, []), $config )); } } } + + /** + * Merges nested arrays key by key, so a child theme overriding one nested value + * keeps the parent's siblings instead of replacing the whole array. + * + * @param array $base + * @param array $overrides + * + * @return array + */ + private function mergeRecursively(array $base, array $overrides): array + { + foreach ($overrides as $key => $value) { + $base[$key] = is_array($value) && is_array($base[$key] ?? null) && ! array_is_list($value) + ? $this->mergeRecursively($base[$key], $value) + : $value; + } + + return $base; + } }