Skip to content

Commit ebbb7e0

Browse files
committed
feat: revert nested config to flat config
1 parent 1209c52 commit ebbb7e0

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

src/App.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function __construct(array $userSettings = [])
3535
$this->setupErrorHandler();
3636
$this->loadConfig($userSettings);
3737

38-
if (!empty(Config::getStatic('scripts'))) {
39-
foreach (Config::getStatic('scripts') as $script) {
38+
$scripts = Config::getStatic('scripts');
39+
40+
if (!empty($scripts)) {
41+
foreach ($scripts as $script) {
4042
\call_user_func($script, $this);
4143
}
4244

@@ -118,7 +120,10 @@ private function setupDefaultContainer()
118120
return new Http\Headers();
119121
});
120122

121-
Config::set('app.instance', $this);
123+
Config::singleton('app', function () {
124+
return $this;
125+
});
126+
122127
Config::set('mode', _env('APP_ENV', Config::getStatic('mode')));
123128
}
124129

@@ -208,7 +213,7 @@ public function cors($options = [])
208213
public function ws(string $name, callable $callback)
209214
{
210215
Config::set('eien.events', \array_merge(
211-
Config::getStatic('eien')['events'] ?? [],
216+
Config::getStatic('eien.events') ?? [],
212217
[$name => $callback]
213218
));
214219
}
@@ -339,7 +344,7 @@ public static function environment($mode, $callback)
339344
*/
340345
public static function run(?callable $callback = null)
341346
{
342-
if (\class_exists('Leaf\Eien\Server') && Config::getStatic('eien')['enabled']) {
347+
if (\class_exists('Leaf\Eien\Server') && Config::getStatic('eien.enabled')) {
343348
server()
344349
->wrap(function () use ($callback) {
345350
parent::run($callback);

src/Config.php

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ class Config
1616
* @var array
1717
*/
1818
protected static $context = [
19-
'app' => ['down' => false, 'instance' => null],
19+
'app' => null,
20+
'app.down' => false,
2021
'debug' => true,
21-
'eien' => ['enabled' => true],
22-
'http' => ['version' => '1.1'],
23-
'log' => [
24-
'writer' => null,
25-
'level' => null,
26-
'enabled' => false,
27-
'dir' => __DIR__ . '/../../../../storage/logs/',
28-
'file' => 'log.txt',
29-
'open' => true,
30-
],
22+
'eien.enabled' => true,
23+
'http.version' => '1.1',
24+
'log.writer' => null,
25+
'log.level' => null,
26+
'log.enabled' => false,
27+
'log.dir' => __DIR__ . '/../../../../storage/logs/',
28+
'log.file' => 'log.txt',
29+
'log.open' => true,
3130
'mode' => 'development',
3231
'scripts' => [],
33-
'views' => ['path' => null, 'cachePath' => null],
32+
'views.path' => null,
33+
'views.cachePath' => null,
3434
];
3535

3636
/**
@@ -42,18 +42,9 @@ class Config
4242
public static function set($item, $value = null)
4343
{
4444
if (\is_string($item)) {
45-
if (!\strpos($item, '.')) {
46-
static::$context[$item] = $value;
47-
} else {
48-
static::$context = \array_merge(
49-
static::$context,
50-
static::mapContext($item, $value)
51-
);
52-
}
45+
static::$context[$item] = $value;
5346
} else {
54-
foreach ($item as $k => $v) {
55-
static::set($k, $v);
56-
}
47+
static::$context = \array_merge(static::$context, $item);
5748
}
5849
}
5950

@@ -82,7 +73,7 @@ public static function get($item = null)
8273
return static::$context;
8374
}
8475

85-
$value = \Leaf\Anchor::deepGetDot(static::$context, $item);
76+
$value = static::$context[$item] ?? null;
8677

8778
return static::isInvokable($value) ? $value(static::$context) : $value;
8879
}
@@ -198,33 +189,23 @@ public static function reset()
198189
{
199190
static::$context = [
200191
'app' => static::$context['app'],
192+
'app.down' => static::$context['app.down'],
201193
'debug' => static::$context['debug'],
202-
'eien' => static::$context['eien'],
203-
'http' => static::$context['http'],
204-
'log' => static::$context['log'],
194+
'eien.enabled' => static::$context['eien.enabled'],
195+
'http.version' => static::$context['http.version'],
196+
'log.writer' => static::$context['log.writer'],
197+
'log.level' => static::$context['log.level'],
198+
'log.enabled' => static::$context['log.enabled'],
199+
'log.dir' => static::$context['log.dir'],
200+
'log.file' => static::$context['log.file'],
201+
'log.open' => static::$context['log.open'],
205202
'mode' => static::$context['mode'],
206203
'scripts' => [],
207-
'views' => static::$context['views'],
204+
'views.path' => static::$context['views.path'],
205+
'views.cachePath' => static::$context['views.cachePath'],
208206
];
209207
}
210208

211-
/**
212-
* Map nested config to their parents recursively
213-
*/
214-
protected static function mapContext(string $item, $value = null)
215-
{
216-
$config = \explode('.', $item);
217-
218-
if (\count($config) > 2) {
219-
\trigger_error('Nested config can\'t be more than 1 level deep');
220-
}
221-
222-
return [$config[0] => \array_merge(
223-
static::$context[$config[0]] ?? [],
224-
[$config[1] => $value]
225-
)];
226-
}
227-
228209
protected static function getDiIndex($class)
229210
{
230211
$fullName = \explode("\\", \strtolower(\get_class($class)));

src/functions.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
*/
1010
function app(): Leaf\App
1111
{
12-
$app = Leaf\Config::get('app')['instance'] ?? null;
13-
14-
if (!$app) {
15-
$app = new Leaf\App();
16-
Leaf\Config::set('app', ['instance' => $app]);
12+
if (!(\Leaf\Config::getStatic('app'))) {
13+
\Leaf\Config::singleton('app', function () {
14+
return new \Leaf\App();
15+
});
1716
}
1817

19-
return $app;
18+
return \Leaf\Config::get('app');
2019
}
2120
}
2221

0 commit comments

Comments
 (0)