Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Bootstrap/LoadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, mixed> $base
* @param array<string, mixed> $overrides
*
* @return array<string, mixed>
*/
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;
}
}
Loading