Bearsampp 2026.5.5
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 (in_array($class, ['utilpath', 'utilstring', 'utilinput'], true)) {
111 $class = substr_replace($class, '.', 4, 0);
112 $file = $rootPath . '/classes/class.' . $class . '.php';
113 } elseif (UtilString::startWith($class, 'bin')) {
114 $class = $class != 'bins' ? substr_replace($class, '.', 3, 0) : $class;
115 $file = $rootPath . '/classes/bins/class.' . $class . '.php';
116 } elseif (UtilString::startWith($class, 'tool')) {
117 $class = $class != 'tools' ? substr_replace($class, '.', 4, 0) : $class;
118 $file = $rootPath . '/classes/tools/class.' . $class . '.php';
119 } elseif (UtilString::startWith($class, 'app')) {
120 $class = $class != 'apps' ? substr_replace($class, '.', 3, 0) : $class;
121 $file = $rootPath . '/classes/apps/class.' . $class . '.php';
122 } elseif (UtilString::startWith($class, 'action')) {
123 $class = $class != 'action' ? substr_replace($class, '.', 6, 0) : $class;
124 $file = $rootPath . '/classes/actions/class.' . $class . '.php';
125 } elseif (UtilString::startWith($class, 'tplapp') && $class != 'tplapp') {
126 $class = substr_replace(substr_replace($class, '.', 3, 0), '.', 7, 0);
127 $file = $rootPath . '/classes/tpls/app/class.' . $class . '.php';
128 } elseif (UtilString::startWith($class, 'tpl')) {
129 $class = $class != 'tpls' ? substr_replace($class, '.', 3, 0) : $class;
130 $file = $rootPath . '/classes/tpls/class.' . $class . '.php';
131 }
132
133 return $file;
134 }
135
142 public static function getStats()
143 {
144 return self::$stats;
145 }
146
153 public static function clearCache()
154 {
155 self::$classMap = [];
156 self::$failedLookups = [];
157 self::$stats = [
158 'hits' => 0,
159 'misses' => 0,
160 'failed_hits' => 0
161 ];
162 }
163
169 public static function getCacheSize()
170 {
171 return count(self::$classMap);
172 }
173
179 public function register()
180 {
181 return spl_autoload_register(array($this, 'load'));
182 }
183
189 public function unregister()
190 {
191 return spl_autoload_unregister(array($this, 'load'));
192 }
193}
global $bearsamppRoot
resolveClassPath($class, $rootPath)
static getCacheSize()
static $failedLookups
static clearCache()
static startWith($string, $search)