Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Autoloader Class Reference

Public Member Functions

 __construct ()
 load ($class)
 register ()
 unregister ()

Static Public Member Functions

static clearCache ()
static getCacheSize ()
static getStats ()

Private Member Functions

 resolveClassPath ($class, $rootPath)

Static Private Attributes

static $classMap = []
static $failedLookups = []
static $stats

Detailed Description

Class Autoloader

This class handles the autoloading of classes within the Bearsampp application. It registers itself with the SPL autoload stack and loads classes based on naming conventions.

Performance optimizations:

  • Implements class path caching to avoid repeated file_exists() calls
  • Caches both successful and failed lookups to prevent redundant checks
  • Uses static cache that persists for the entire request lifecycle

Definition at line 21 of file class.autoloader.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Autoloader constructor.

Initializes the Autoloader object.

Definition at line 53 of file class.autoloader.php.

54 {
55 }

Member Function Documentation

◆ clearCache()

clearCache ( )
static

Clears the autoloader cache. Useful for testing or when class files are modified during runtime.

Returns
void

Definition at line 164 of file class.autoloader.php.

165 {
166 self::$classMap = [];
167 self::$failedLookups = [];
168 self::$stats = [
169 'hits' => 0,
170 'misses' => 0,
171 'failed_hits' => 0
172 ];
173 }

◆ getCacheSize()

getCacheSize ( )
static

Gets the size of the class map cache.

Returns
int Number of cached class paths

Definition at line 180 of file class.autoloader.php.

181 {
182 return count(self::$classMap);
183 }

◆ getStats()

getStats ( )
static

Gets the current cache statistics. Useful for monitoring and debugging cache effectiveness.

Returns
array Array containing hits, misses, and failed_hits counts

Definition at line 153 of file class.autoloader.php.

154 {
155 return self::$stats;
156 }

◆ load()

load ( $class)

Loads the specified class file based on the class name. Implements caching to improve performance on repeated class loads.

Parameters
string$classThe name of the class to load.
Returns
bool True if the class file was successfully loaded, false otherwise.

Definition at line 65 of file class.autoloader.php.

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 }
global $bearsamppRoot
resolveClassPath($class, $rootPath)
static getCorePath($aetrayPath=false)

References $bearsamppRoot, Path\getCorePath(), and resolveClassPath().

Here is the call graph for this function:

◆ register()

register ( )

Registers the autoloader with the SPL autoload stack.

Returns
bool True on success, false on failure.

Definition at line 190 of file class.autoloader.php.

191 {
192 return spl_autoload_register(array($this, 'load'));
193 }

◆ resolveClassPath()

resolveClassPath ( $class,
$rootPath )
private

Resolves the file path for a given class name based on naming conventions. Extracted into separate method for better maintainability and testability.

Parameters
string$classThe lowercase class name
string$rootPathThe root path to the classes directory
Returns
string The resolved file path

Definition at line 115 of file class.autoloader.php.

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 }
static startWith($string, $search)

References UtilString\startWith().

Referenced by load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unregister()

unregister ( )

Unregisters the autoloader from the SPL autoload stack.

Returns
bool True on success, false on failure.

Definition at line 200 of file class.autoloader.php.

201 {
202 return spl_autoload_unregister(array($this, 'load'));
203 }

Field Documentation

◆ $classMap

$classMap = []
staticprivate

Definition at line 28 of file class.autoloader.php.

◆ $failedLookups

$failedLookups = []
staticprivate

Definition at line 35 of file class.autoloader.php.

◆ $stats

$stats
staticprivate
Initial value:
= [
'hits' => 0,
'misses' => 0,
'failed_hits' => 0
]

Definition at line 42 of file class.autoloader.php.


The documentation for this class was generated from the following file: