Bearsampp 2026.3.26
API documentation
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
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 = $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 }
97
106 private function resolveClassPath($class, $rootPath)
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 }
132
139 public static function getStats()
140 {
141 return self::$stats;
142 }
143
150 public static function clearCache()
151 {
152 self::$classMap = [];
153 self::$failedLookups = [];
154 self::$stats = [
155 'hits' => 0,
156 'misses' => 0,
157 'failed_hits' => 0
158 ];
159 }
160
166 public static function getCacheSize()
167 {
168 return count(self::$classMap);
169 }
170
176 public function register()
177 {
178 return spl_autoload_register(array($this, 'load'));
179 }
180
186 public function unregister()
187 {
188 return spl_autoload_unregister(array($this, 'load'));
189 }
190}
global $bearsamppRoot
resolveClassPath($class, $rootPath)
static getCacheSize()
static $failedLookups
static clearCache()
static startWith($string, $search)