2024.8.23
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
10/**
11 * Class Autoloader
12 *
13 * This class handles the autoloading of classes within the Bearsampp application.
14 * It registers itself with the SPL autoload stack and loads classes based on naming conventions.
15 */
17{
18 /**
19 * Autoloader constructor.
20 *
21 * Initializes the Autoloader object.
22 */
23 public function __construct()
24 {
25 }
26
27 /**
28 * Loads the specified class file based on the class name.
29 *
30 * @param string $class The name of the class to load.
31 * @return bool True if the class file was successfully loaded, false otherwise.
32 */
33 public function load($class)
34 {
35 global $bearsamppRoot;
36
37 $class = strtolower($class);
38 $rootPath = $bearsamppRoot->getCorePath();
39
40 $file = $rootPath . '/classes/class.' . $class . '.php';
41 if (Util::startWith($class, 'bin')) {
42 $class = $class != 'bins' ? substr_replace($class, '.', 3, 0) : $class;
43 $file = $rootPath . '/classes/bins/class.' . $class . '.php';
44 } elseif (Util::startWith($class, 'tool')) {
45 $class = $class != 'tools' ? substr_replace($class, '.', 4, 0) : $class;
46 $file = $rootPath . '/classes/tools/class.' . $class . '.php';
47 } elseif (Util::startWith($class, 'app')) {
48 $class = $class != 'apps' ? substr_replace($class, '.', 3, 0) : $class;
49 $file = $rootPath . '/classes/apps/class.' . $class . '.php';
50 } elseif (Util::startWith($class, 'action')) {
51 $class = $class != 'action' ? substr_replace($class, '.', 6, 0) : $class;
52 $file = $rootPath . '/classes/actions/class.' . $class . '.php';
53 } elseif (Util::startWith($class, 'tplapp') && $class != 'tplapp') {
54 $class = substr_replace(substr_replace($class, '.', 3, 0), '.', 7, 0);
55 $file = $rootPath . '/classes/tpls/app/class.' . $class . '.php';
56 } elseif (Util::startWith($class, 'tpl')) {
57 $class = $class != 'tpls' ? substr_replace($class, '.', 3, 0) : $class;
58 $file = $rootPath . '/classes/tpls/class.' . $class . '.php';
59 }
60
61 if (!file_exists($file)) {
62 return false;
63 }
64
65 require_once $file;
66 return true;
67 }
68
69 /**
70 * Registers the autoloader with the SPL autoload stack.
71 *
72 * @return bool True on success, false on failure.
73 */
74 public function register()
75 {
76 return spl_autoload_register(array($this, 'load'));
77 }
78
79 /**
80 * Unregisters the autoloader from the SPL autoload stack.
81 *
82 * @return bool True on success, false on failure.
83 */
84 public function unregister()
85 {
86 return spl_autoload_unregister(array($this, 'load'));
87 }
88}
global $bearsamppRoot
static startWith($string, $search)