Skip to content

Commit 5cd3088

Browse files
committed
feat: rework view engine
1 parent bf8b952 commit 5cd3088

File tree

2 files changed

+34
-59
lines changed

2 files changed

+34
-59
lines changed

src/Core.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ public static function loadApplicationConfig()
6464
\Leaf\Vite::config('build', 'public/build');
6565
\Leaf\Vite::config('hotFile', 'public/hot');
6666
}
67+
68+
Config::attachView(ViewConfig('viewEngine'), 'template');
69+
70+
if (ViewConfig('config')) {
71+
call_user_func_array(ViewConfig('config'), [
72+
app()->template(),
73+
[
74+
'views' => AppConfig('views.path'),
75+
'cache' => AppConfig('views.cachePath'),
76+
]
77+
]);
78+
} else if (method_exists(app()->template(), 'configure')) {
79+
app()->template()->configure([
80+
'views' => AppConfig('views.path'),
81+
'cache' => AppConfig('views.cachePath'),
82+
]);
83+
}
84+
85+
if (is_callable(ViewConfig('extend'))) {
86+
call_user_func_array(ViewConfig('extend'), app()->template());
87+
}
6788
}
6889
}
6990

@@ -79,12 +100,12 @@ protected static function loadConfig()
79100
'app.down' => _env('APP_DOWN', false),
80101
'debug' => _env('APP_DEBUG', true),
81102
'log.dir' => 'storage/logs/',
82-
'log.enabled' => true,
103+
'log.enabled' => _env('APP_LOG_ENABLED', true),
83104
'log.file' => 'app.log',
84105
'log.level' => Log::DEBUG,
85106
'log.open' => true,
86107
'log.writer' => null,
87-
'mode' => 'development',
108+
'mode' => _env('APP_ENV', 'development'),
88109
'views.path' => ViewsPath(null, false),
89110
'views.cachePath' => StoragePath('framework/views')
90111
],
@@ -95,11 +116,11 @@ protected static function loadConfig()
95116
'timestamps.format' => 'YYYY-MM-DD HH:mm:ss',
96117
'unique' => ['email'],
97118
'hidden' => ['field.id', 'field.password'],
98-
'session' => true,
119+
'session' => _env('AUTH_SESSION', true),
99120
'session.lifetime' => 60 * 60 * 24,
100121
'session.cookie' => ['secure' => false, 'httponly' => true, 'samesite' => 'lax'],
101122
'token.lifetime' => 60 * 60 * 24 * 365,
102-
'token.secret' => _env('TOKEN_SECRET', '@leaf$MVC*JWT#AUTH.Secret'),
123+
'token.secret' => _env('AUTH_TOKEN_SECRET', '@leaf$MVC*JWT#AUTH.Secret'),
103124
'messages.loginParamsError' => 'Incorrect credentials!',
104125
'messages.loginPasswordError' => 'Password is incorrect!',
105126
'password.key' => 'password',
@@ -111,10 +132,10 @@ protected static function loadConfig()
111132
},
112133
],
113134
'cors' => [
114-
'origin' => '*',
115-
'methods' => 'GET,HEAD,PUT,PATCH,POST,DELETE',
116-
'allowedHeaders' => '*',
117-
'exposedHeaders' => '',
135+
'origin' => _env('CORS_ALLOWED_ORIGINS', '*'),
136+
'methods' => _env('CORS_ALLOWED_METHODS', 'GET,HEAD,PUT,PATCH,POST,DELETE'),
137+
'allowedHeaders' => _env('CORS_ALLOWED_HEADERS', '*'),
138+
'exposedHeaders' => _env('CORS_EXPOSED_HEADERS', ''),
118139
'credentials' => false,
119140
'maxAge' => null,
120141
'preflightContinue' => false,
@@ -188,10 +209,11 @@ protected static function loadConfig()
188209
],
189210
'view' => [
190211
'viewEngine' => \Leaf\Blade::class,
191-
'render' => null,
192-
'config' => function ($config) {
193-
\Leaf\Config::get('views.blade')->configure($config['views'], $config['cache']);
212+
'config' => function ($engine, $config) {
213+
$engine->configure($config['views'], $config['cache']);
194214
},
215+
'render' => null,
216+
'extend' => null,
195217
],
196218
'mail' => [
197219
'host' => _env('MAIL_HOST', 'smtp.mailtrap.io'),

src/globals/functions.php

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,7 @@ function assets($assets = '')
2222
*/
2323
function view(string $view, $data = [])
2424
{
25-
/// WILL REFACTOR IN NEXT VERSION
26-
27-
if (is_object($data)) {
28-
$data = (array) $data;
29-
}
30-
31-
if (ViewConfig('render')) {
32-
if (ViewConfig('config')) {
33-
call_user_func_array(ViewConfig('config'), [[
34-
'views' => AppConfig('views.path'),
35-
'cache' => AppConfig('views.cachePath'),
36-
]]);
37-
}
38-
39-
return ViewConfig('render')($view, $data);
40-
}
41-
42-
$engine = ViewConfig('viewEngine');
43-
$className = strtolower(get_class(new $engine));
44-
45-
$fullName = explode('\\', $className);
46-
$className = $fullName[count($fullName) - 1];
47-
48-
if (\Leaf\Config::getStatic("views.$className")) {
49-
if (ViewConfig('config')) {
50-
call_user_func_array(ViewConfig('config'), [[
51-
'views' => AppConfig('views.path'),
52-
'cache' => AppConfig('views.cachePath'),
53-
]]);
54-
} else {
55-
\Leaf\Config::get("views.$className")->configure(AppConfig('views.path'), AppConfig('views.cachePath'));
56-
}
57-
58-
return \Leaf\Config::get("views.$className")->render($view, $data);
59-
}
60-
61-
$engine = new $engine($engine);
62-
63-
if (ViewConfig('config')) {
64-
call_user_func_array(ViewConfig('config'), [[
65-
'views' => AppConfig('views.path'),
66-
'cache' => AppConfig('views.cachePath'),
67-
]]);
68-
} else {
69-
$engine->config(AppConfig('views.path'), AppConfig('views.cachePath'));
70-
}
71-
72-
return $engine->render($view, $data);
25+
return app()->template()->render($view, $data);
7326
}
7427
}
7528

0 commit comments

Comments
 (0)