Bearsampp 2026.9.23
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
16class 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)
67
68 // Initialize fiber loader (true async for 8.1+)
69 $useFibers = FiberModuleLoader::init();
70
71 // Load critical modules synchronously (required for main functionality)
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) {
86 } else {
91 }
92
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+)
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;
279 }
280
285 public static function loadToolsAsync()
286 {
288 global $bearsamppTools;
289 $bearsamppTools = new Tools();
290 });
291 }
292
297 public static function loadAppsAsync()
298 {
300 global $bearsamppApps;
301 $bearsamppApps = new Apps();
302 });
303 }
304
309 public static function loadRegistryAsync()
310 {
312 global $bearsamppRegistry;
313 $bearsamppRegistry = new Registry();
314 });
315 }
316
321 public static function loadHomepageAsync()
322 {
324 global $bearsamppHomepage;
326 });
327 }
328
333 public static function loadToolsFiber()
334 {
336 global $bearsamppTools;
337 $bearsamppTools = new Tools();
338 });
339 }
340
345 public static function loadAppsFiber()
346 {
348 global $bearsamppApps;
349 $bearsamppApps = new Apps();
350 });
351 }
352
357 public static function loadRegistryFiber()
358 {
360 global $bearsamppRegistry;
361 $bearsamppRegistry = new Registry();
362 });
363 }
364
369 public static function loadHomepageFiber()
370 {
372 global $bearsamppHomepage;
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
451{
452 return ' #' . ($match[1] - 1);
453}
global $bearsamppBins
global $bearsamppLang
global $bearsamppCore
debugStringPregReplace($match)
static init(string $cacheDir)
static waitForModule(string $module, int $timeout=5000)
static loadInFiber(string $module, callable $loader, int $timeout=5000)
static init()
Definition class.log.php:84
static separator()
static loadAsync($module, callable $loader)
static waitForModule($module, $timeout=5000)
static getRootPath($aetrayPath=false)
static init($rootPath, $corePath)
static formatUnixPath($path)
static getTmpPath($aetrayPath=false)
static getErrorLogFilePath($aetrayPath=false)
removeErrorHandling()
static debugStringBacktrace()
static loadBins()
static loadRegistryAsync()
static clearCaches()
errorHandler($errno, $errstr, $errfile, $errline)
static loadOpenSsl()
static loadLang()
static ensureModuleLoaded($module, $timeout=5000)
static loadApps()
static loadTools()
static loadRegistryFiber()
static loadWinbinder()
static loadToolsAsync()
$procsLoaded
static loadToolsFiber()
static loadHomepage()
static loadAppsAsync()
getProcs()
__construct($rootPath)
static loadCore()
static loadHomepageAsync()
static getCacheStats()
const ERROR_HANDLER
static loadAppsFiber()
static loadConfig()
static loadHomepageFiber()
static loadRegistry()
initErrorHandling()
static getListProcs()
global $bearsamppConfig
Definition homepage.php:46
global $bearsamppHomepage
Definition homepage.php:46
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