Bearsampp 2026.7.11
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
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 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+)
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;
277 }
278
283 public static function loadToolsAsync()
284 {
286 global $bearsamppTools;
287 $bearsamppTools = new Tools();
288 });
289 }
290
295 public static function loadAppsAsync()
296 {
298 global $bearsamppApps;
299 $bearsamppApps = new Apps();
300 });
301 }
302
307 public static function loadRegistryAsync()
308 {
310 global $bearsamppRegistry;
311 $bearsamppRegistry = new Registry();
312 });
313 }
314
319 public static function loadHomepageAsync()
320 {
322 global $bearsamppHomepage;
324 });
325 }
326
331 public static function loadToolsFiber()
332 {
334 global $bearsamppTools;
335 $bearsamppTools = new Tools();
336 });
337 }
338
343 public static function loadAppsFiber()
344 {
346 global $bearsamppApps;
347 $bearsamppApps = new Apps();
348 });
349 }
350
355 public static function loadRegistryFiber()
356 {
358 global $bearsamppRegistry;
359 $bearsamppRegistry = new Registry();
360 });
361 }
362
367 public static function loadHomepageFiber()
368 {
370 global $bearsamppHomepage;
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 }
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:72
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:41
global $bearsamppHomepage
Definition homepage.php:41
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