Bearsampp
2026.9.23
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-2026 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
131
return
$this->procs
;
132
}
133
144
public
static
function
ensureModuleLoaded
($module, $timeout = 5000)
145
{
146
// Try Fiber loader first (if available on PHP 8.1+)
147
if
(
FiberModuleLoader::isAvailable
()) {
148
return
FiberModuleLoader::waitForModule
($module, $timeout);
149
}
150
151
// Fallback to deferred loading
152
return
ModuleLoader::waitForModule
($module, $timeout);
153
}
154
161
public
static
function
getCacheStats
(): array
162
{
163
return
CacheManager::getStats
();
164
}
165
172
public
static
function
clearCaches
(): int
173
{
174
return
CacheManager::clearAll
();
175
}
176
182
public
function
isRoot
()
183
{
184
return
$this->isRoot
;
185
}
186
190
public
static
function
loadCore
()
191
{
192
global
$bearsamppCore
;
193
$bearsamppCore
=
new
Core
();
194
}
195
199
public
static
function
loadConfig
()
200
{
201
global
$bearsamppConfig
;
202
$bearsamppConfig
=
new
Config
();
203
}
204
208
public
static
function
loadLang
()
209
{
210
global
$bearsamppLang
;
211
$bearsamppLang
=
new
LangProc
();
212
}
213
217
public
static
function
loadOpenSsl
()
218
{
219
global $bearsamppOpenSsl;
220
$bearsamppOpenSsl =
new
OpenSsl
();
221
}
222
226
public
static
function
loadBins
()
227
{
228
global
$bearsamppBins
;
229
$bearsamppBins
=
new
Bins
();
230
}
231
235
public
static
function
loadTools
()
236
{
237
global $bearsamppTools;
238
if
(!isset($bearsamppTools)) {
239
$bearsamppTools =
new
Tools
();
240
}
241
}
242
246
public
static
function
loadApps
()
247
{
248
global $bearsamppApps;
249
$bearsamppApps =
new
Apps
();
250
}
251
255
public
static
function
loadWinbinder
()
256
{
257
global $bearsamppWinbinder;
258
if
(extension_loaded(
'winbinder'
)) {
259
$bearsamppWinbinder =
new
WinBinder
();
260
}
261
}
262
266
public
static
function
loadRegistry
()
267
{
268
global $bearsamppRegistry;
269
$bearsamppRegistry =
new
Registry
();
270
}
271
275
public
static
function
loadHomepage
()
276
{
277
global
$bearsamppHomepage
;
278
$bearsamppHomepage
=
new
Homepage
();
279
}
280
285
public
static
function
loadToolsAsync
()
286
{
287
ModuleLoader::loadAsync
(
ModuleLoader::TOOLS
,
function
() {
288
global $bearsamppTools;
289
$bearsamppTools =
new
Tools
();
290
});
291
}
292
297
public
static
function
loadAppsAsync
()
298
{
299
ModuleLoader::loadAsync
(
ModuleLoader::APPS
,
function
() {
300
global $bearsamppApps;
301
$bearsamppApps =
new
Apps
();
302
});
303
}
304
309
public
static
function
loadRegistryAsync
()
310
{
311
ModuleLoader::loadAsync
(
ModuleLoader::REGISTRY
,
function
() {
312
global $bearsamppRegistry;
313
$bearsamppRegistry =
new
Registry
();
314
});
315
}
316
321
public
static
function
loadHomepageAsync
()
322
{
323
ModuleLoader::loadAsync
(
ModuleLoader::HOMEPAGE
,
function
() {
324
global
$bearsamppHomepage
;
325
$bearsamppHomepage
=
new
Homepage
();
326
});
327
}
328
333
public
static
function
loadToolsFiber
()
334
{
335
FiberModuleLoader::loadInFiber
(
ModuleLoader::TOOLS
,
function
() {
336
global $bearsamppTools;
337
$bearsamppTools =
new
Tools
();
338
});
339
}
340
345
public
static
function
loadAppsFiber
()
346
{
347
FiberModuleLoader::loadInFiber
(
ModuleLoader::APPS
,
function
() {
348
global $bearsamppApps;
349
$bearsamppApps =
new
Apps
();
350
});
351
}
352
357
public
static
function
loadRegistryFiber
()
358
{
359
FiberModuleLoader::loadInFiber
(
ModuleLoader::REGISTRY
,
function
() {
360
global $bearsamppRegistry;
361
$bearsamppRegistry =
new
Registry
();
362
});
363
}
364
369
public
static
function
loadHomepageFiber
()
370
{
371
FiberModuleLoader::loadInFiber
(
ModuleLoader::HOMEPAGE
,
function
() {
372
global
$bearsamppHomepage
;
373
$bearsamppHomepage
=
new
Homepage
();
374
});
375
}
376
385
public
function
errorHandler
($errno, $errstr, $errfile, $errline)
386
{
387
if
(!(error_reporting() & $errno)) {
388
return
;
389
}
390
391
$errfile =
Path::formatUnixPath
($errfile);
392
$errfile = str_replace(
Path::getRootPath
(),
''
, $errfile);
393
394
if
(!defined(
'E_DEPRECATED'
)) {
395
define(
'E_DEPRECATED'
, 8192);
396
}
397
398
$errNames = array(
399
E_ERROR =>
'E_ERROR'
,
400
E_WARNING =>
'E_WARNING'
,
401
E_PARSE =>
'E_PARSE'
,
402
E_NOTICE =>
'E_NOTICE'
,
403
E_CORE_ERROR =>
'E_CORE_ERROR'
,
404
E_CORE_WARNING =>
'E_CORE_WARNING'
,
405
E_COMPILE_ERROR =>
'E_COMPILE_ERROR'
,
406
E_COMPILE_WARNING =>
'E_COMPILE_WARNING'
,
407
E_USER_ERROR
=>
'E_USER_ERROR'
,
408
E_USER_WARNING =>
'E_USER_WARNING'
,
409
E_USER_NOTICE =>
'E_USER_NOTICE'
,
410
E_RECOVERABLE_ERROR =>
'E_RECOVERABLE_ERROR'
,
411
E_DEPRECATED =>
'E_DEPRECATED'
,
412
);
413
414
$content =
'['
. date(
'Y-m-d H:i:s'
, time()) .
'] '
;
415
$content .= $errNames[$errno] .
' '
;
416
$content .= $errstr .
' in '
. $errfile;
417
$content .=
' on line '
. $errline . PHP_EOL;
418
$content .=
self::debugStringBacktrace
() . PHP_EOL;
419
420
@file_put_contents(
Path::getErrorLogFilePath
(), $content, FILE_APPEND);
421
}
422
428
private
static
function
debugStringBacktrace
()
429
{
430
ob_start();
431
debug_print_backtrace();
432
$trace = ob_get_contents();
433
ob_end_clean();
434
435
$trace = preg_replace(
'/^#0\s+Root::debugStringBacktrace[^\n]*\n/'
,
''
, $trace, 1);
436
$trace = preg_replace(
'/^#1\s+isRoot->errorHandler[^\n]*\n/'
,
''
, $trace, 1);
437
$trace = preg_replace_callback(
'/^#(\d+)/m'
,
'debugStringPregReplace'
, $trace);
438
439
return
$trace;
440
}
441
}
442
450
function
debugStringPregReplace
($match)
451
{
452
return
' #'
. ($match[1] - 1);
453
}
$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:450
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:230
CacheManager\clearAll
static clearAll()
Definition
class.cachemanager.php:193
CacheManager\init
static init(string $cacheDir)
Definition
class.cachemanager.php:42
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:80
FiberModuleLoader\loadInFiber
static loadInFiber(string $module, callable $loader, int $timeout=5000)
Definition
class.fibermoduleloader.php:52
FiberModuleLoader\isAvailable
static isAvailable()
Definition
class.fibermoduleloader.php:169
FiberModuleLoader\init
static init()
Definition
class.fibermoduleloader.php:31
Homepage
Definition
class.homepage.php:18
LangProc
Definition
class.langproc.php:17
Log\init
static init()
Definition
class.log.php:84
Log\separator
static separator()
Definition
class.log.php:678
ModuleLoader\HOMEPAGE
const HOMEPAGE
Definition
class.moduleloader.php:36
ModuleLoader\APPS
const APPS
Definition
class.moduleloader.php:33
ModuleLoader\loadAsync
static loadAsync($module, callable $loader)
Definition
class.moduleloader.php:64
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
OpenSsl
Definition
class.openssl.php:19
Path\getRootPath
static getRootPath($aetrayPath=false)
Definition
class.path.php:324
Path\init
static init($rootPath, $corePath)
Definition
class.path.php:91
Path\formatUnixPath
static formatUnixPath($path)
Definition
class.path.php:166
Path\getTmpPath
static getTmpPath($aetrayPath=false)
Definition
class.path.php:1110
Path\getErrorLogFilePath
static getErrorLogFilePath($aetrayPath=false)
Definition
class.path.php:463
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:428
Root\loadBins
static loadBins()
Definition
class.root.php:226
Root\loadRegistryAsync
static loadRegistryAsync()
Definition
class.root.php:309
Root\clearCaches
static clearCaches()
Definition
class.root.php:172
Root\errorHandler
errorHandler($errno, $errstr, $errfile, $errline)
Definition
class.root.php:385
Root\loadOpenSsl
static loadOpenSsl()
Definition
class.root.php:217
Root\loadLang
static loadLang()
Definition
class.root.php:208
Root\ensureModuleLoaded
static ensureModuleLoaded($module, $timeout=5000)
Definition
class.root.php:144
Root\loadApps
static loadApps()
Definition
class.root.php:246
Root\loadTools
static loadTools()
Definition
class.root.php:235
Root\loadRegistryFiber
static loadRegistryFiber()
Definition
class.root.php:357
Root\isRoot
isRoot()
Definition
class.root.php:182
Root\loadWinbinder
static loadWinbinder()
Definition
class.root.php:255
Root\loadToolsAsync
static loadToolsAsync()
Definition
class.root.php:285
Root\$procsLoaded
$procsLoaded
Definition
class.root.php:22
Root\loadToolsFiber
static loadToolsFiber()
Definition
class.root.php:333
Root\loadHomepage
static loadHomepage()
Definition
class.root.php:275
Root\loadAppsAsync
static loadAppsAsync()
Definition
class.root.php:297
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:190
Root\loadHomepageAsync
static loadHomepageAsync()
Definition
class.root.php:321
Root\getCacheStats
static getCacheStats()
Definition
class.root.php:161
Root\ERROR_HANDLER
const ERROR_HANDLER
Definition
class.root.php:18
Root\loadAppsFiber
static loadAppsFiber()
Definition
class.root.php:345
Root\loadConfig
static loadConfig()
Definition
class.root.php:199
Root\loadHomepageFiber
static loadHomepageFiber()
Definition
class.root.php:369
Root\loadRegistry
static loadRegistry()
Definition
class.root.php:266
Root\initErrorHandling
initErrorHandling()
Definition
class.root.php:99
Tools
Definition
class.tools.php:18
Win32Ps\getListProcs
static getListProcs()
Definition
class.win32ps.php:84
WinBinder
Definition
class.winbinder.php:19
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:46
$bearsamppHomepage
global $bearsamppHomepage
Definition
homepage.php:46
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