Bearsampp
2026.9.23
Toggle main menu visibility
Loading...
Searching...
No Matches
class.autoloader.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
21
class
Autoloader
22
{
28
private
static
$classMap
= [];
29
35
private
static
$failedLookups
= [];
36
42
private
static
$stats
= [
43
'hits'
=> 0,
44
'misses'
=> 0,
45
'failed_hits'
=> 0
46
];
47
53
public
function
__construct
()
54
{
55
}
56
65
public
function
load
($class)
66
{
67
global
$bearsamppRoot
;
68
69
$originalClass = $class;
70
$class = strtolower($class);
71
72
// Check if we've already successfully loaded this class
73
if
(isset(self::$classMap[$class])) {
74
self::$stats[
'hits'
]++;
75
require_once self::$classMap[$class];
76
77
return
true
;
78
}
79
80
// Check if we've already determined this class doesn't exist
81
if
(isset(self::$failedLookups[$class])) {
82
self::$stats[
'failed_hits'
]++;
83
84
return
false
;
85
}
86
87
self::$stats[
'misses'
]++;
88
89
$rootPath =
Path::getCorePath
();
90
$file = $this->
resolveClassPath
($class, $rootPath);
91
92
if
(!file_exists($file)) {
93
// Cache the failed lookup
94
self::$failedLookups[$class] =
true
;
95
96
return
false
;
97
}
98
99
// Cache the successful path resolution
100
self::$classMap[$class] = $file;
101
require_once $file;
102
103
return
true
;
104
}
105
115
private
function
resolveClassPath
($class, $rootPath)
116
{
117
$file = $rootPath .
'/classes/class.'
. $class .
'.php'
;
118
119
if
(in_array($class, [
'utilstring'
,
'utilinput'
],
true
)) {
120
$class = substr_replace($class,
'.'
, 4, 0);
121
$file = $rootPath .
'/classes/class.'
. $class .
'.php'
;
122
} elseif ($class ===
'path'
) {
123
$file = $rootPath .
'/classes/class.path.php'
;
124
} elseif (
UtilString::startWith
($class,
'bin'
)) {
125
$class = $class !=
'bins'
? substr_replace($class,
'.'
, 3, 0) : $class;
126
$file = $rootPath .
'/classes/bins/class.'
. $class .
'.php'
;
127
} elseif (
UtilString::startWith
($class,
'tool'
)) {
128
$class = $class !=
'tools'
? substr_replace($class,
'.'
, 4, 0) : $class;
129
$file = $rootPath .
'/classes/tools/class.'
. $class .
'.php'
;
130
} elseif (
UtilString::startWith
($class,
'app'
)) {
131
$class = $class !=
'apps'
? substr_replace($class,
'.'
, 3, 0) : $class;
132
$file = $rootPath .
'/classes/apps/class.'
. $class .
'.php'
;
133
} elseif (
UtilString::startWith
($class,
'action'
)) {
134
$class = $class !=
'action'
? substr_replace($class,
'.'
, 6, 0) : $class;
135
$file = $rootPath .
'/classes/actions/class.'
. $class .
'.php'
;
136
} elseif (
UtilString::startWith
($class,
'tplapp'
) && $class !=
'tplapp'
) {
137
$class = substr_replace(substr_replace($class,
'.'
, 3, 0),
'.'
, 7, 0);
138
$file = $rootPath .
'/classes/tpls/app/class.'
. $class .
'.php'
;
139
} elseif (
UtilString::startWith
($class,
'tpl'
)) {
140
$class = $class !=
'tpls'
? substr_replace($class,
'.'
, 3, 0) : $class;
141
$file = $rootPath .
'/classes/tpls/class.'
. $class .
'.php'
;
142
}
143
144
return
$file;
145
}
146
153
public
static
function
getStats
()
154
{
155
return
self::$stats;
156
}
157
164
public
static
function
clearCache
()
165
{
166
self::$classMap = [];
167
self::$failedLookups = [];
168
self::$stats = [
169
'hits'
=> 0,
170
'misses'
=> 0,
171
'failed_hits'
=> 0
172
];
173
}
174
180
public
static
function
getCacheSize
()
181
{
182
return
count(self::$classMap);
183
}
184
190
public
function
register
()
191
{
192
return
spl_autoload_register(array($this,
'load'
));
193
}
194
200
public
function
unregister
()
201
{
202
return
spl_autoload_unregister(array($this,
'load'
));
203
}
204
}
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
Autoloader
Definition
class.autoloader.php:22
Autoloader\__construct
__construct()
Definition
class.autoloader.php:53
Autoloader\unregister
unregister()
Definition
class.autoloader.php:200
Autoloader\getStats
static getStats()
Definition
class.autoloader.php:153
Autoloader\resolveClassPath
resolveClassPath($class, $rootPath)
Definition
class.autoloader.php:115
Autoloader\$stats
static $stats
Definition
class.autoloader.php:42
Autoloader\$classMap
static $classMap
Definition
class.autoloader.php:28
Autoloader\getCacheSize
static getCacheSize()
Definition
class.autoloader.php:180
Autoloader\$failedLookups
static $failedLookups
Definition
class.autoloader.php:35
Autoloader\clearCache
static clearCache()
Definition
class.autoloader.php:164
Autoloader\load
load($class)
Definition
class.autoloader.php:65
Path\getCorePath
static getCorePath($aetrayPath=false)
Definition
class.path.php:408
UtilString\startWith
static startWith($string, $search)
Definition
class.util.string.php:53
sandbox
core
classes
class.autoloader.php
Generated by
1.17.0