From c357f392a054d205580c7ed3848a96b52c150631 Mon Sep 17 00:00:00 2001 From: slawkens Date: Sun, 9 Mar 2025 22:39:16 +0100 Subject: [PATCH] [WIP] App rewrite --- system/functions.php | 10 +++++----- system/init.php | 3 ++- system/libs/pot/OTS_Player.php | 3 ++- system/pages/characters.php | 2 +- system/src/App/App.php | 28 ++++++++++++++++++++++----- system/src/Hook.php | 10 +++++++--- system/src/Services/LoginService.php | 10 ++++++---- system/src/Services/RouterService.php | 4 +++- system/src/Twig/EnvironmentBridge.php | 8 ++++---- system/template.php | 2 ++ system/templates/characters.html.twig | 2 +- templates/tibiacom/index.php | 7 +++---- 12 files changed, 59 insertions(+), 30 deletions(-) diff --git a/system/functions.php b/system/functions.php index 160e354f..85514788 100644 --- a/system/functions.php +++ b/system/functions.php @@ -575,7 +575,7 @@ function template_footer(): string // please respect my work and help spreading the word, thanks! $footer[] = base64_decode('UG93ZXJlZCBieSA8YSBocmVmPSJodHRwOi8vbXktYWFjLm9yZyIgdGFyZ2V0PSJfYmxhbmsiPk15QUFDLjwvYT4='); - global $hooks; + $hooks = app()->get('hooks'); $footer = $hooks->triggerFilter(HOOK_FILTER_THEME_FOOTER, $footer); return implode('
', $footer); @@ -1688,12 +1688,12 @@ function getAccountIdentityColumn(): string } function app() { - static $_app; - if (!isset($_app)) { - $_app = new App(); + static $__app; + if (!isset($__app)) { + $__app = new App(); } - return $_app; + return $__app; } // validator functions diff --git a/system/init.php b/system/init.php index d066c7a3..d13f1336 100644 --- a/system/init.php +++ b/system/init.php @@ -78,6 +78,7 @@ foreach($_REQUEST as $var => $value) { // load otserv config file $config_lua_reload = true; +global $cache; $cache = app()->get('cache'); if($cache->enabled()) { $tmp = null; @@ -129,7 +130,7 @@ if(!isset($foundValue)) { $foundValue = config('server_path') . 'data/'; } -$config['data_path'] = $foundValue; +config(['data_path', $foundValue]); unset($foundValue); // POT diff --git a/system/libs/pot/OTS_Player.php b/system/libs/pot/OTS_Player.php index 10e1ba98..17e52798 100644 --- a/system/libs/pot/OTS_Player.php +++ b/system/libs/pot/OTS_Player.php @@ -662,8 +662,9 @@ class OTS_Player extends OTS_Row_DAO //$groups = new DOMDocument(); //$groups->load($path); - global $groups; + $groups = app()->get('groups'); $tmp = $groups->getGroup($this->data['group_id']); + if($tmp) { return $tmp; } diff --git a/system/pages/characters.php b/system/pages/characters.php index 52f6ece0..2da16a7d 100644 --- a/system/pages/characters.php +++ b/system/pages/characters.php @@ -14,7 +14,7 @@ use MyAAC\Models\PlayerDeath; defined('MYAAC') or die('Direct access not allowed!'); $title = 'Characters'; -$groups = new OTS_Groups_List(); +$groups = app()->get('groups'); function generate_search_form($autofocus = false): string { $twig = app()->get('twig'); diff --git a/system/src/App/App.php b/system/src/App/App.php index 20f1e383..207e2295 100644 --- a/system/src/App/App.php +++ b/system/src/App/App.php @@ -16,6 +16,7 @@ use Twig\Loader\FilesystemLoader; class App { private bool $isLoggedIn = false; + private ?\OTS_Account $accountLogged = null; private array $instances = []; public function run(): void @@ -29,11 +30,12 @@ class App $template_place_holders = []; require_once SYSTEM . 'init.php'; - require_once SYSTEM . 'template.php'; $loginService = new LoginService(); - $this->isLoggedIn = $loginService->checkLogin(); + $checkLogin = $loginService->checkLogin(); + $logged = $checkLogin['logged']; + $account_logged = $checkLogin['account']; $statusService = new StatusService(); $status = $statusService->checkStatus(); @@ -53,8 +55,8 @@ class App $title = $handleRouting['title']; $content = $handleRouting['content']; - $anonymouseStatisticsService = new AnonymousStatisticsService(); - $anonymouseStatisticsService->checkReport(); + $anonymousStatisticsService = new AnonymousStatisticsService(); + $anonymousStatisticsService->checkReport(); if(setting('core.views_counter')) { require_once SYSTEM . 'counter.php'; @@ -71,12 +73,16 @@ class App */ if ($this->isLoggedIn && admin()) { $content .= $twig->render('admin-bar.html.twig', [ - 'username' => USE_ACCOUNT_NAME ? $account_logged->getName() : $account_logged->getId() + 'username' => USE_ACCOUNT_NAME ? $this->accountLogged->getName() : $this->accountLogged->getId() ]); } global $template_path, $template_index; $title_full = (isset($title) ? $title . ' - ' : '') . $config['lua']['serverName']; + + $logged = $this->isLoggedIn; + $accountLogged = $this->accountLogged; + require $template_path . '/' . $template_index; echo base64_decode('PCEtLSBQb3dlcmVkIGJ5IE15QUFDIDo6IGh0dHBzOi8vd3d3Lm15LWFhYy5vcmcvIC0tPg==') . PHP_EOL; @@ -91,6 +97,14 @@ class App $hooks->trigger(HOOK_FINISH); } + public function setAccountLogged(\OTS_Account $accountLogged): void { + $this->accountLogged = $accountLogged; + } + + public function getAccountLogged(): ?\OTS_Account { + return $this->accountLogged; + } + public function setLoggedIn($loggedIn): void { $this->isLoggedIn = $loggedIn; } @@ -116,6 +130,10 @@ class App $this->instances[$what] = $databaseService->getConnectionHandle(); break; + case 'groups': + $this->instances[$what] = new \OTS_Groups_List(); + break; + case 'hooks': $this->instances[$what] = new Hooks(); break; diff --git a/system/src/Hook.php b/system/src/Hook.php index 9f4b7978..80ac22ec 100644 --- a/system/src/Hook.php +++ b/system/src/Hook.php @@ -14,10 +14,14 @@ class Hook public function execute($params) { - global $db, $config, $template_path, $ots, $content, $twig; + global $config, $template_path, $ots, $content; - if(is_callable($this->_file)) - { + $db = app()->get('db'); + $cache = app()->get('cache'); + $hooks = app()->get('hooks'); + $twig = app()->get('twig'); + + if(is_callable($this->_file)) { $params['db'] = $db; $params['config'] = $config; $params['template_path'] = $template_path; diff --git a/system/src/Services/LoginService.php b/system/src/Services/LoginService.php index 3272411f..f73783d9 100644 --- a/system/src/Services/LoginService.php +++ b/system/src/Services/LoginService.php @@ -4,9 +4,9 @@ namespace MyAAC\Services; class LoginService { - public function checkLogin(): bool + public function checkLogin(): array { - global $logged, $logged_flags, $account_logged; + global $logged_flags; $logged = false; $logged_flags = 0; @@ -41,7 +41,9 @@ class LoginService } setSession('last_uri', $_SERVER['REQUEST_URI']); - app()->setLoggedIn($logged); - return $logged; + return [ + 'logged' => $logged, + 'account' => $account_logged, + ]; } } diff --git a/system/src/Services/RouterService.php b/system/src/Services/RouterService.php index 5e1c26a3..f703021c 100644 --- a/system/src/Services/RouterService.php +++ b/system/src/Services/RouterService.php @@ -40,7 +40,6 @@ class RouterService $uri = $_SERVER['REQUEST_URI']; if(str_contains($uri, 'index.php')) { - /** @var TYPE_NAME $uri */ $uri = str_replace_first('/index.php', '', $uri); } @@ -92,6 +91,7 @@ class RouterService /** @var \OTS_Account $account_logged */ global $logged_access; $logged_access = 0; + $account_logged = app()->getAccountLogged(); if($logged && $account_logged && $account_logged->isLoaded()) { $logged_access = $account_logged->getAccess(); } @@ -309,6 +309,8 @@ class RouterService ob_start(); global $config; + + $cache = app()->get('cache'); $hooks = app()->get('hooks'); if($hooks->trigger(HOOK_BEFORE_PAGE)) { diff --git a/system/src/Twig/EnvironmentBridge.php b/system/src/Twig/EnvironmentBridge.php index ab54bd25..d19436e1 100644 --- a/system/src/Twig/EnvironmentBridge.php +++ b/system/src/Twig/EnvironmentBridge.php @@ -8,9 +8,9 @@ class EnvironmentBridge extends Environment { public function display($name, array $context = []): void { - global $hooks; - $context['viewName'] = $name; + + $hooks = app()->get('hooks'); $context = $hooks->triggerFilter(HOOK_FILTER_TWIG_DISPLAY, $context); parent::display($name, $context); @@ -18,9 +18,9 @@ class EnvironmentBridge extends Environment public function render($name, array $context = []): string { - global $hooks; - $context['viewName'] = $name; + + $hooks = app()->get('hooks'); $context = $hooks->triggerFilter(HOOK_FILTER_TWIG_RENDER, $context); return parent::render($name, $context); diff --git a/system/template.php b/system/template.php index 1fa12517..7befccbf 100644 --- a/system/template.php +++ b/system/template.php @@ -15,6 +15,7 @@ use MyAAC\Plugins; defined('MYAAC') or die('Direct access not allowed!'); // template +global $template_name; $template_name = setting('core.template'); if(setting('core.template_allow_change')) { @@ -69,6 +70,7 @@ else { } } +global $config; if(file_exists(BASE . $template_path . '/config.php')) { require BASE . $template_path . '/config.php'; } diff --git a/system/templates/characters.html.twig b/system/templates/characters.html.twig index 2e09bd26..17baf1d0 100644 --- a/system/templates/characters.html.twig +++ b/system/templates/characters.html.twig @@ -9,7 +9,7 @@
- {{ hook(constant('HOOK_CHARACTERS_BEFORE_INFORMATIONS')) }} + {{ hook(constant('HOOK_CHARACTERS_BEFORE_INFORMATIONS')) }} {% if canEdit %} Edit diff --git a/templates/tibiacom/index.php b/templates/tibiacom/index.php index 6d0d1e63..784889f4 100644 --- a/templates/tibiacom/index.php +++ b/templates/tibiacom/index.php @@ -24,7 +24,7 @@ if(isset($config['boxes']))