Bearsampp 2026.3.26
API documentation
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 50 of file class.autoloader.php.

51 {
52 }

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 150 of file class.autoloader.php.

151 {
152 self::$classMap = [];
153 self::$failedLookups = [];
154 self::$stats = [
155 'hits' => 0,
156 'misses' => 0,
157 'failed_hits' => 0
158 ];
159 }

◆ getCacheSize()

getCacheSize ( )
static

Gets the size of the class map cache.

Returns
int Number of cached class paths

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

167 {
168 return count(self::$classMap);
169 }

◆ 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 139 of file class.autoloader.php.

140 {
141 return self::$stats;
142 }

◆ 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 61 of file class.autoloader.php.

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

References $bearsamppRoot, and resolveClassPath().

◆ register()

register ( )

Registers the autoloader with the SPL autoload stack.

Returns
bool True on success, false on failure.

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

177 {
178 return spl_autoload_register(array($this, 'load'));
179 }

◆ 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 106 of file class.autoloader.php.

107 {
108 $file = $rootPath . '/classes/class.' . $class . '.php';
109
110 if (Util::startWith($class, 'bin')) {
111 $class = $class != 'bins' ? substr_replace($class, '.', 3, 0) : $class;
112 $file = $rootPath . '/classes/bins/class.' . $class . '.php';
113 } elseif (Util::startWith($class, 'tool')) {
114 $class = $class != 'tools' ? substr_replace($class, '.', 4, 0) : $class;
115 $file = $rootPath . '/classes/tools/class.' . $class . '.php';
116 } elseif (Util::startWith($class, 'app')) {
117 $class = $class != 'apps' ? substr_replace($class, '.', 3, 0) : $class;
118 $file = $rootPath . '/classes/apps/class.' . $class . '.php';
119 } elseif (Util::startWith($class, 'action')) {
120 $class = $class != 'action' ? substr_replace($class, '.', 6, 0) : $class;
121 $file = $rootPath . '/classes/actions/class.' . $class . '.php';
122 } elseif (Util::startWith($class, 'tplapp') && $class != 'tplapp') {
123 $class = substr_replace(substr_replace($class, '.', 3, 0), '.', 7, 0);
124 $file = $rootPath . '/classes/tpls/app/class.' . $class . '.php';
125 } elseif (Util::startWith($class, 'tpl')) {
126 $class = $class != 'tpls' ? substr_replace($class, '.', 3, 0) : $class;
127 $file = $rootPath . '/classes/tpls/class.' . $class . '.php';
128 }
129
130 return $file;
131 }
static startWith($string, $search)

References Util\startWith().

Referenced by load().

◆ unregister()

unregister ( )

Unregisters the autoloader from the SPL autoload stack.

Returns
bool True on success, false on failure.

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

187 {
188 return spl_autoload_unregister(array($this, 'load'));
189 }

Field Documentation

◆ $classMap

$classMap = []
staticprivate

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

◆ $failedLookups

$failedLookups = []
staticprivate

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

◆ $stats

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

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


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