Skip to content

Commit ebd5c76

Browse files
committed
feat: change config loading to increase performance
1 parent 4b2700f commit ebd5c76

File tree

2 files changed

+116
-120
lines changed

2 files changed

+116
-120
lines changed

src/Core.php

Lines changed: 115 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -27,81 +27,19 @@ public static function loadApplicationConfig()
2727
{
2828
static::loadConfig();
2929

30-
if (class_exists('Leaf\Auth')) {
31-
auth()->config(Config::getStatic('mvc.config.auth'));
32-
}
33-
34-
if (class_exists('Leaf\Mail')) {
35-
mailer()->connect(Config::getStatic('mvc.config.mail'));
36-
}
37-
3830
if (php_sapi_name() !== 'cli') {
39-
app()->config(Config::getStatic('mvc.config.app'));
40-
41-
if (class_exists('Leaf\Http\Cors')) {
42-
app()->cors(Config::getStatic('mvc.config.cors'));
43-
}
44-
45-
if (class_exists('Leaf\Anchor\CSRF')) {
46-
$csrfConfig = Config::getStatic('mvc.config.csrf');
47-
48-
$csrfEnabled = (
49-
$csrfConfig &&
50-
Config::getStatic('mvc.config.auth')['session'] ?? false
51-
);
52-
53-
if (($csrfConfig['enabled'] ?? null) !== null) {
54-
$csrfEnabled = $csrfConfig['enabled'];
55-
}
56-
57-
if ($csrfEnabled) {
58-
app()->csrf($csrfConfig);
59-
}
60-
}
61-
6231
if (class_exists('Leaf\Vite')) {
6332
\Leaf\Vite::config('assets', PublicPath('build'));
6433
\Leaf\Vite::config('build', 'public/build');
6534
\Leaf\Vite::config('hotFile', 'public/hot');
6635
}
6736

68-
if (ViewConfig('viewEngine')) {
69-
Config::attachView(ViewConfig('viewEngine'), 'template');
70-
71-
if (ViewConfig('config')) {
72-
call_user_func_array(ViewConfig('config'), [
73-
app()->template(),
74-
[
75-
'views' => AppConfig('views.path'),
76-
'cache' => AppConfig('views.cachePath'),
77-
]
78-
]);
79-
} else if (method_exists(app()->template(), 'configure')) {
80-
app()->template()->configure([
81-
'views' => AppConfig('views.path'),
82-
'cache' => AppConfig('views.cachePath'),
83-
]);
84-
}
85-
86-
if (is_callable(ViewConfig('extend'))) {
87-
call_user_func(ViewConfig('extend'), app()->template());
88-
}
89-
}
90-
9137
\Leaf\Database::initDb();
9238

9339
if (storage()->exists(LibPath())) {
9440
static::loadLibs();
9541
}
9642

97-
if (
98-
class_exists('Leaf\Billing\Stripe') ||
99-
class_exists('Leaf\Billing\PayStack') ||
100-
class_exists('Leaf\Billing\LemonSqueezy')
101-
) {
102-
billing(Config::getStatic('mvc.config.billing'));
103-
}
104-
10543
if (storage()->exists('app/index.php')) {
10644
require 'app/index.php';
10745
}
@@ -129,47 +67,6 @@ protected static function loadConfig()
12967
'views.path' => ViewsPath(null, false),
13068
'views.cachePath' => StoragePath('framework/views')
13169
],
132-
'auth' => [
133-
'db.table' => 'users',
134-
'id.key' => 'id',
135-
'timestamps' => true,
136-
'timestamps.format' => 'YYYY-MM-DD HH:mm:ss',
137-
'unique' => ['email'],
138-
'hidden' => ['field.id', 'field.password'],
139-
'session' => _env('AUTH_SESSION', true),
140-
'session.lifetime' => 60 * 60 * 24,
141-
'session.cookie' => ['secure' => false, 'httponly' => true, 'samesite' => 'lax'],
142-
'token.lifetime' => 60 * 60 * 24 * 365,
143-
'token.secret' => _env('AUTH_TOKEN_SECRET', '@leaf$MVC*JWT#AUTH.Secret'),
144-
'messages.loginParamsError' => 'Incorrect credentials!',
145-
'messages.loginPasswordError' => 'Password is incorrect!',
146-
'password.key' => 'password',
147-
'password.encode' => function ($password) {
148-
return \Leaf\Helpers\Password::hash($password);
149-
},
150-
'password.verify' => function ($password, $hashedPassword) {
151-
return \Leaf\Helpers\Password::verify($password, $hashedPassword);
152-
},
153-
],
154-
'cors' => [
155-
'origin' => _env('CORS_ALLOWED_ORIGINS', '*'),
156-
'methods' => _env('CORS_ALLOWED_METHODS', 'GET,HEAD,PUT,PATCH,POST,DELETE'),
157-
'allowedHeaders' => _env('CORS_ALLOWED_HEADERS', '*'),
158-
'exposedHeaders' => _env('CORS_EXPOSED_HEADERS', ''),
159-
'credentials' => false,
160-
'maxAge' => null,
161-
'preflightContinue' => false,
162-
'optionsSuccessStatus' => 204,
163-
],
164-
'csrf' => [
165-
'secret' => _env('APP_KEY', '@nkor_leaf$0Secret!!'),
166-
'secretKey' => 'X-Leaf-CSRF-Token',
167-
'except' => [],
168-
'methods' => ['POST', 'PUT', 'PATCH', 'DELETE'],
169-
'messages.tokenNotFound' => 'Token not found.',
170-
'messages.tokenInvalid' => 'Invalid token.',
171-
'onError' => null,
172-
],
17370
'database' => [
17471
'default' => _env('DB_CONNECTION', 'mysql'),
17572
'connections' => [
@@ -229,13 +126,114 @@ protected static function loadConfig()
229126
],
230127
'view' => [
231128
'viewEngine' => \Leaf\Blade::class,
232-
'config' => function ($engine, $config) {
233-
$engine->configure($config['views'], $config['cache']);
129+
'config' => function ($engine, $viewConfig) {
130+
$engine->configure($viewConfig['views'], $viewConfig['cache']);
234131
},
235132
'render' => null,
236133
'extend' => null,
237134
],
238-
'mail' => [
135+
];
136+
137+
if (storage()->exists($configPath = static::$paths['config'])) {
138+
foreach (glob("$configPath/*.php") as $configFile) {
139+
$config[basename($configFile, '.php')] = require $configFile;
140+
}
141+
}
142+
143+
app()->config($config['app']);
144+
145+
if ($config['view']['viewEngine']) {
146+
Config::attachView($config['view']['viewEngine'], 'template');
147+
148+
if ($config['view']['config']) {
149+
call_user_func_array($config['view']['config'], [
150+
app()->template(),
151+
[
152+
'views' => $config['app']['views.path'],
153+
'cache' => $config['app']['views.cachePath'],
154+
]
155+
]);
156+
} else if (method_exists(app()->template(), 'configure')) {
157+
app()->template()->configure([
158+
'views' => $config['app']['views.path'],
159+
'cache' => $config['app']['views.cachePath'],
160+
]);
161+
}
162+
163+
if (is_callable($config['view']['extend'])) {
164+
call_user_func($config['view']['extend'], app()->template());
165+
}
166+
}
167+
168+
if (class_exists('Leaf\Auth')) {
169+
$config['auth'] = [
170+
'db.table' => 'users',
171+
'id.key' => 'id',
172+
'timestamps' => true,
173+
'timestamps.format' => 'YYYY-MM-DD HH:mm:ss',
174+
'unique' => ['email'],
175+
'hidden' => ['field.id', 'field.password'],
176+
'session' => _env('AUTH_SESSION', true),
177+
'session.lifetime' => 60 * 60 * 24,
178+
'session.cookie' => ['secure' => false, 'httponly' => true, 'samesite' => 'lax'],
179+
'token.lifetime' => 60 * 60 * 24 * 365,
180+
'token.secret' => _env('AUTH_TOKEN_SECRET', '@leaf$MVC*JWT#AUTH.Secret'),
181+
'messages.loginParamsError' => 'Incorrect credentials!',
182+
'messages.loginPasswordError' => 'Password is incorrect!',
183+
'password.key' => 'password',
184+
'password.encode' => function ($password) {
185+
return \Leaf\Helpers\Password::hash($password);
186+
},
187+
'password.verify' => function ($password, $hashedPassword) {
188+
return \Leaf\Helpers\Password::verify($password, $hashedPassword);
189+
},
190+
];
191+
192+
auth()->config($config['auth']);
193+
}
194+
195+
if (class_exists('Leaf\Http\Cors')) {
196+
$config['cors'] = [
197+
'origin' => _env('CORS_ALLOWED_ORIGINS', '*'),
198+
'methods' => _env('CORS_ALLOWED_METHODS', 'GET,HEAD,PUT,PATCH,POST,DELETE'),
199+
'allowedHeaders' => _env('CORS_ALLOWED_HEADERS', '*'),
200+
'exposedHeaders' => _env('CORS_EXPOSED_HEADERS', ''),
201+
'credentials' => false,
202+
'maxAge' => null,
203+
'preflightContinue' => false,
204+
'optionsSuccessStatus' => 204,
205+
];
206+
207+
app()->cors($config['cors']);
208+
}
209+
210+
if (class_exists('Leaf\Anchor\CSRF')) {
211+
$config['csrf'] = [
212+
'secret' => _env('APP_KEY', '@nkor_leaf$0Secret!!'),
213+
'secretKey' => 'X-Leaf-CSRF-Token',
214+
'except' => [],
215+
'methods' => ['POST', 'PUT', 'PATCH', 'DELETE'],
216+
'messages.tokenNotFound' => 'Token not found.',
217+
'messages.tokenInvalid' => 'Invalid token.',
218+
'onError' => null,
219+
];
220+
221+
$csrfEnabled = (
222+
$config['csrf'] &&
223+
Config::getStatic('mvc.config.auth')['session'] ?? false
224+
);
225+
226+
if (($config['csrf']['enabled'] ?? null) !== null) {
227+
$csrfEnabled = $config['csrf']['enabled'];
228+
}
229+
230+
if ($csrfEnabled) {
231+
app()->csrf($config['csrf']);
232+
}
233+
}
234+
235+
if (class_exists('Leaf\Mail')) {
236+
$config['mail'] = [
239237
'host' => _env('MAIL_HOST', 'smtp.mailtrap.io'),
240238
'port' => _env('MAIL_PORT', 2525),
241239
'keepAlive' => true,
@@ -251,22 +249,20 @@ protected static function loadConfig()
251249
'replyToName' => _env('MAIL_REPLY_TO_NAME'),
252250
'replyToEmail' => _env('MAIL_REPLY_TO_EMAIL'),
253251
],
254-
],
255-
];
252+
];
256253

257-
foreach ($config as $configName => $config) {
258-
\Leaf\Config::set("mvc.config.$configName", $config);
254+
mailer()->connect($config['mail']);
259255
}
260256

261-
$configPath = static::$paths['config'];
262-
$configFiles = glob("$configPath/*.php");
263-
264-
foreach ($configFiles as $configFile) {
265-
$configName = basename($configFile, '.php');
266-
$config = require $configFile;
267-
268-
\Leaf\Config::set("mvc.config.$configName", $config);
257+
if (
258+
class_exists('Leaf\Billing\Stripe') ||
259+
class_exists('Leaf\Billing\PayStack') ||
260+
class_exists('Leaf\Billing\LemonSqueezy')
261+
) {
262+
billing($config['billing']);
269263
}
264+
265+
Config::set('mvc.config', $config);
270266
}
271267

272268
/**
@@ -291,7 +287,7 @@ public static function loadConsole($externalCommands = [])
291287

292288
\Leaf\Database::connect();
293289

294-
$console = new \Aloe\Console('v4.x-ALPHA');
290+
$console = new \Aloe\Console('v4.x-BETA');
295291

296292
if (\Leaf\FS\Directory::exists(static::$paths['commands'])) {
297293
$consolePath = static::$paths['commands'];

src/globals/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ function MailConfig($setting = null)
9494
*/
9595
function MvcConfig($appConfig, $setting = null)
9696
{
97-
$config = \Leaf\Config::getStatic("mvc.config.$appConfig");
97+
$config = \Leaf\Config::getStatic('mvc.config')[$appConfig] ?? null;
9898
return !$setting ? $config : ($config[$setting] ?? null);
9999
}

0 commit comments

Comments
 (0)