Avoid globals where possible

$logged => logged()
$account_logged => accountLogged()
This commit is contained in:
slawkens
2025-03-16 12:36:22 +01:00
parent a71f41193c
commit 13b8fcf454
85 changed files with 226 additions and 192 deletions

View File

@@ -16,7 +16,7 @@ use Twig\Loader\FilesystemLoader;
class App
{
private bool $isLoggedIn = false;
private ?\OTS_Account $accountLogged = null;
private ?\OTS_Account $accountLogged;
private array $instances = [];
public function run(): void
@@ -37,9 +37,10 @@ class App
$this->accountLogged = $checkLogin['account'];
$this->isLoggedIn = $checkLogin['logged'];
global $logged, $account_logged;
global $logged, $account_logged, $logged_flags;
$logged = $this->isLoggedIn;
$account_logged = $this->accountLogged;
$logged_flags = $checkLogin['flags'];
$statusService = new StatusService();
$status = $statusService->checkStatus();

View File

@@ -119,7 +119,7 @@ class Changelog
{
global $template_name;
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled())
{
$tmp = '';
@@ -134,7 +134,7 @@ class Changelog
static public function clearCache()
{
global $template_name;
$cache = Cache::getInstance();
$cache = app()->get('cache');
if (!$cache->enabled()) {
return;
}

View File

@@ -54,7 +54,7 @@ class MigrateRunCommand extends Command
private function executeMigration($id, $io): void
{
global $db;
$db = app()->get('database');
$db->revalidateCache();

View File

@@ -132,7 +132,7 @@ class CreateCharacter
return false;
}
global $db;
$db = app()->get('database');
if($sex == "0")
$playerSample->setLookType(136);
@@ -250,7 +250,7 @@ class CreateCharacter
return false;
}
global $twig;
$twig = app()->get('twig');
$twig->display('success.html.twig', array(
'title' => 'Character Created',
'description' => 'The character <b>' . $name . '</b> has been created.<br/>

View File

@@ -20,25 +20,25 @@ class Data
public function get($where)
{
global $db;
$db = app()->get('database');
return $db->select($this->table, $where);
}
public function add($data)
{
global $db;
$db = app()->get('database');
return $db->insert($this->table, $data);
}
public function delete($data, $where)
{
global $db;
$db = app()->get('database');
return $db->delete($this->table, $data, $where);
}
public function update($data, $where)
{
global $db;
$db = app()->get('database');
return $db->update($this->table, $data, $where);
}
}

View File

@@ -81,12 +81,12 @@ class DataLoader
self::$startTime = microtime(true);
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$cache->delete('towns'); // will be reloaded after next page load
}
global $db;
$db = app()->get('database');
if ($db->hasTable('towns') && Town::count() > 0) {
success(self::$locale['step_database_loaded_towns'] . self::getLoadedTime());
}

View File

@@ -37,13 +37,15 @@ class Forum
*/
public static function canPost($account)
{
global $db, $config;
if(!$account->isLoaded() || $account->isBanned())
if(!$account->isLoaded() || $account->isBanned()) {
return false;
}
if(self::isModerator())
if(self::isModerator()) {
return true;
}
$db = app()->get('database');
return
$db->query(
@@ -58,7 +60,7 @@ class Forum
public static function add_thread($title, $body, $section_id, $player_id, $account_id, &$errors)
{
global $db;
$db = app()->get('database');
$thread_id = 0;
if($db->insert(FORUM_TABLE_PREFIX . 'forum', array(
'first_post' => 0,
@@ -83,7 +85,7 @@ class Forum
public static function add_post($thread_id, $section, $author_aid, $author_guid, $post_text, $post_topic, $smile, $html)
{
global $db;
$db = app()->get('database');
$db->insert(FORUM_TABLE_PREFIX . 'forum', array(
'first_post' => $thread_id,
'section' => $section,
@@ -99,7 +101,7 @@ class Forum
}
public static function add_board($name, $description, $access, $guild, &$errors)
{
global $db;
$db = app()->get('database');
if(isset($name[0]) && isset($description[0]))
{
$query = $db->select(TABLE_PREFIX . 'forum_boards', array('name' => $name));
@@ -129,19 +131,21 @@ class Forum
return !count($errors);
}
public static function get_board($id) {
global $db;
public static function get_board($id)
{
$db = app()->get('database');
return $db->select(TABLE_PREFIX . 'forum_boards', array('id' => $id));
}
public static function update_board($id, $name, $access, $guild, $description) {
global $db;
public static function update_board($id, $name, $access, $guild, $description)
{
$db = app()->get('database');
$db->update(TABLE_PREFIX . 'forum_boards', array('name' => $name, 'description' => $description, 'access' => $access, 'guild' => $guild), array('id' => $id));
}
public static function delete_board($id, &$errors)
{
global $db;
$db = app()->get('database');
if(isset($id))
{
if(self::get_board($id) !== false)
@@ -157,7 +161,7 @@ class Forum
public static function toggleHide_board($id, &$errors)
{
global $db;
$db = app()->get('database');
if(isset($id))
{
$query = self::get_board($id);
@@ -174,7 +178,7 @@ class Forum
public static function move_board($id, $i, &$errors)
{
global $db;
$db = app()->get('database');
$query = self::get_board($id);
if($query !== false)
{
@@ -295,12 +299,12 @@ class Forum
$hasAccess = true;
$section = $sections[$board_id];
if($section['guild'] > 0) {
if(app()->isLoggedIn()) {
if(logged()) {
$guild = new \OTS_Guild();
$guild->load($section['guild']);
$status = false;
if($guild->isLoaded()) {
$account_players = app()->getAccountLogged()->getPlayersList();
$account_players = accountLogged()->getPlayersList();
foreach ($account_players as $player) {
if($guild->hasMember($player)) {
$status = true;
@@ -316,7 +320,7 @@ class Forum
}
if($section['access'] > 0) {
$logged_access = app()->isLoggedIn() ? app()->getAccountLogged()->getAccess() : 0;
$logged_access = logged() ? accountLogged()->getAccess() : 0;
if($logged_access < $section['access']) {
$hasAccess = false;
}

View File

@@ -11,7 +11,7 @@ class Guild extends Model {
public function owner()
{
global $db;
$db = app()->get('database');
$column = 'ownerid';
if($db->hasColumn('guilds', 'owner_id')) {
$column = 'owner_id';

View File

@@ -40,7 +40,7 @@ class Player extends Model {
public function scopeOrderBySkill($query, $value)
{
global $db;
$db = app()->get('database');
$query->when($db->hasColumn('players', 'skill_fist'), function ($query) {
});
@@ -69,8 +69,9 @@ class Player extends Model {
return false;
}
public function scopeNotDeleted($query) {
global $db;
public function scopeNotDeleted($query)
{
$db = app()->get('database');
$column = 'deleted';
if($db->hasColumn('players', 'deletion')) {
@@ -82,7 +83,7 @@ class Player extends Model {
public function scopeWithOnlineStatus($query)
{
global $db;
$db = app()->get('database');
if ($db->hasColumn('players', 'online')) {
$query->addSelect('online');
}
@@ -99,7 +100,7 @@ class Player extends Model {
public function getOnlineStatusAttribute()
{
global $db;
$db = app()->get('database');
if ($db->hasColumn('players', 'online')) {
return $this->online;
}

View File

@@ -182,7 +182,7 @@ class News
{
global $template_name;
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled())
{
$tmp = '';
@@ -196,7 +196,7 @@ class News
static public function clearCache()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if (!$cache->enabled()) {
return;
}

View File

@@ -13,7 +13,7 @@ class Plugins {
public static function getRoutes()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins_routes', $tmp)) {
@@ -146,7 +146,7 @@ class Plugins {
public static function getThemes()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins_themes', $tmp)) {
@@ -178,7 +178,7 @@ class Plugins {
public static function getCommands()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins_commands', $tmp)) {
@@ -207,7 +207,7 @@ class Plugins {
public static function getHooks()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins_hooks', $tmp)) {
@@ -257,7 +257,7 @@ class Plugins {
public static function getAllPluginsSettings()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins_settings', $tmp)) {
@@ -287,7 +287,7 @@ class Plugins {
public static function getAllPluginsJson($disabled = false)
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins', $tmp)) {
@@ -357,7 +357,7 @@ class Plugins {
public static function install($file): bool
{
global $db;
$db = app()->get('database');
if(!\class_exists('\ZipArchive')) {
throw new \RuntimeException('Please install PHP zip extension. Plugins upload disabled until then.');
@@ -660,7 +660,7 @@ class Plugins {
return false;
}
global $db;
$db = app()->get('database');
if (file_exists(BASE . $plugin_json['install'])) {
$db->revalidateCache();
require BASE . $plugin_json['install'];
@@ -770,7 +770,7 @@ class Plugins {
*/
public static function installMenus($templateName, $menus, $clearOld = false)
{
global $db;
$db = app()->get('database');
if ($clearOld) {
Menu::where('template', $templateName)->delete();

View File

@@ -42,7 +42,7 @@ class RateLimit
public function increment(string $ip): bool
{
global $cache;
$cache = app()->get('cache');
if ($this->enabled && $cache->enabled()) {
if (isset($this->data[$ip]['attempts']) && isset($this->data[$ip]['last'])) {
$this->data[$ip]['attempts']++;
@@ -75,7 +75,7 @@ class RateLimit
public function save(): void
{
global $cache;
$cache = app()->get('cache');
if (!$this->enabled || !$cache->enabled()) {
return;
}
@@ -86,7 +86,7 @@ class RateLimit
public function load(): void
{
global $cache;
$cache = app()->get('cache');
if (!$this->enabled) {
return;
}

View File

@@ -6,8 +6,6 @@ class LoginService
{
public function checkLogin(): array
{
global $logged_flags;
$logged = false;
$logged_flags = 0;
$account_logged = new \OTS_Account();
@@ -23,7 +21,7 @@ class LoginService
}
else {
unsetSession('account');
unset($account_logged);
$account_logged = new \OTS_Account();
}
}
@@ -44,6 +42,7 @@ class LoginService
return [
'logged' => $logged,
'account' => $account_logged,
'flags' => $logged_flags,
];
}
}

View File

@@ -13,8 +13,8 @@ class RouterService
$db = app()->get('database');
$twig = app()->get('twig');
$logged = app()->isLoggedIn();
$account_logged = app()->getAccountLogged();
$logged = logged();
$account_logged = accountLogged();
if(!isset($content[0])) {
$content = '';
@@ -32,7 +32,7 @@ class RouterService
$load_it = false;
}
if(!$logged) {
if(!logged()) {
ob_start();
require SYSTEM . 'pages/account/manage.php';
$content .= ob_get_contents();

View File

@@ -28,7 +28,7 @@ class Settings implements \ArrayAccess
public function load()
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('settings', $tmp)) {
@@ -72,7 +72,7 @@ class Settings implements \ArrayAccess
}
}
global $db;
$db = app()->get('database');
try {
$db->beginTransaction();
@@ -630,7 +630,7 @@ class Settings implements \ArrayAccess
public function clearCache(): void
{
$cache = Cache::getInstance();
$cache = app()->get('cache');
if ($cache->enabled()) {
$cache->delete('settings');
}

View File

@@ -33,7 +33,9 @@ class UsageStatistics {
}
public static function getStats() {
global $config, $db;
global $config;
$db = app()->get('database');
$ret = array();

View File

@@ -237,7 +237,9 @@ class Validator
*/
public static function newCharacterName($name)
{
global $db, $config;
global $config;
$db = app()->get('database');
$name_lower = strtolower($name);