Skip to content

Commit

Permalink
Add config defaults support to the localhost site provider
Browse files Browse the repository at this point in the history
  • Loading branch information
acharron-hl committed Dec 7, 2023
1 parent 3d30428 commit 8fc1f4d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/Local/LocalSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public function __construct(string $configPath, SiteRecord $siteRecord, LocalSit
*/
protected function loadSiteConfig(): array
{
// Config defaults
$configDefaults = $this->siteProvider->loadPhpConfigFile(
$this->siteProvider->siteConfigFsBasePath . "/config-defaults.php",
);

$dockerDefaultsPath = $this->siteProvider->siteConfigFsBasePath . "/docker-defaults.php";
$dockerDefaults = file_exists($dockerDefaultsPath)
? $this->siteProvider->loadPhpConfigFile($dockerDefaultsPath)
: [];

try {
$clusterConfigPath = "/clusters/" . $this->getClusterID() . ".php";
$clusterConfig = $this->siteProvider->loadPhpConfigFile($clusterConfigPath);
Expand All @@ -50,9 +60,18 @@ protected function loadSiteConfig(): array

$siteConfig = $this->siteProvider->loadPhpConfigFile($this->configPath);

$config = ArrayUtils::mergeRecursive($clusterConfig, $siteConfig, function ($a, $b) {
return $b;
});
return $config;
$configs = [$configDefaults, $dockerDefaults, $clusterConfig, $siteConfig];

$finalConfig = array_reduce(
$configs,
function (array $acc, array $new) {
return ArrayUtils::mergeRecursive($acc, $new, function ($a, $b) {
return $b;
});
},
[],
);

return $finalConfig;
}
}
2 changes: 1 addition & 1 deletion src/Local/LocalSiteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LocalSiteProvider extends SiteProvider
const CONF_ACCOUNT_ID = "Vanilla.AccountID";
const CONF_SITE_ID = "Vanilla.SiteID";

private string $siteConfigFsBasePath;
public string $siteConfigFsBasePath;

/**
* @param string $siteConfigFsBasePath
Expand Down
2 changes: 2 additions & 0 deletions tests/LocalSitesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private function expectedSites(): array
$commonConfig = [
"Config1" => "val1",
"Nested.Nested1" => "valnested1",
"DefaultConfig" => "foo",
"DockerConfig" => "bar",
];
return [
self::SID_CFG_PHP => new ExpectedSite(
Expand Down
2 changes: 2 additions & 0 deletions tests/configs/config-defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$Configuration["DefaultConfig"] = "foo";
2 changes: 2 additions & 0 deletions tests/configs/docker-defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$Configuration["DockerConfig"] = "bar";

0 comments on commit 8fc1f4d

Please sign in to comment.