| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- declare(strict_types=1);
- namespace App\Services;
- use App\Models\Config;
- use Illuminate\Database\DatabaseManager;
- use Smarty\Smarty;
- use Twig\Environment;
- use Twig\Loader\FilesystemLoader;
- use const BASE_PATH;
- final class View
- {
- public static DatabaseManager $connection;
- public static float $beginTime;
- public static function getSmarty(): Smarty
- {
- $smarty = new Smarty(); //实例化smarty
- $user = Auth::getUser();
- $smarty->setTemplateDir(BASE_PATH . '/resources/views/' . self::getTheme($user) . '/'); //设置模板文件存放目录
- $smarty->setCompileDir(BASE_PATH . '/storage/framework/smarty/compile/'); //设置生成文件存放目录
- $smarty->setCacheDir(BASE_PATH . '/storage/framework/smarty/cache/'); //设置缓存文件存放目录
- // add config
- $smarty->assign('config', self::getConfig());
- $smarty->assign('public_setting', Config::getPublicConfig());
- $smarty->assign('user', $user);
- return $smarty;
- }
- public static function getTwig(): Environment
- {
- $loader = new FilesystemLoader(BASE_PATH . '/resources/views/' . self::getTheme($user) . '/');
- $twig = new Environment($loader, [
- 'cache' => BASE_PATH . '/storage/framework/twig/cache/',
- ]);
- $twig->addGlobal('config', self::getConfig());
- $twig->addGlobal('public_setting', Config::getPublicConfig());
- $twig->addGlobal('user', $user);
- return $twig;
- }
- public static function getTheme($user): string
- {
- if ($user->isLogin) {
- $theme = $user->theme;
- } else {
- $theme = $_ENV['theme'];
- }
- return $theme;
- }
- public static function getConfig(): array
- {
- return [
- 'appName' => $_ENV['appName'],
- 'baseUrl' => $_ENV['baseUrl'],
- 'jump_delay' => $_ENV['jump_delay'],
- 'enable_kill' => $_ENV['enable_kill'],
- 'enable_change_email' => $_ENV['enable_change_email'],
- 'enable_r2_client_download' => $_ENV['enable_r2_client_download'],
- 'jsdelivr_url' => $_ENV['jsdelivr_url'],
- ];
- }
- }
|