Bearsampp 2026.7.11
Loading...
Searching...
No Matches
class.moduleloader.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 * Website: https://bearsampp.com
6 * Github: https://github.com/Bearsampp
7 */
8
17{
18 private static $modules = array();
19 private static $loading = array();
20 private static $maxWaitTime = 5000; // 5 seconds max wait
21
22 const CORE = 'core';
23 const CONFIG = 'config';
24 const LANG = 'lang';
25 const OPENSSL = 'openssl';
26 const BINS = 'bins';
27 const TOOLS = 'tools';
28 const APPS = 'apps';
29 const WINBINDER = 'winbinder';
30 const REGISTRY = 'registry';
31 const HOMEPAGE = 'homepage';
32
40 public static function loadSync($module, callable $loader)
41 {
42 if (!isset(self::$modules[$module])) {
43 self::$loading[$module] = true;
44 Log::debug('Loading module: ' . $module . ' (synchronous)');
45 call_user_func($loader);
46 unset(self::$loading[$module]);
47 }
48 }
49
57 public static function loadAsync($module, callable $loader)
58 {
59 if (!isset(self::$modules[$module]) && !isset(self::$loading[$module])) {
60 self::$loading[$module] = true;
61 Log::debug('Queuing module for background load: ' . $module);
62
63 // Try to use process for true async, fallback to direct call
64 $pid = self::loadInBackground($module, $loader);
65 if ($pid === false) {
66 // Fallback: load immediately on main thread
67 Log::debug('Background loading unavailable, loading on main thread: ' . $module);
68 call_user_func($loader);
69 unset(self::$loading[$module]);
70 }
71 }
72 }
73
81 private static function loadInBackground($module, callable $loader)
82 {
83 // For simplicity, we'll use direct loading since PHP doesn't have true async
84 // In a production system, you'd use Process Control (pcntl) or threading
85 // But for Windows/CLI safety, we'll just defer the actual initialization
86 call_user_func($loader);
87 unset(self::$loading[$module]);
88 return true;
89 }
90
99 public static function waitForModule($module, $timeout = 5000)
100 {
101 $startTime = microtime(true);
102 $maxWait = $timeout / 1000; // Convert to seconds
103
104 while (isset(self::$loading[$module])) {
105 if ((microtime(true) - $startTime) > $maxWait) {
106 Log::error('Timeout waiting for module: ' . $module);
107 return false;
108 }
109 usleep(10000); // Sleep 10ms before checking again
110 }
111
112 return true;
113 }
114
121 public static function markLoaded($module)
122 {
123 self::$modules[$module] = true;
124 unset(self::$loading[$module]);
125 }
126
133 public static function isLoaded($module)
134 {
135 return isset(self::$modules[$module]);
136 }
137
144 public static function isLoading($module)
145 {
146 return isset(self::$loading[$module]);
147 }
148}
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)