Bearsampp
2026.7.11
Toggle main menu visibility
Loading...
Searching...
No Matches
class.root.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
* Author: Bear
6
* Website: https://bearsampp.com
7
* Github: https://github.com/Bearsampp
8
*/
9
16
class
Root
17
{
18
const
ERROR_HANDLER
=
'errorHandler'
;
19
20
public
$path
;
21
private
$procs
;
22
private
$procsLoaded
=
false
;
23
private
$isRoot
;
24
30
public
function
__construct
($rootPath)
31
{
32
$this->path = str_replace(
'\\'
,
'/'
, rtrim($rootPath,
'/\\'
));
33
$this->
isRoot
= $_SERVER[
'PHP_SELF'
] ==
'root.php'
;
34
Path::init
(dirname($this->path), $this->path);
35
}
36
40
public
function
register
()
41
{
42
// Params
43
set_time_limit(0);
44
clearstatcache();
45
46
// Error log
47
$this->
initErrorHandling
();
48
49
// External classes
50
require_once $this->path .
'/classes/class.log.php'
;
51
require_once $this->path .
'/classes/class.util.php'
;
52
require_once $this->path .
'/classes/class.util.string.php'
;
53
Log::init
();
54
55
// Autoloader
56
require_once $this->path .
'/classes/class.autoloader.php'
;
57
$bearsamppAutoloader =
new
Autoloader
();
58
$bearsamppAutoloader->register();
59
60
// Module loaders for async initialization
61
require_once $this->path .
'/classes/class.moduleloader.php'
;
62
require_once $this->path .
'/classes/class.cachemanager.php'
;
63
require_once $this->path .
'/classes/class.fibermoduleloader.php'
;
64
65
// Initialize cache system (disk caching for warm starts)
66
CacheManager::init
(
Path::getTmpPath
() .
'/cache'
);
67
68
// Initialize fiber loader (true async for 8.1+)
69
$useFibers =
FiberModuleLoader::init
();
70
71
// Load critical modules synchronously (required for main functionality)
72
self::loadCore
();
73
self::loadConfig
();
74
self::loadLang
();
75
self::loadOpenSsl
();
76
self::loadBins
();
77
Log::separator
();
78
79
// Load non-critical modules asynchronously (for UI/admin functions)
80
// Uses Fibers if PHP 8.1+, fallback to deferred loading
81
if
($useFibers) {
82
self::loadToolsFiber
();
83
self::loadAppsFiber
();
84
self::loadRegistryFiber
();
85
self::loadHomepageFiber
();
86
}
else
{
87
self::loadToolsAsync
();
88
self::loadAppsAsync
();
89
self::loadRegistryAsync
();
90
self::loadHomepageAsync
();
91
}
92
93
self::loadWinbinder
();
94
}
95
99
public
function
initErrorHandling
()
100
{
101
error_reporting(-1);
102
ini_set(
'error_log'
,
Path::getErrorLogFilePath
());
103
ini_set(
'display_errors'
,
'1'
);
104
set_error_handler(array($this, self::ERROR_HANDLER));
105
}
106
110
public
function
removeErrorHandling
()
111
{
112
error_reporting(0);
113
ini_set(
'error_log'
,
null
);
114
ini_set(
'display_errors'
,
'0'
);
115
restore_error_handler();
116
}
117
124
public
function
getProcs
()
125
{
126
if
(!$this->procsLoaded && $this->
isRoot
()) {
127
$this->procs =
Win32Ps::getListProcs
();
128
$this->procsLoaded =
true
;
129
}
130
return
$this->procs
;
131
}
132
142
public
static
function
ensureModuleLoaded
($module, $timeout = 5000)
143
{
144
// Try Fiber loader first (if available on PHP 8.1+)
145
if
(
FiberModuleLoader::isAvailable
()) {
146
return
FiberModuleLoader::waitForModule
($module, $timeout);
147
}
148
149
// Fallback to deferred loading
150
return
ModuleLoader::waitForModule
($module, $timeout);
151
}
152
159
public
static
function
getCacheStats
(): array
160
{
161
return
CacheManager::getStats
();
162
}
163
170
public
static
function
clearCaches
(): int
171
{
172
return
CacheManager::clearAll
();
173
}
174
180
public
function
isRoot
()
181
{
182
return
$this->isRoot
;
183
}
184
188
public
static
function
loadCore
()
189
{
190
global
$bearsamppCore
;
191
$bearsamppCore
=
new
Core
();
192
}
193
197
public
static
function
loadConfig
()
198
{
199
global
$bearsamppConfig
;
200
$bearsamppConfig
=
new
Config
();
201
}
202
206
public
static
function
loadLang
()
207
{
208
global
$bearsamppLang
;
209
$bearsamppLang
=
new
LangProc
();
210
}
211
215
public
static
function
loadOpenSsl
()
216
{
217
global $bearsamppOpenSsl;
218
$bearsamppOpenSsl =
new
OpenSsl
();
219
}
220
224
public
static
function
loadBins
()
225
{
226
global
$bearsamppBins
;
227
$bearsamppBins
=
new
Bins
();
228
}
229
233
public
static
function
loadTools
()
234
{
235
global $bearsamppTools;
236
if
(!isset($bearsamppTools)) {
237
$bearsamppTools =
new
Tools
();
238
}
239
}
240
244
public
static
function
loadApps
()
245
{
246
global $bearsamppApps;
247
$bearsamppApps =
new
Apps
();
248
}
249
253
public
static
function
loadWinbinder
()
254
{
255
global $bearsamppWinbinder;
256
if
(extension_loaded(
'winbinder'
)) {
257
$bearsamppWinbinder =
new
WinBinder
();
258
}
259
}
260
264
public
static
function
loadRegistry
()
265
{
266
global $bearsamppRegistry;
267
$bearsamppRegistry =
new
Registry
();
268
}
269
273
public
static
function
loadHomepage
()
274
{
275
global
$bearsamppHomepage
;
276
$bearsamppHomepage
=
new
Homepage
();
277
}
278
283
public
static
function
loadToolsAsync
()
284
{
285
ModuleLoader::loadAsync
(
ModuleLoader::TOOLS
,
function
() {
286
global $bearsamppTools;
287
$bearsamppTools =
new
Tools
();
288
});
289
}
290
295
public
static
function
loadAppsAsync
()
296
{
297
ModuleLoader::loadAsync
(
ModuleLoader::APPS
,
function
() {
298
global $bearsamppApps;
299
$bearsamppApps =
new
Apps
();
300
});
301
}
302
307
public
static
function
loadRegistryAsync
()
308
{
309
ModuleLoader::loadAsync
(
ModuleLoader::REGISTRY
,
function
() {
310
global $bearsamppRegistry;
311
$bearsamppRegistry =
new
Registry
();
312
});
313
}
314
319
public
static
function
loadHomepageAsync
()
320
{
321
ModuleLoader::loadAsync
(
ModuleLoader::HOMEPAGE
,
function
() {
322
global
$bearsamppHomepage
;
323
$bearsamppHomepage
=
new
Homepage
();
324
});
325
}
326
331
public
static
function
loadToolsFiber
()
332
{
333
FiberModuleLoader::loadInFiber
(
ModuleLoader::TOOLS
,
function
() {
334
global $bearsamppTools;
335
$bearsamppTools =
new
Tools
();
336
});
337
}
338
343
public
static
function
loadAppsFiber
()
344
{
345
FiberModuleLoader::loadInFiber
(
ModuleLoader::APPS
,
function
() {
346
global $bearsamppApps;
347
$bearsamppApps =
new
Apps
();
348
});
349
}
350
355
public
static
function
loadRegistryFiber
()
356
{
357
FiberModuleLoader::loadInFiber
(
ModuleLoader::REGISTRY
,
function
() {
358
global $bearsamppRegistry;
359
$bearsamppRegistry =
new
Registry
();
360
});
361
}
362
367
public
static
function
loadHomepageFiber
()
368
{
369
FiberModuleLoader::loadInFiber
(
ModuleLoader::HOMEPAGE
,
function
() {
370
global
$bearsamppHomepage
;
371
$bearsamppHomepage
=
new
Homepage
();
372
});
373
}
374
383
public
function
errorHandler
($errno, $errstr, $errfile, $errline)
384
{
385
if
(!(error_reporting() & $errno)) {
386
return
;
387
}
388
389
$errfile =
Path::formatUnixPath
($errfile);
390
$errfile = str_replace(
Path::getRootPath
(),
''
, $errfile);
391
392
if
(!defined(
'E_DEPRECATED'
)) {
393
define(
'E_DEPRECATED'
, 8192);
394
}
395
396
$errNames = array(
397
E_ERROR =>
'E_ERROR'
,
398
E_WARNING =>
'E_WARNING'
,
399
E_PARSE =>
'E_PARSE'
,
400
E_NOTICE =>
'E_NOTICE'
,
401
E_CORE_ERROR =>
'E_CORE_ERROR'
,
402
E_CORE_WARNING =>
'E_CORE_WARNING'
,
403
E_COMPILE_ERROR =>
'E_COMPILE_ERROR'
,
404
E_COMPILE_WARNING =>
'E_COMPILE_WARNING'
,
405
E_USER_ERROR
=>
'E_USER_ERROR'
,
406
E_USER_WARNING =>
'E_USER_WARNING'
,
407
E_USER_NOTICE =>
'E_USER_NOTICE'
,
408
E_RECOVERABLE_ERROR =>
'E_RECOVERABLE_ERROR'
,
409
E_DEPRECATED =>
'E_DEPRECATED'
,
410
);
411
412
$content =
'['
. date(
'Y-m-d H:i:s'
, time()) .
'] '
;
413
$content .= $errNames[$errno] .
' '
;
414
$content .= $errstr .
' in '
. $errfile;
415
$content .=
' on line '
. $errline . PHP_EOL;
416
$content .=
self::debugStringBacktrace
() . PHP_EOL;
417
418
file_put_contents(
Path::getErrorLogFilePath
(), $content, FILE_APPEND);
419
}
420
426
private
static
function
debugStringBacktrace
()
427
{
428
ob_start();
429
debug_print_backtrace();
430
$trace = ob_get_contents();
431
ob_end_clean();
432
433
$trace = preg_replace(
'/^#0\s+Root::debugStringBacktrace[^\n]*\n/'
,
''
, $trace, 1);
434
$trace = preg_replace(
'/^#1\s+isRoot->errorHandler[^\n]*\n/'
,
''
, $trace, 1);
435
$trace = preg_replace_callback(
'/^#(\d+)/m'
,
'debugStringPregReplace'
, $trace);
436
return
$trace;
437
}
438
}
439
446
function
debugStringPregReplace
($match)
447
{
448
return
' #'
. ($match[1] - 1);
449
}
$bearsamppBins
global $bearsamppBins
Definition
ajax.apache.php:16
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppCore
global $bearsamppCore
Definition
ajax.latestversion.php:24
debugStringPregReplace
debugStringPregReplace($match)
Definition
class.root.php:446
Apps
Definition
class.apps.php:17
Autoloader
Definition
class.autoloader.php:22
Bins
Definition
class.bins.php:17
CacheManager\getStats
static getStats()
Definition
class.cachemanager.php:216
CacheManager\clearAll
static clearAll()
Definition
class.cachemanager.php:179
CacheManager\init
static init(string $cacheDir)
Definition
class.cachemanager.php:36
Config
Definition
class.config.php:18
Core
Definition
class.core.php:19
FiberModuleLoader\waitForModule
static waitForModule(string $module, int $timeout=5000)
Definition
class.fibermoduleloader.php:74
FiberModuleLoader\loadInFiber
static loadInFiber(string $module, callable $loader, int $timeout=5000)
Definition
class.fibermoduleloader.php:48
FiberModuleLoader\isAvailable
static isAvailable()
Definition
class.fibermoduleloader.php:158
FiberModuleLoader\init
static init()
Definition
class.fibermoduleloader.php:28
Homepage
Definition
class.homepage.php:18
LangProc
Definition
class.langproc.php:17
Log\init
static init()
Definition
class.log.php:72
Log\separator
static separator()
Definition
class.log.php:573
ModuleLoader\HOMEPAGE
const HOMEPAGE
Definition
class.moduleloader.php:31
ModuleLoader\APPS
const APPS
Definition
class.moduleloader.php:28
ModuleLoader\loadAsync
static loadAsync($module, callable $loader)
Definition
class.moduleloader.php:57
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
OpenSsl
Definition
class.openssl.php:12
Path\getRootPath
static getRootPath($aetrayPath=false)
Definition
class.path.php:309
Path\init
static init($rootPath, $corePath)
Definition
class.path.php:84
Path\formatUnixPath
static formatUnixPath($path)
Definition
class.path.php:156
Path\getTmpPath
static getTmpPath($aetrayPath=false)
Definition
class.path.php:940
Path\getErrorLogFilePath
static getErrorLogFilePath($aetrayPath=false)
Definition
class.path.php:439
Registry
Definition
class.registry.php:18
Root
Definition
class.root.php:17
Root\removeErrorHandling
removeErrorHandling()
Definition
class.root.php:110
Root\$path
$path
Definition
class.root.php:20
Root\debugStringBacktrace
static debugStringBacktrace()
Definition
class.root.php:426
Root\loadBins
static loadBins()
Definition
class.root.php:224
Root\loadRegistryAsync
static loadRegistryAsync()
Definition
class.root.php:307
Root\clearCaches
static clearCaches()
Definition
class.root.php:170
Root\errorHandler
errorHandler($errno, $errstr, $errfile, $errline)
Definition
class.root.php:383
Root\loadOpenSsl
static loadOpenSsl()
Definition
class.root.php:215
Root\loadLang
static loadLang()
Definition
class.root.php:206
Root\ensureModuleLoaded
static ensureModuleLoaded($module, $timeout=5000)
Definition
class.root.php:142
Root\loadApps
static loadApps()
Definition
class.root.php:244
Root\loadTools
static loadTools()
Definition
class.root.php:233
Root\loadRegistryFiber
static loadRegistryFiber()
Definition
class.root.php:355
Root\isRoot
isRoot()
Definition
class.root.php:180
Root\loadWinbinder
static loadWinbinder()
Definition
class.root.php:253
Root\loadToolsAsync
static loadToolsAsync()
Definition
class.root.php:283
Root\$procsLoaded
$procsLoaded
Definition
class.root.php:22
Root\loadToolsFiber
static loadToolsFiber()
Definition
class.root.php:331
Root\loadHomepage
static loadHomepage()
Definition
class.root.php:273
Root\loadAppsAsync
static loadAppsAsync()
Definition
class.root.php:295
Root\getProcs
getProcs()
Definition
class.root.php:124
Root\$isRoot
$isRoot
Definition
class.root.php:23
Root\$procs
$procs
Definition
class.root.php:21
Root\__construct
__construct($rootPath)
Definition
class.root.php:30
Root\loadCore
static loadCore()
Definition
class.root.php:188
Root\loadHomepageAsync
static loadHomepageAsync()
Definition
class.root.php:319
Root\getCacheStats
static getCacheStats()
Definition
class.root.php:159
Root\ERROR_HANDLER
const ERROR_HANDLER
Definition
class.root.php:18
Root\loadAppsFiber
static loadAppsFiber()
Definition
class.root.php:343
Root\loadConfig
static loadConfig()
Definition
class.root.php:197
Root\loadHomepageFiber
static loadHomepageFiber()
Definition
class.root.php:367
Root\loadRegistry
static loadRegistry()
Definition
class.root.php:264
Root\initErrorHandling
initErrorHandling()
Definition
class.root.php:99
Tools
Definition
class.tools.php:18
Win32Ps\getListProcs
static getListProcs()
Definition
class.win32ps.php:79
WinBinder
Definition
class.winbinder.php:19
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:41
$bearsamppHomepage
global $bearsamppHomepage
Definition
homepage.php:41
E_USER_ERROR
if(!extension_loaded('winbinder')) if(!dl('php_winbinder.dll')) trigger_error("WinBinder extension could not be loaded.\n" E_USER_ERROR
Definition
winbinder.php:14
sandbox
core
classes
class.root.php
Generated by
1.17.0