Bearsampp 2026.7.11
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 = 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}
global $bearsamppRoot
resolveClassPath($class, $rootPath)
static getCacheSize()
static $failedLookups
static clearCache()
static getCorePath($aetrayPath=false)
static startWith($string, $search)