Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
class.moduleloader.php
Go to the documentation of this file.
1
<?php
2
/*
3
* Copyright (c) 2021-2026 Bearsampp
4
* License: GNU General Public License version 3 or later; see LICENSE.txt
5
* Website: https://bearsampp.com
6
* Github: https://github.com/Bearsampp
7
*/
8
16
class
ModuleLoader
17
{
19
private
static
$modules
= array();
20
22
private
static
$loading
= array();
23
25
private
static
$maxWaitTime
= 5000;
// 5 seconds max wait
26
27
const
CORE
=
'core'
;
28
const
CONFIG
=
'config'
;
29
const
LANG
=
'lang'
;
30
const
OPENSSL
=
'openssl'
;
31
const
BINS
=
'bins'
;
32
const
TOOLS
=
'tools'
;
33
const
APPS
=
'apps'
;
34
const
WINBINDER
=
'winbinder'
;
35
const
REGISTRY
=
'registry'
;
36
const
HOMEPAGE
=
'homepage'
;
37
46
public
static
function
loadSync
($module, callable $loader)
47
{
48
if
(!isset(self::$modules[$module])) {
49
self::$loading[$module] =
true
;
50
Log::debug
(
'Loading module: '
. $module .
' (synchronous)'
);
51
call_user_func($loader);
52
unset(self::$loading[$module]);
53
}
54
}
55
64
public
static
function
loadAsync
($module, callable $loader)
65
{
66
if
(!isset(self::$modules[$module]) && !isset(self::$loading[$module])) {
67
self::$loading[$module] =
true
;
68
Log::debug
(
'Queuing module for background load: '
. $module);
69
70
// Try to use process for true async, fallback to direct call
71
$pid =
self::loadInBackground
($module, $loader);
72
if
($pid ===
false
) {
73
// Fallback: load immediately on main thread
74
Log::debug
(
'Background loading unavailable, loading on main thread: '
. $module);
75
call_user_func($loader);
76
unset(self::$loading[$module]);
77
}
78
}
79
}
80
92
private
static
function
loadInBackground
($module, callable $loader)
93
{
94
// For simplicity, we'll use direct loading since PHP doesn't have true async
95
// In a production system, you'd use Process Control (pcntl) or threading
96
// But for Windows/CLI safety, we'll just defer the actual initialization
97
call_user_func($loader);
98
unset(self::$loading[$module]);
99
100
return
true
;
101
}
102
112
public
static
function
waitForModule
($module, $timeout = 5000)
113
{
114
$startTime = microtime(
true
);
115
$maxWait = $timeout / 1000;
// Convert to seconds
116
117
while
(isset(self::$loading[$module])) {
118
if
((microtime(
true
) - $startTime) > $maxWait) {
119
Log::error
(
'Timeout waiting for module: '
. $module);
120
121
return
false
;
122
}
123
usleep(10000);
// Sleep 10ms before checking again
124
}
125
126
return
true
;
127
}
128
136
public
static
function
markLoaded
($module)
137
{
138
self::$modules[$module] =
true
;
139
unset(self::$loading[$module]);
140
}
141
149
public
static
function
isLoaded
($module)
150
{
151
return
isset(self::$modules[$module]);
152
}
153
161
public
static
function
isLoading
($module)
162
{
163
return
isset(self::$loading[$module]);
164
}
165
}
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Log\error
static error($data, $file=null)
Definition
class.log.php:760
ModuleLoader
Definition
class.moduleloader.php:17
ModuleLoader\loadInBackground
static loadInBackground($module, callable $loader)
Definition
class.moduleloader.php:92
ModuleLoader\HOMEPAGE
const HOMEPAGE
Definition
class.moduleloader.php:36
ModuleLoader\$maxWaitTime
static $maxWaitTime
Definition
class.moduleloader.php:25
ModuleLoader\$modules
static $modules
Definition
class.moduleloader.php:19
ModuleLoader\WINBINDER
const WINBINDER
Definition
class.moduleloader.php:34
ModuleLoader\markLoaded
static markLoaded($module)
Definition
class.moduleloader.php:136
ModuleLoader\$loading
static $loading
Definition
class.moduleloader.php:22
ModuleLoader\APPS
const APPS
Definition
class.moduleloader.php:33
ModuleLoader\OPENSSL
const OPENSSL
Definition
class.moduleloader.php:30
ModuleLoader\CORE
const CORE
Definition
class.moduleloader.php:27
ModuleLoader\CONFIG
const CONFIG
Definition
class.moduleloader.php:28
ModuleLoader\loadAsync
static loadAsync($module, callable $loader)
Definition
class.moduleloader.php:64
ModuleLoader\isLoading
static isLoading($module)
Definition
class.moduleloader.php:161
ModuleLoader\LANG
const LANG
Definition
class.moduleloader.php:29
ModuleLoader\REGISTRY
const REGISTRY
Definition
class.moduleloader.php:35
ModuleLoader\waitForModule
static waitForModule($module, $timeout=5000)
Definition
class.moduleloader.php:112
ModuleLoader\TOOLS
const TOOLS
Definition
class.moduleloader.php:32
ModuleLoader\BINS
const BINS
Definition
class.moduleloader.php:31
ModuleLoader\isLoaded
static isLoaded($module)
Definition
class.moduleloader.php:149
ModuleLoader\loadSync
static loadSync($module, callable $loader)
Definition
class.moduleloader.php:46
sandbox
core
classes
class.moduleloader.php
Generated by
1.17.0