Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.moduleloader.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 * Website: https://bearsampp.com
6 * Github: https://github.com/Bearsampp
7 */
8
17{
19 private static $modules = array();
20
22 private static $loading = array();
23
25 private static $maxWaitTime = 5000; // 5 seconds max wait
26
27 const CORE = 'core';
28 const CONFIG = 'config';
29 const LANG = 'lang';
30 const OPENSSL = 'openssl';
31 const BINS = 'bins';
32 const TOOLS = 'tools';
33 const APPS = 'apps';
34 const WINBINDER = 'winbinder';
35 const REGISTRY = 'registry';
36 const HOMEPAGE = 'homepage';
37
46 public static function loadSync($module, callable $loader)
47 {
48 if (!isset(self::$modules[$module])) {
49 self::$loading[$module] = true;
50 Log::debug('Loading module: ' . $module . ' (synchronous)');
51 call_user_func($loader);
52 unset(self::$loading[$module]);
53 }
54 }
55
64 public static function loadAsync($module, callable $loader)
65 {
66 if (!isset(self::$modules[$module]) && !isset(self::$loading[$module])) {
67 self::$loading[$module] = true;
68 Log::debug('Queuing module for background load: ' . $module);
69
70 // Try to use process for true async, fallback to direct call
71 $pid = self::loadInBackground($module, $loader);
72 if ($pid === false) {
73 // Fallback: load immediately on main thread
74 Log::debug('Background loading unavailable, loading on main thread: ' . $module);
75 call_user_func($loader);
76 unset(self::$loading[$module]);
77 }
78 }
79 }
80
92 private static function loadInBackground($module, callable $loader)
93 {
94 // For simplicity, we'll use direct loading since PHP doesn't have true async
95 // In a production system, you'd use Process Control (pcntl) or threading
96 // But for Windows/CLI safety, we'll just defer the actual initialization
97 call_user_func($loader);
98 unset(self::$loading[$module]);
99
100 return true;
101 }
102
112 public static function waitForModule($module, $timeout = 5000)
113 {
114 $startTime = microtime(true);
115 $maxWait = $timeout / 1000; // Convert to seconds
116
117 while (isset(self::$loading[$module])) {
118 if ((microtime(true) - $startTime) > $maxWait) {
119 Log::error('Timeout waiting for module: ' . $module);
120
121 return false;
122 }
123 usleep(10000); // Sleep 10ms before checking again
124 }
125
126 return true;
127 }
128
136 public static function markLoaded($module)
137 {
138 self::$modules[$module] = true;
139 unset(self::$loading[$module]);
140 }
141
149 public static function isLoaded($module)
150 {
151 return isset(self::$modules[$module]);
152 }
153
161 public static function isLoading($module)
162 {
163 return isset(self::$loading[$module]);
164 }
165}
static debug($data, $file=null)
static error($data, $file=null)
static loadInBackground($module, callable $loader)
static markLoaded($module)
static loadAsync($module, callable $loader)
static isLoading($module)
static waitForModule($module, $timeout=5000)
static isLoaded($module)
static loadSync($module, callable $loader)