-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathAutoloader.php
More file actions
117 lines (95 loc) · 3.18 KB
/
Copy pathAutoloader.php
File metadata and controls
117 lines (95 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2025 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 3
*/
declare(strict_types=1);
namespace SMF;
/*
* An autoloader for certain classes.
*
* @param string $class The fully-qualified class name.
*/
spl_autoload_register(function ($class) {
static $hook_value = '';
static $class_map = [
// Some special cases.
'phpseclib3\\' => '{$sourcedir}/Phpseclib/',
'ReCaptcha\\' => '{$sourcedir}/ReCaptcha/',
'MatthiasMullie\\Minify\\' => '{$sourcedir}/minify/src/',
'MatthiasMullie\\PathConverter\\' => '{$sourcedir}/minify/path-converter/src/',
'ZxcvbnPhp\\' => '{$sourcedir}/ZxcvbnPhp/',
// The path to the Themes dir is hardcoded.
'SMF\\Themes\\' => '{$boarddir}/Themes/',
// In general, the SMF namespace maps to $sourcedir.
'SMF\\' => '{$sourcedir}/',
];
// Ensure $sourcedir is set to something valid.
if (class_exists(Config::class, false) && isset(Config::$sourcedir)) {
$sourcedir = Config::$sourcedir;
}
if (empty($sourcedir) || !is_dir($sourcedir)) {
$sourcedir = __DIR__;
}
// Ensure $boarddir is set to something valid.
if (class_exists(Config::class, false) && isset(Config::$boarddir)) {
$boarddir = Config::$boarddir;
}
if (empty($boarddir) || !is_dir($boarddir)) {
if (isset($_SERVER['SCRIPT_NAME'])) {
$boarddir = \dirname($_SERVER['SCRIPT_NAME']);
} elseif (!empty(debug_backtrace())) {
$bt = debug_backtrace();
$boarddir = \dirname(array_pop($bt)['file']);
} else {
$boarddir = \dirname($sourcedir);
}
}
// Do any third-party scripts want in on the fun?
if (!\defined('SMF_INSTALLING') && class_exists(Config::class, false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) {
if (!class_exists(IntegrationHook::class, false) && is_file($sourcedir . '/IntegrationHook.php')) {
require_once $sourcedir . '/IntegrationHook.php';
}
if (class_exists(IntegrationHook::class, false)) {
$hook_value = Config::$modSettings['integrate_autoload'];
IntegrationHook::call('integrate_autoload', [&$class_map]);
}
}
foreach ($class_map as $prefix => $dirname) {
// Does the class use the namespace prefix?
$len = \strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
continue;
}
// Get the relative class name.
$relative_class = substr($class, $len);
// If unspecified, assume it is relative to $sourcedir.
if (!str_contains($dirname, '{')) {
$dirname = '{$sourcedir}/' . $dirname;
}
// Get the full $dirname.
$dirname = strtr($dirname, [
'{$sourcedir}' => $sourcedir,
'{$boarddir}' => $boarddir,
]);
// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$filename = $dirname . strtr($relative_class, '\\', '/') . '.php';
// Failsafe: Never load a file named index.php.
if (basename($filename) === 'index.php') {
return;
}
// If the file exists, require it.
if (file_exists($filename)) {
require $filename;
return;
}
}
});