Bearsampp
2026.7.11
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-2024 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
{
18
private
static
$modules
= array();
19
private
static
$loading
= array();
20
private
static
$maxWaitTime
= 5000;
// 5 seconds max wait
21
22
const
CORE
=
'core'
;
23
const
CONFIG
=
'config'
;
24
const
LANG
=
'lang'
;
25
const
OPENSSL
=
'openssl'
;
26
const
BINS
=
'bins'
;
27
const
TOOLS
=
'tools'
;
28
const
APPS
=
'apps'
;
29
const
WINBINDER
=
'winbinder'
;
30
const
REGISTRY
=
'registry'
;
31
const
HOMEPAGE
=
'homepage'
;
32
40
public
static
function
loadSync
($module, callable $loader)
41
{
42
if
(!isset(self::$modules[$module])) {
43
self::$loading[$module] =
true
;
44
Log::debug
(
'Loading module: '
. $module .
' (synchronous)'
);
45
call_user_func($loader);
46
unset(self::$loading[$module]);
47
}
48
}
49
57
public
static
function
loadAsync
($module, callable $loader)
58
{
59
if
(!isset(self::$modules[$module]) && !isset(self::$loading[$module])) {
60
self::$loading[$module] =
true
;
61
Log::debug
(
'Queuing module for background load: '
. $module);
62
63
// Try to use process for true async, fallback to direct call
64
$pid =
self::loadInBackground
($module, $loader);
65
if
($pid ===
false
) {
66
// Fallback: load immediately on main thread
67
Log::debug
(
'Background loading unavailable, loading on main thread: '
. $module);
68
call_user_func($loader);
69
unset(self::$loading[$module]);
70
}
71
}
72
}
73
81
private
static
function
loadInBackground
($module, callable $loader)
82
{
83
// For simplicity, we'll use direct loading since PHP doesn't have true async
84
// In a production system, you'd use Process Control (pcntl) or threading
85
// But for Windows/CLI safety, we'll just defer the actual initialization
86
call_user_func($loader);
87
unset(self::$loading[$module]);
88
return
true
;
89
}
90
99
public
static
function
waitForModule
($module, $timeout = 5000)
100
{
101
$startTime = microtime(
true
);
102
$maxWait = $timeout / 1000;
// Convert to seconds
103
104
while
(isset(self::$loading[$module])) {
105
if
((microtime(
true
) - $startTime) > $maxWait) {
106
Log::error
(
'Timeout waiting for module: '
. $module);
107
return
false
;
108
}
109
usleep(10000);
// Sleep 10ms before checking again
110
}
111
112
return
true
;
113
}
114
121
public
static
function
markLoaded
($module)
122
{
123
self::$modules[$module] =
true
;
124
unset(self::$loading[$module]);
125
}
126
133
public
static
function
isLoaded
($module)
134
{
135
return
isset(self::$modules[$module]);
136
}
137
144
public
static
function
isLoading
($module)
145
{
146
return
isset(self::$loading[$module]);
147
}
148
}
Log\debug
static debug($data, $file=null)
Definition
class.log.php:616
Log\error
static error($data, $file=null)
Definition
class.log.php:650
ModuleLoader
Definition
class.moduleloader.php:17
ModuleLoader\loadInBackground
static loadInBackground($module, callable $loader)
Definition
class.moduleloader.php:81
ModuleLoader\HOMEPAGE
const HOMEPAGE
Definition
class.moduleloader.php:31
ModuleLoader\$maxWaitTime
static $maxWaitTime
Definition
class.moduleloader.php:20
ModuleLoader\$modules
static $modules
Definition
class.moduleloader.php:18
ModuleLoader\WINBINDER
const WINBINDER
Definition
class.moduleloader.php:29
ModuleLoader\markLoaded
static markLoaded($module)
Definition
class.moduleloader.php:121
ModuleLoader\$loading
static $loading
Definition
class.moduleloader.php:19
ModuleLoader\APPS
const APPS
Definition
class.moduleloader.php:28
ModuleLoader\OPENSSL
const OPENSSL
Definition
class.moduleloader.php:25
ModuleLoader\CORE
const CORE
Definition
class.moduleloader.php:22
ModuleLoader\CONFIG
const CONFIG
Definition
class.moduleloader.php:23
ModuleLoader\loadAsync
static loadAsync($module, callable $loader)
Definition
class.moduleloader.php:57
ModuleLoader\isLoading
static isLoading($module)
Definition
class.moduleloader.php:144
ModuleLoader\LANG
const LANG
Definition
class.moduleloader.php:24
ModuleLoader\REGISTRY
const REGISTRY
Definition
class.moduleloader.php:30
ModuleLoader\waitForModule
static waitForModule($module, $timeout=5000)
Definition
class.moduleloader.php:99
ModuleLoader\TOOLS
const TOOLS
Definition
class.moduleloader.php:27
ModuleLoader\BINS
const BINS
Definition
class.moduleloader.php:26
ModuleLoader\isLoaded
static isLoaded($module)
Definition
class.moduleloader.php:133
ModuleLoader\loadSync
static loadSync($module, callable $loader)
Definition
class.moduleloader.php:40
sandbox
core
classes
class.moduleloader.php
Generated by
1.17.0