Bearsampp
2026.7.11
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-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
21
class
Autoloader
22
{
27
private
static
$classMap
= [];
28
33
private
static
$failedLookups
= [];
34
39
private
static
$stats
= [
40
'hits'
=> 0,
41
'misses'
=> 0,
42
'failed_hits'
=> 0
43
];
44
50
public
function
__construct
()
51
{
52
}
53
61
public
function
load
($class)
62
{
63
global
$bearsamppRoot
;
64
65
$originalClass = $class;
66
$class = strtolower($class);
67
68
// Check if we've already successfully loaded this class
69
if
(isset(self::$classMap[$class])) {
70
self::$stats[
'hits'
]++;
71
require_once self::$classMap[$class];
72
return
true
;
73
}
74
75
// Check if we've already determined this class doesn't exist
76
if
(isset(self::$failedLookups[$class])) {
77
self::$stats[
'failed_hits'
]++;
78
return
false
;
79
}
80
81
self::$stats[
'misses'
]++;
82
83
$rootPath =
Path::getCorePath
();
84
$file = $this->
resolveClassPath
($class, $rootPath);
85
86
if
(!file_exists($file)) {
87
// Cache the failed lookup
88
self::$failedLookups[$class] =
true
;
89
return
false
;
90
}
91
92
// Cache the successful path resolution
93
self::$classMap[$class] = $file;
94
require_once $file;
95
return
true
;
96
}
97
106
private
function
resolveClassPath
($class, $rootPath)
107
{
108
$file = $rootPath .
'/classes/class.'
. $class .
'.php'
;
109
110
if
(in_array($class, [
'utilstring'
,
'utilinput'
],
true
)) {
111
$class = substr_replace($class,
'.'
, 4, 0);
112
$file = $rootPath .
'/classes/class.'
. $class .
'.php'
;
113
} elseif ($class ===
'path'
) {
114
$file = $rootPath .
'/classes/class.path.php'
;
115
} elseif (
UtilString::startWith
($class,
'bin'
)) {
116
$class = $class !=
'bins'
? substr_replace($class,
'.'
, 3, 0) : $class;
117
$file = $rootPath .
'/classes/bins/class.'
. $class .
'.php'
;
118
} elseif (
UtilString::startWith
($class,
'tool'
)) {
119
$class = $class !=
'tools'
? substr_replace($class,
'.'
, 4, 0) : $class;
120
$file = $rootPath .
'/classes/tools/class.'
. $class .
'.php'
;
121
} elseif (
UtilString::startWith
($class,
'app'
)) {
122
$class = $class !=
'apps'
? substr_replace($class,
'.'
, 3, 0) : $class;
123
$file = $rootPath .
'/classes/apps/class.'
. $class .
'.php'
;
124
} elseif (
UtilString::startWith
($class,
'action'
)) {
125
$class = $class !=
'action'
? substr_replace($class,
'.'
, 6, 0) : $class;
126
$file = $rootPath .
'/classes/actions/class.'
. $class .
'.php'
;
127
} elseif (
UtilString::startWith
($class,
'tplapp'
) && $class !=
'tplapp'
) {
128
$class = substr_replace(substr_replace($class,
'.'
, 3, 0),
'.'
, 7, 0);
129
$file = $rootPath .
'/classes/tpls/app/class.'
. $class .
'.php'
;
130
} elseif (
UtilString::startWith
($class,
'tpl'
)) {
131
$class = $class !=
'tpls'
? substr_replace($class,
'.'
, 3, 0) : $class;
132
$file = $rootPath .
'/classes/tpls/class.'
. $class .
'.php'
;
133
}
134
135
return
$file;
136
}
137
144
public
static
function
getStats
()
145
{
146
return
self::$stats;
147
}
148
155
public
static
function
clearCache
()
156
{
157
self::$classMap = [];
158
self::$failedLookups = [];
159
self::$stats = [
160
'hits'
=> 0,
161
'misses'
=> 0,
162
'failed_hits'
=> 0
163
];
164
}
165
171
public
static
function
getCacheSize
()
172
{
173
return
count(self::$classMap);
174
}
175
181
public
function
register
()
182
{
183
return
spl_autoload_register(array($this,
'load'
));
184
}
185
191
public
function
unregister
()
192
{
193
return
spl_autoload_unregister(array($this,
'load'
));
194
}
195
}
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
Autoloader
Definition
class.autoloader.php:22
Autoloader\__construct
__construct()
Definition
class.autoloader.php:50
Autoloader\unregister
unregister()
Definition
class.autoloader.php:191
Autoloader\getStats
static getStats()
Definition
class.autoloader.php:144
Autoloader\resolveClassPath
resolveClassPath($class, $rootPath)
Definition
class.autoloader.php:106
Autoloader\$stats
static $stats
Definition
class.autoloader.php:39
Autoloader\$classMap
static $classMap
Definition
class.autoloader.php:27
Autoloader\getCacheSize
static getCacheSize()
Definition
class.autoloader.php:171
Autoloader\$failedLookups
static $failedLookups
Definition
class.autoloader.php:33
Autoloader\clearCache
static clearCache()
Definition
class.autoloader.php:155
Autoloader\load
load($class)
Definition
class.autoloader.php:61
Path\getCorePath
static getCorePath($aetrayPath=false)
Definition
class.path.php:386
UtilString\startWith
static startWith($string, $search)
Definition
class.util.string.php:53
sandbox
core
classes
class.autoloader.php
Generated by
1.17.0