fuc/src/Controllers/BaseController.php
2025-02-23 13:30:57 +08:00

93 lines
1.8 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Models\User;
use App\Services\Auth;
use App\Services\View;
use Smarty\Smarty;
use Twig\Environment;
use voku\helper\AntiXSS;
use function microtime;
use function round;
abstract class BaseController
{
/**
* @var Smarty
*/
protected Smarty $view;
/**
* @var Environment
*/
protected Environment $twig;
/**
* @var User
*/
protected User $user;
/**
* @var AntiXSS
*/
protected AntiXSS $antiXss;
/**
* Construct page renderer
*/
public function __construct()
{
$this->user = Auth::getUser();
$this->user['money_aff'] = (float) $this->user['money_aff'];
$this->antiXss = new AntiXSS();
}
/**
* Get smarty
*/
public function view(): Smarty
{
$this->view = View::getSmarty();
if (View::$connection) {
$this->view->assign(
'queryLog',
View::$connection
->connection('default')
->getQueryLog()
)->assign(
'optTime',
round((microtime(true) - View::$beginTime) * 1000, 2)
);
}
return $this->view;
}
/**
* Get twig
*/
public function twig(): Environment
{
$this->twig = View::getTwig();
if (View::$connection) {
$this->twig->addGlobal(
'queryLog',
View::$connection
->connection('default')
->getQueryLog()
);
$this->twig->addGlobal(
'optTime',
round((microtime(true) - View::$beginTime) * 1000, 2)
);
}
return $this->twig;
}
}