Skip to content

Commit c8dce47

Browse files
committed
feat(core): load local and production configurations last
1 parent 914ed58 commit c8dce47

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

packages/core/src/Kernel/LoadConfig.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SplFileInfo;
1111
use Tempest\Core\ConfigCache;
1212
use Tempest\Core\Kernel;
13+
use Tempest\Support\Arr;
1314

1415
/** @internal */
1516
final readonly class LoadConfig
@@ -21,10 +22,7 @@ public function __construct(
2122

2223
public function __invoke(): void
2324
{
24-
$configPaths = $this->cache->resolve(
25-
'config_cache',
26-
fn () => $this->find(),
27-
);
25+
$configPaths = $this->cache->resolve('config_cache', fn () => $this->find());
2826

2927
foreach ($configPaths as $path) {
3028
$configFile = require $path;
@@ -55,6 +53,29 @@ public function find(): array
5553
}
5654
}
5755

58-
return $configPaths;
56+
return Arr\sort_by_callback($configPaths, function (string $path1, string $path2) {
57+
$getPriority = fn (string $path): int => match (true) {
58+
str_contains($path, '/vendor/') => 0,
59+
str_contains($path, '.local.config.php') => 5,
60+
str_contains($path, '.dev.config.php') => 5,
61+
str_contains($path, '.production.config.php') => 4,
62+
str_contains($path, '.prod.config.php') => 4,
63+
str_contains($path, '.prd.config.php') => 4,
64+
str_contains($path, '.staging.config.php') => 3,
65+
str_contains($path, '.stg.config.php') => 3,
66+
str_contains($path, '.test.config.php') => 3,
67+
str_contains($path, '.config.php') => 2,
68+
default => 1,
69+
};
70+
71+
$priorityA = $getPriority($path1);
72+
$priorityB = $getPriority($path2);
73+
74+
if ($priorityA !== $priorityB) {
75+
return $priorityA <=> $priorityB;
76+
}
77+
78+
return strcmp($path1, $path2);
79+
});
5980
}
6081
}

packages/support/src/Arr/functions.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,12 +1116,8 @@ function sort(iterable $array, bool $desc = false, ?bool $preserveKeys = null, i
11161116
* @template TValue
11171117
*
11181118
* @param iterable<TKey,TValue> $array
1119-
* @param callable $callback The function to use for comparing values. It should accept two parameters
1120-
* and return an integer less than, equal to, or greater than zero if the
1121-
* first argument is considered to be respectively less than, equal to, or
1122-
* greater than the second.
1123-
* @param bool|null $preserveKeys Preserves array keys if `true`; reindexes numerically if `false`.
1124-
* Defaults to `null`, which auto-detects preservation based on array type (associative or list).
1119+
* @param \Closure(TValue $a, TValue $b) $callback The function to use for comparing values. It should accept two parameters and return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
1120+
* @param bool|null $preserveKeys Preserves array keys if `true`; reindexes numerically if `false`. Defaults to `null`, which auto-detects preservation based on array type (associative or list).
11251121
* @return array<array-key, TValue> Key type depends on whether array keys are preserved or not.
11261122
*/
11271123
function sort_by_callback(iterable $array, callable $callback, ?bool $preserveKeys = null): array
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tempest\Database\Config\SQLiteConfig;
4+
5+
return new SQLiteConfig(
6+
path: 'dblocal.sqlite',
7+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tempest\Database\Config\SQLiteConfig;
4+
5+
return new SQLiteConfig(
6+
path: 'dbstg.sqlite',
7+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tempest\Database\Config\SQLiteConfig;
4+
5+
return new SQLiteConfig(
6+
path: 'db1.sqlite',
7+
);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Integration\Core\Config;
4+
5+
use Tempest\Core\ConfigCache;
6+
use Tempest\Core\Kernel\LoadConfig;
7+
use Tempest\Discovery\DiscoveryLocation;
8+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
9+
10+
final class LoadConfigTest extends FrameworkIntegrationTestCase
11+
{
12+
public function test_config_loaded_in_order()
13+
{
14+
$this->container->get(ConfigCache::class)->clear();
15+
16+
$this->kernel->discoveryLocations = [
17+
new DiscoveryLocation('App', __DIR__ . '/Fixtures'),
18+
];
19+
20+
$config = $this->container->get(LoadConfig::class)->find();
21+
22+
$this->assertStringContainsString('db.config.php', $config[0]);
23+
$this->assertStringContainsString('db.stg.config.php', $config[1]);
24+
$this->assertStringContainsString('db.local.config.php', $config[2]);
25+
}
26+
}

0 commit comments

Comments
 (0)