Skip to content

Commit fe73534

Browse files
committed
Load application configuration files from <basePath>/config by default
1 parent 56f6eda commit fe73534

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/Util/Container/Application.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Lkrms\Exception\FilesystemErrorException;
1010
use Lkrms\Exception\InvalidEnvironmentException;
1111
use Lkrms\Facade\Cache;
12+
use Lkrms\Facade\Config;
1213
use Lkrms\Facade\Console;
1314
use Lkrms\Facade\Err;
1415
use Lkrms\Facade\Profile;
@@ -264,15 +265,21 @@ final public function getTempPath(bool $create = true): string
264265
* is used after removing common PHP file extensions and recognised version
265266
* numbers.
266267
*
268+
* If `$configDir` exists and is a directory, it is passed to
269+
* {@see Config::loadDirectory()} after `.env` files are loaded and applied.
270+
*
267271
* @api
268272
*
269273
* @param int-mask-of<EnvFlag::*> $envFlags Values to apply from the
270274
* environment to the running script.
275+
* @param string|null $configDir A path relative to the application's base
276+
* path, or `null` if configuration files should not be loaded.
271277
*/
272278
public function __construct(
273279
?string $basePath = null,
274280
?string $appName = null,
275-
int $envFlags = EnvFlag::ALL
281+
int $envFlags = EnvFlag::ALL,
282+
?string $configDir = 'config'
276283
) {
277284
if (!isset(self::$StartTime)) {
278285
self::$StartTime = hrtime(true);
@@ -345,6 +352,15 @@ public function __construct(
345352
if ($adodb !== null) {
346353
Err::silencePath($adodb);
347354
}
355+
356+
if ((string) $configDir !== '') {
357+
if (!File::isAbsolute($configDir)) {
358+
$configDir = "{$this->BasePath}/{$configDir}";
359+
}
360+
if (is_dir($configDir)) {
361+
Config::loadDirectory($configDir);
362+
}
363+
}
348364
}
349365

350366
/**

src/Util/Container/ApplicationInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function getBasePath(): string;
5050
public function getCachePath(): string;
5151

5252
/**
53-
* Get a writable directory for the application's configuration files
53+
* Get a writable directory for configuration files created by the
54+
* application
5455
*/
5556
public function getConfigPath(): string;
5657

tests/unit/Util/Container/ApplicationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
use Lkrms\Container\ApplicationInterface;
77
use Lkrms\Container\Container;
88
use Lkrms\Container\ContainerInterface;
9+
use Lkrms\Facade\Config;
910
use Lkrms\Tests\TestCase;
11+
use Lkrms\Utility\Catalog\EnvFlag;
1012
use Lkrms\Utility\Env;
1113
use Lkrms\Utility\File;
1214
use Psr\Container\ContainerInterface as PsrContainerInterface;
1315

1416
final class ApplicationTest extends TestCase
1517
{
18+
private const CONFIG = [
19+
'app' => [
20+
'name' => 'My App',
21+
],
22+
'services' => [],
23+
];
24+
1625
public function testBindContainer(): void
1726
{
1827
$app = new Application();
@@ -29,6 +38,34 @@ public function testBindContainer(): void
2938
$app->unload();
3039
}
3140

41+
public function testConfigDir(): void
42+
{
43+
$this->assertSame([], Config::all());
44+
45+
$basePath = File::createTempDir();
46+
$configDir = $basePath . '/config';
47+
File::createDir($configDir);
48+
foreach (self::CONFIG as $name => $data) {
49+
$data = sprintf(
50+
'<?php return %s;' . \PHP_EOL,
51+
var_export($data, true),
52+
);
53+
File::putContents("{$configDir}/{$name}.php", $data);
54+
}
55+
56+
$app = new Application($basePath, null, EnvFlag::ALL, null);
57+
$this->assertSame([], Config::all());
58+
59+
$app = new Application($basePath);
60+
$this->assertSame(self::CONFIG, Config::all());
61+
62+
$app->unload();
63+
Config::unload();
64+
65+
File::pruneDir($basePath);
66+
rmdir($basePath);
67+
}
68+
3269
/**
3370
* @backupGlobals enabled
3471
*/

0 commit comments

Comments
 (0)